1 //===- bolt/Rewrite/RewriteInstance.cpp - ELF rewriter --------------------===//
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 "bolt/Rewrite/RewriteInstance.h"
10 #include "bolt/Core/BinaryContext.h"
11 #include "bolt/Core/BinaryEmitter.h"
12 #include "bolt/Core/BinaryFunction.h"
13 #include "bolt/Core/DebugData.h"
14 #include "bolt/Core/Exceptions.h"
15 #include "bolt/Core/MCPlusBuilder.h"
16 #include "bolt/Core/ParallelUtilities.h"
17 #include "bolt/Core/Relocation.h"
18 #include "bolt/Passes/CacheMetrics.h"
19 #include "bolt/Passes/ReorderFunctions.h"
20 #include "bolt/Profile/BoltAddressTranslation.h"
21 #include "bolt/Profile/DataAggregator.h"
22 #include "bolt/Profile/DataReader.h"
23 #include "bolt/Profile/YAMLProfileReader.h"
24 #include "bolt/Profile/YAMLProfileWriter.h"
25 #include "bolt/Rewrite/BinaryPassManager.h"
26 #include "bolt/Rewrite/DWARFRewriter.h"
27 #include "bolt/Rewrite/ExecutableFileMemoryManager.h"
28 #include "bolt/RuntimeLibs/HugifyRuntimeLibrary.h"
29 #include "bolt/RuntimeLibs/InstrumentationRuntimeLibrary.h"
30 #include "bolt/Utils/CommandLineOpts.h"
31 #include "bolt/Utils/Utils.h"
32 #include "llvm/ADT/Optional.h"
33 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
34 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
35 #include "llvm/ExecutionEngine/RuntimeDyld.h"
36 #include "llvm/MC/MCAsmBackend.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/MC/MCAsmLayout.h"
39 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
40 #include "llvm/MC/MCObjectStreamer.h"
41 #include "llvm/MC/MCStreamer.h"
42 #include "llvm/MC/MCSymbol.h"
43 #include "llvm/MC/TargetRegistry.h"
44 #include "llvm/Object/ObjectFile.h"
45 #include "llvm/Support/Alignment.h"
46 #include "llvm/Support/Casting.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/DataExtractor.h"
49 #include "llvm/Support/Errc.h"
50 #include "llvm/Support/Error.h"
51 #include "llvm/Support/FileSystem.h"
52 #include "llvm/Support/LEB128.h"
53 #include "llvm/Support/ManagedStatic.h"
54 #include "llvm/Support/Timer.h"
55 #include "llvm/Support/ToolOutputFile.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include <algorithm>
58 #include <fstream>
59 #include <memory>
60 #include <system_error>
61 
62 #undef  DEBUG_TYPE
63 #define DEBUG_TYPE "bolt"
64 
65 using namespace llvm;
66 using namespace object;
67 using namespace bolt;
68 
69 extern cl::opt<uint32_t> X86AlignBranchBoundary;
70 extern cl::opt<bool> X86AlignBranchWithin32BBoundaries;
71 
72 namespace opts {
73 
74 extern cl::opt<MacroFusionType> AlignMacroOpFusion;
75 extern cl::list<std::string> HotTextMoveSections;
76 extern cl::opt<bool> Hugify;
77 extern cl::opt<bool> Instrument;
78 extern cl::opt<JumpTableSupportLevel> JumpTables;
79 extern cl::list<std::string> ReorderData;
80 extern cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions;
81 extern cl::opt<bool> TimeBuild;
82 
83 static cl::opt<bool> ForceToDataRelocations(
84     "force-data-relocations",
85     cl::desc("force relocations to data sections to always be processed"),
86 
87     cl::Hidden, cl::cat(BoltCategory));
88 
89 cl::opt<std::string>
90     BoltID("bolt-id",
91            cl::desc("add any string to tag this execution in the "
92                     "output binary via bolt info section"),
93            cl::cat(BoltCategory));
94 
95 cl::opt<bool>
96 AllowStripped("allow-stripped",
97   cl::desc("allow processing of stripped binaries"),
98   cl::Hidden,
99   cl::cat(BoltCategory));
100 
101 cl::opt<bool> DumpDotAll(
102     "dump-dot-all",
103     cl::desc("dump function CFGs to graphviz format after each stage;"
104              "enable '-print-loops' for color-coded blocks"),
105     cl::Hidden, cl::cat(BoltCategory));
106 
107 static cl::list<std::string>
108 ForceFunctionNames("funcs",
109   cl::CommaSeparated,
110   cl::desc("limit optimizations to functions from the list"),
111   cl::value_desc("func1,func2,func3,..."),
112   cl::Hidden,
113   cl::cat(BoltCategory));
114 
115 static cl::opt<std::string>
116 FunctionNamesFile("funcs-file",
117   cl::desc("file with list of functions to optimize"),
118   cl::Hidden,
119   cl::cat(BoltCategory));
120 
121 static cl::list<std::string> ForceFunctionNamesNR(
122     "funcs-no-regex", cl::CommaSeparated,
123     cl::desc("limit optimizations to functions from the list (non-regex)"),
124     cl::value_desc("func1,func2,func3,..."), cl::Hidden, cl::cat(BoltCategory));
125 
126 static cl::opt<std::string> FunctionNamesFileNR(
127     "funcs-file-no-regex",
128     cl::desc("file with list of functions to optimize (non-regex)"), cl::Hidden,
129     cl::cat(BoltCategory));
130 
131 cl::opt<bool>
132 KeepTmp("keep-tmp",
133   cl::desc("preserve intermediate .o file"),
134   cl::Hidden,
135   cl::cat(BoltCategory));
136 
137 cl::opt<bool> Lite("lite", cl::desc("skip processing of cold functions"),
138                    cl::cat(BoltCategory));
139 
140 static cl::opt<unsigned>
141 LiteThresholdPct("lite-threshold-pct",
142   cl::desc("threshold (in percent) for selecting functions to process in lite "
143             "mode. Higher threshold means fewer functions to process. E.g "
144             "threshold of 90 means only top 10 percent of functions with "
145             "profile will be processed."),
146   cl::init(0),
147   cl::ZeroOrMore,
148   cl::Hidden,
149   cl::cat(BoltOptCategory));
150 
151 static cl::opt<unsigned> LiteThresholdCount(
152     "lite-threshold-count",
153     cl::desc("similar to '-lite-threshold-pct' but specify threshold using "
154              "absolute function call count. I.e. limit processing to functions "
155              "executed at least the specified number of times."),
156     cl::init(0), cl::Hidden, cl::cat(BoltOptCategory));
157 
158 static cl::opt<unsigned>
159     MaxFunctions("max-funcs",
160                  cl::desc("maximum number of functions to process"), cl::Hidden,
161                  cl::cat(BoltCategory));
162 
163 static cl::opt<unsigned> MaxDataRelocations(
164     "max-data-relocations",
165     cl::desc("maximum number of data relocations to process"), cl::Hidden,
166     cl::cat(BoltCategory));
167 
168 cl::opt<bool> PrintAll("print-all",
169                        cl::desc("print functions after each stage"), cl::Hidden,
170                        cl::cat(BoltCategory));
171 
172 cl::opt<bool> PrintCFG("print-cfg",
173                        cl::desc("print functions after CFG construction"),
174                        cl::Hidden, cl::cat(BoltCategory));
175 
176 cl::opt<bool> PrintDisasm("print-disasm",
177                           cl::desc("print function after disassembly"),
178                           cl::Hidden, cl::cat(BoltCategory));
179 
180 static cl::opt<bool>
181     PrintGlobals("print-globals",
182                  cl::desc("print global symbols after disassembly"), cl::Hidden,
183                  cl::cat(BoltCategory));
184 
185 extern cl::opt<bool> PrintSections;
186 
187 static cl::opt<bool> PrintLoopInfo("print-loops",
188                                    cl::desc("print loop related information"),
189                                    cl::Hidden, cl::cat(BoltCategory));
190 
191 static cl::opt<bool> PrintSDTMarkers("print-sdt",
192                                      cl::desc("print all SDT markers"),
193                                      cl::Hidden, cl::cat(BoltCategory));
194 
195 enum PrintPseudoProbesOptions {
196   PPP_None = 0,
197   PPP_Probes_Section_Decode = 0x1,
198   PPP_Probes_Address_Conversion = 0x2,
199   PPP_Encoded_Probes = 0x3,
200   PPP_All = 0xf
201 };
202 
203 cl::opt<PrintPseudoProbesOptions> PrintPseudoProbes(
204     "print-pseudo-probes", cl::desc("print pseudo probe info"),
205     cl::init(PPP_None),
206     cl::values(clEnumValN(PPP_Probes_Section_Decode, "decode",
207                           "decode probes section from binary"),
208                clEnumValN(PPP_Probes_Address_Conversion, "address_conversion",
209                           "update address2ProbesMap with output block address"),
210                clEnumValN(PPP_Encoded_Probes, "encoded_probes",
211                           "display the encoded probes in binary section"),
212                clEnumValN(PPP_All, "all", "enable all debugging printout")),
213     cl::ZeroOrMore, cl::Hidden, cl::cat(BoltCategory));
214 
215 static cl::opt<cl::boolOrDefault> RelocationMode(
216     "relocs", cl::desc("use relocations in the binary (default=autodetect)"),
217     cl::cat(BoltCategory));
218 
219 static cl::opt<std::string>
220 SaveProfile("w",
221   cl::desc("save recorded profile to a file"),
222   cl::cat(BoltOutputCategory));
223 
224 static cl::list<std::string>
225 SkipFunctionNames("skip-funcs",
226   cl::CommaSeparated,
227   cl::desc("list of functions to skip"),
228   cl::value_desc("func1,func2,func3,..."),
229   cl::Hidden,
230   cl::cat(BoltCategory));
231 
232 static cl::opt<std::string>
233 SkipFunctionNamesFile("skip-funcs-file",
234   cl::desc("file with list of functions to skip"),
235   cl::Hidden,
236   cl::cat(BoltCategory));
237 
238 cl::opt<bool>
239 TrapOldCode("trap-old-code",
240   cl::desc("insert traps in old function bodies (relocation mode)"),
241   cl::Hidden,
242   cl::cat(BoltCategory));
243 
244 static cl::opt<std::string> DWPPathName("dwp",
245                                         cl::desc("Path and name to DWP file."),
246                                         cl::Hidden, cl::init(""),
247                                         cl::cat(BoltCategory));
248 
249 static cl::opt<bool>
250 UseGnuStack("use-gnu-stack",
251   cl::desc("use GNU_STACK program header for new segment (workaround for "
252            "issues with strip/objcopy)"),
253   cl::ZeroOrMore,
254   cl::cat(BoltCategory));
255 
256 static cl::opt<bool>
257     TimeRewrite("time-rewrite",
258                 cl::desc("print time spent in rewriting passes"), cl::Hidden,
259                 cl::cat(BoltCategory));
260 
261 static cl::opt<bool>
262 SequentialDisassembly("sequential-disassembly",
263   cl::desc("performs disassembly sequentially"),
264   cl::init(false),
265   cl::cat(BoltOptCategory));
266 
267 static cl::opt<bool> WriteBoltInfoSection(
268     "bolt-info", cl::desc("write bolt info section in the output binary"),
269     cl::init(true), cl::Hidden, cl::cat(BoltOutputCategory));
270 
271 } // namespace opts
272 
273 constexpr const char *RewriteInstance::SectionsToOverwrite[];
274 std::vector<std::string> RewriteInstance::DebugSectionsToOverwrite = {
275     ".debug_abbrev", ".debug_aranges",  ".debug_line",   ".debug_line_str",
276     ".debug_loc",    ".debug_loclists", ".debug_ranges", ".debug_rnglists",
277     ".gdb_index",    ".debug_addr"};
278 
279 const char RewriteInstance::TimerGroupName[] = "rewrite";
280 const char RewriteInstance::TimerGroupDesc[] = "Rewrite passes";
281 
282 namespace llvm {
283 namespace bolt {
284 
285 extern const char *BoltRevision;
286 
287 MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
288                                    const MCInstrAnalysis *Analysis,
289                                    const MCInstrInfo *Info,
290                                    const MCRegisterInfo *RegInfo) {
291 #ifdef X86_AVAILABLE
292   if (Arch == Triple::x86_64)
293     return createX86MCPlusBuilder(Analysis, Info, RegInfo);
294 #endif
295 
296 #ifdef AARCH64_AVAILABLE
297   if (Arch == Triple::aarch64)
298     return createAArch64MCPlusBuilder(Analysis, Info, RegInfo);
299 #endif
300 
301   llvm_unreachable("architecture unsupported by MCPlusBuilder");
302 }
303 
304 } // namespace bolt
305 } // namespace llvm
306 
307 namespace {
308 
309 bool refersToReorderedSection(ErrorOr<BinarySection &> Section) {
310   auto Itr =
311       std::find_if(opts::ReorderData.begin(), opts::ReorderData.end(),
312                    [&](const std::string &SectionName) {
313                      return (Section && Section->getName() == SectionName);
314                    });
315   return Itr != opts::ReorderData.end();
316 }
317 
318 } // anonymous namespace
319 
320 Expected<std::unique_ptr<RewriteInstance>>
321 RewriteInstance::createRewriteInstance(ELFObjectFileBase *File, const int Argc,
322                                        const char *const *Argv,
323                                        StringRef ToolPath) {
324   Error Err = Error::success();
325   auto RI = std::make_unique<RewriteInstance>(File, Argc, Argv, ToolPath, Err);
326   if (Err)
327     return std::move(Err);
328   return std::move(RI);
329 }
330 
331 RewriteInstance::RewriteInstance(ELFObjectFileBase *File, const int Argc,
332                                  const char *const *Argv, StringRef ToolPath,
333                                  Error &Err)
334     : InputFile(File), Argc(Argc), Argv(Argv), ToolPath(ToolPath),
335       SHStrTab(StringTableBuilder::ELF) {
336   ErrorAsOutParameter EAO(&Err);
337   auto ELF64LEFile = dyn_cast<ELF64LEObjectFile>(InputFile);
338   if (!ELF64LEFile) {
339     Err = createStringError(errc::not_supported,
340                             "Only 64-bit LE ELF binaries are supported");
341     return;
342   }
343 
344   bool IsPIC = false;
345   const ELFFile<ELF64LE> &Obj = ELF64LEFile->getELFFile();
346   if (Obj.getHeader().e_type != ELF::ET_EXEC) {
347     outs() << "BOLT-INFO: shared object or position-independent executable "
348               "detected\n";
349     IsPIC = true;
350   }
351 
352   auto BCOrErr = BinaryContext::createBinaryContext(
353       File, IsPIC,
354       DWARFContext::create(*File, DWARFContext::ProcessDebugRelocations::Ignore,
355                            nullptr, opts::DWPPathName,
356                            WithColor::defaultErrorHandler,
357                            WithColor::defaultWarningHandler));
358   if (Error E = BCOrErr.takeError()) {
359     Err = std::move(E);
360     return;
361   }
362   BC = std::move(BCOrErr.get());
363   BC->initializeTarget(std::unique_ptr<MCPlusBuilder>(createMCPlusBuilder(
364       BC->TheTriple->getArch(), BC->MIA.get(), BC->MII.get(), BC->MRI.get())));
365 
366   BAT = std::make_unique<BoltAddressTranslation>(*BC);
367 
368   if (opts::UpdateDebugSections)
369     DebugInfoRewriter = std::make_unique<DWARFRewriter>(*BC);
370 
371   if (opts::Instrument)
372     BC->setRuntimeLibrary(std::make_unique<InstrumentationRuntimeLibrary>());
373   else if (opts::Hugify)
374     BC->setRuntimeLibrary(std::make_unique<HugifyRuntimeLibrary>());
375 }
376 
377 RewriteInstance::~RewriteInstance() {}
378 
379 Error RewriteInstance::setProfile(StringRef Filename) {
380   if (!sys::fs::exists(Filename))
381     return errorCodeToError(make_error_code(errc::no_such_file_or_directory));
382 
383   if (ProfileReader) {
384     // Already exists
385     return make_error<StringError>(Twine("multiple profiles specified: ") +
386                                        ProfileReader->getFilename() + " and " +
387                                        Filename,
388                                    inconvertibleErrorCode());
389   }
390 
391   // Spawn a profile reader based on file contents.
392   if (DataAggregator::checkPerfDataMagic(Filename))
393     ProfileReader = std::make_unique<DataAggregator>(Filename);
394   else if (YAMLProfileReader::isYAML(Filename))
395     ProfileReader = std::make_unique<YAMLProfileReader>(Filename);
396   else
397     ProfileReader = std::make_unique<DataReader>(Filename);
398 
399   return Error::success();
400 }
401 
402 /// Return true if the function \p BF should be disassembled.
403 static bool shouldDisassemble(const BinaryFunction &BF) {
404   if (BF.isPseudo())
405     return false;
406 
407   if (opts::processAllFunctions())
408     return true;
409 
410   return !BF.isIgnored();
411 }
412 
413 Error RewriteInstance::discoverStorage() {
414   NamedRegionTimer T("discoverStorage", "discover storage", TimerGroupName,
415                      TimerGroupDesc, opts::TimeRewrite);
416 
417   // Stubs are harmful because RuntimeDyld may try to increase the size of
418   // sections accounting for stubs when we need those sections to match the
419   // same size seen in the input binary, in case this section is a copy
420   // of the original one seen in the binary.
421   BC->EFMM.reset(new ExecutableFileMemoryManager(*BC, /*AllowStubs*/ false));
422 
423   auto ELF64LEFile = dyn_cast<ELF64LEObjectFile>(InputFile);
424   const ELFFile<ELF64LE> &Obj = ELF64LEFile->getELFFile();
425 
426   BC->StartFunctionAddress = Obj.getHeader().e_entry;
427 
428   NextAvailableAddress = 0;
429   uint64_t NextAvailableOffset = 0;
430   Expected<ELF64LE::PhdrRange> PHsOrErr = Obj.program_headers();
431   if (Error E = PHsOrErr.takeError())
432     return E;
433 
434   ELF64LE::PhdrRange PHs = PHsOrErr.get();
435   for (const ELF64LE::Phdr &Phdr : PHs) {
436     switch (Phdr.p_type) {
437     case ELF::PT_LOAD:
438       BC->FirstAllocAddress = std::min(BC->FirstAllocAddress,
439                                        static_cast<uint64_t>(Phdr.p_vaddr));
440       NextAvailableAddress = std::max(NextAvailableAddress,
441                                       Phdr.p_vaddr + Phdr.p_memsz);
442       NextAvailableOffset = std::max(NextAvailableOffset,
443                                      Phdr.p_offset + Phdr.p_filesz);
444 
445       BC->SegmentMapInfo[Phdr.p_vaddr] = SegmentInfo{Phdr.p_vaddr,
446                                                      Phdr.p_memsz,
447                                                      Phdr.p_offset,
448                                                      Phdr.p_filesz,
449                                                      Phdr.p_align};
450       break;
451     case ELF::PT_INTERP:
452       BC->HasInterpHeader = true;
453       break;
454     }
455   }
456 
457   for (const SectionRef &Section : InputFile->sections()) {
458     Expected<StringRef> SectionNameOrErr = Section.getName();
459     if (Error E = SectionNameOrErr.takeError())
460       return E;
461     StringRef SectionName = SectionNameOrErr.get();
462     if (SectionName == ".text") {
463       BC->OldTextSectionAddress = Section.getAddress();
464       BC->OldTextSectionSize = Section.getSize();
465 
466       Expected<StringRef> SectionContentsOrErr = Section.getContents();
467       if (Error E = SectionContentsOrErr.takeError())
468         return E;
469       StringRef SectionContents = SectionContentsOrErr.get();
470       BC->OldTextSectionOffset =
471           SectionContents.data() - InputFile->getData().data();
472     }
473 
474     if (!opts::HeatmapMode &&
475         !(opts::AggregateOnly && BAT->enabledFor(InputFile)) &&
476         (SectionName.startswith(getOrgSecPrefix()) ||
477          SectionName == getBOLTTextSectionName()))
478       return createStringError(
479           errc::function_not_supported,
480           "BOLT-ERROR: input file was processed by BOLT. Cannot re-optimize");
481   }
482 
483   if (!NextAvailableAddress || !NextAvailableOffset)
484     return createStringError(errc::executable_format_error,
485                              "no PT_LOAD pheader seen");
486 
487   outs() << "BOLT-INFO: first alloc address is 0x"
488          << Twine::utohexstr(BC->FirstAllocAddress) << '\n';
489 
490   FirstNonAllocatableOffset = NextAvailableOffset;
491 
492   NextAvailableAddress = alignTo(NextAvailableAddress, BC->PageAlign);
493   NextAvailableOffset = alignTo(NextAvailableOffset, BC->PageAlign);
494 
495   if (!opts::UseGnuStack) {
496     // This is where the black magic happens. Creating PHDR table in a segment
497     // other than that containing ELF header is tricky. Some loaders and/or
498     // parts of loaders will apply e_phoff from ELF header assuming both are in
499     // the same segment, while others will do the proper calculation.
500     // We create the new PHDR table in such a way that both of the methods
501     // of loading and locating the table work. There's a slight file size
502     // overhead because of that.
503     //
504     // NB: bfd's strip command cannot do the above and will corrupt the
505     //     binary during the process of stripping non-allocatable sections.
506     if (NextAvailableOffset <= NextAvailableAddress - BC->FirstAllocAddress)
507       NextAvailableOffset = NextAvailableAddress - BC->FirstAllocAddress;
508     else
509       NextAvailableAddress = NextAvailableOffset + BC->FirstAllocAddress;
510 
511     assert(NextAvailableOffset ==
512                NextAvailableAddress - BC->FirstAllocAddress &&
513            "PHDR table address calculation error");
514 
515     outs() << "BOLT-INFO: creating new program header table at address 0x"
516            << Twine::utohexstr(NextAvailableAddress) << ", offset 0x"
517            << Twine::utohexstr(NextAvailableOffset) << '\n';
518 
519     PHDRTableAddress = NextAvailableAddress;
520     PHDRTableOffset = NextAvailableOffset;
521 
522     // Reserve space for 3 extra pheaders.
523     unsigned Phnum = Obj.getHeader().e_phnum;
524     Phnum += 3;
525 
526     NextAvailableAddress += Phnum * sizeof(ELF64LEPhdrTy);
527     NextAvailableOffset += Phnum * sizeof(ELF64LEPhdrTy);
528   }
529 
530   // Align at cache line.
531   NextAvailableAddress = alignTo(NextAvailableAddress, 64);
532   NextAvailableOffset = alignTo(NextAvailableOffset, 64);
533 
534   NewTextSegmentAddress = NextAvailableAddress;
535   NewTextSegmentOffset = NextAvailableOffset;
536   BC->LayoutStartAddress = NextAvailableAddress;
537 
538   // Tools such as objcopy can strip section contents but leave header
539   // entries. Check that at least .text is mapped in the file.
540   if (!getFileOffsetForAddress(BC->OldTextSectionAddress))
541     return createStringError(errc::executable_format_error,
542                              "BOLT-ERROR: input binary is not a valid ELF "
543                              "executable as its text section is not "
544                              "mapped to a valid segment");
545   return Error::success();
546 }
547 
548 void RewriteInstance::parseSDTNotes() {
549   if (!SDTSection)
550     return;
551 
552   StringRef Buf = SDTSection->getContents();
553   DataExtractor DE = DataExtractor(Buf, BC->AsmInfo->isLittleEndian(),
554                                    BC->AsmInfo->getCodePointerSize());
555   uint64_t Offset = 0;
556 
557   while (DE.isValidOffset(Offset)) {
558     uint32_t NameSz = DE.getU32(&Offset);
559     DE.getU32(&Offset); // skip over DescSz
560     uint32_t Type = DE.getU32(&Offset);
561     Offset = alignTo(Offset, 4);
562 
563     if (Type != 3)
564       errs() << "BOLT-WARNING: SDT note type \"" << Type
565              << "\" is not expected\n";
566 
567     if (NameSz == 0)
568       errs() << "BOLT-WARNING: SDT note has empty name\n";
569 
570     StringRef Name = DE.getCStr(&Offset);
571 
572     if (!Name.equals("stapsdt"))
573       errs() << "BOLT-WARNING: SDT note name \"" << Name
574              << "\" is not expected\n";
575 
576     // Parse description
577     SDTMarkerInfo Marker;
578     Marker.PCOffset = Offset;
579     Marker.PC = DE.getU64(&Offset);
580     Marker.Base = DE.getU64(&Offset);
581     Marker.Semaphore = DE.getU64(&Offset);
582     Marker.Provider = DE.getCStr(&Offset);
583     Marker.Name = DE.getCStr(&Offset);
584     Marker.Args = DE.getCStr(&Offset);
585     Offset = alignTo(Offset, 4);
586     BC->SDTMarkers[Marker.PC] = Marker;
587   }
588 
589   if (opts::PrintSDTMarkers)
590     printSDTMarkers();
591 }
592 
593 void RewriteInstance::parsePseudoProbe() {
594   if (!PseudoProbeDescSection && !PseudoProbeSection) {
595     // pesudo probe is not added to binary. It is normal and no warning needed.
596     return;
597   }
598 
599   // If only one section is found, it might mean the ELF is corrupted.
600   if (!PseudoProbeDescSection) {
601     errs() << "BOLT-WARNING: fail in reading .pseudo_probe_desc binary\n";
602     return;
603   } else if (!PseudoProbeSection) {
604     errs() << "BOLT-WARNING: fail in reading .pseudo_probe binary\n";
605     return;
606   }
607 
608   StringRef Contents = PseudoProbeDescSection->getContents();
609   if (!BC->ProbeDecoder.buildGUID2FuncDescMap(
610           reinterpret_cast<const uint8_t *>(Contents.data()),
611           Contents.size())) {
612     errs() << "BOLT-WARNING: fail in building GUID2FuncDescMap\n";
613     return;
614   }
615   Contents = PseudoProbeSection->getContents();
616   if (!BC->ProbeDecoder.buildAddress2ProbeMap(
617           reinterpret_cast<const uint8_t *>(Contents.data()),
618           Contents.size())) {
619     BC->ProbeDecoder.getAddress2ProbesMap().clear();
620     errs() << "BOLT-WARNING: fail in building Address2ProbeMap\n";
621     return;
622   }
623 
624   if (opts::PrintPseudoProbes == opts::PrintPseudoProbesOptions::PPP_All ||
625       opts::PrintPseudoProbes ==
626           opts::PrintPseudoProbesOptions::PPP_Probes_Section_Decode) {
627     outs() << "Report of decoding input pseudo probe binaries \n";
628     BC->ProbeDecoder.printGUID2FuncDescMap(outs());
629     BC->ProbeDecoder.printProbesForAllAddresses(outs());
630   }
631 }
632 
633 void RewriteInstance::printSDTMarkers() {
634   outs() << "BOLT-INFO: Number of SDT markers is " << BC->SDTMarkers.size()
635          << "\n";
636   for (auto It : BC->SDTMarkers) {
637     SDTMarkerInfo &Marker = It.second;
638     outs() << "BOLT-INFO: PC: " << utohexstr(Marker.PC)
639            << ", Base: " << utohexstr(Marker.Base)
640            << ", Semaphore: " << utohexstr(Marker.Semaphore)
641            << ", Provider: " << Marker.Provider << ", Name: " << Marker.Name
642            << ", Args: " << Marker.Args << "\n";
643   }
644 }
645 
646 void RewriteInstance::parseBuildID() {
647   if (!BuildIDSection)
648     return;
649 
650   StringRef Buf = BuildIDSection->getContents();
651 
652   // Reading notes section (see Portable Formats Specification, Version 1.1,
653   // pg 2-5, section "Note Section").
654   DataExtractor DE = DataExtractor(Buf, true, 8);
655   uint64_t Offset = 0;
656   if (!DE.isValidOffset(Offset))
657     return;
658   uint32_t NameSz = DE.getU32(&Offset);
659   if (!DE.isValidOffset(Offset))
660     return;
661   uint32_t DescSz = DE.getU32(&Offset);
662   if (!DE.isValidOffset(Offset))
663     return;
664   uint32_t Type = DE.getU32(&Offset);
665 
666   LLVM_DEBUG(dbgs() << "NameSz = " << NameSz << "; DescSz = " << DescSz
667                     << "; Type = " << Type << "\n");
668 
669   // Type 3 is a GNU build-id note section
670   if (Type != 3)
671     return;
672 
673   StringRef Name = Buf.slice(Offset, Offset + NameSz);
674   Offset = alignTo(Offset + NameSz, 4);
675   if (Name.substr(0, 3) != "GNU")
676     return;
677 
678   BuildID = Buf.slice(Offset, Offset + DescSz);
679 }
680 
681 Optional<std::string> RewriteInstance::getPrintableBuildID() const {
682   if (BuildID.empty())
683     return NoneType();
684 
685   std::string Str;
686   raw_string_ostream OS(Str);
687   const unsigned char *CharIter = BuildID.bytes_begin();
688   while (CharIter != BuildID.bytes_end()) {
689     if (*CharIter < 0x10)
690       OS << "0";
691     OS << Twine::utohexstr(*CharIter);
692     ++CharIter;
693   }
694   return OS.str();
695 }
696 
697 void RewriteInstance::patchBuildID() {
698   raw_fd_ostream &OS = Out->os();
699 
700   if (BuildID.empty())
701     return;
702 
703   size_t IDOffset = BuildIDSection->getContents().rfind(BuildID);
704   assert(IDOffset != StringRef::npos && "failed to patch build-id");
705 
706   uint64_t FileOffset = getFileOffsetForAddress(BuildIDSection->getAddress());
707   if (!FileOffset) {
708     errs() << "BOLT-WARNING: Non-allocatable build-id will not be updated.\n";
709     return;
710   }
711 
712   char LastIDByte = BuildID[BuildID.size() - 1];
713   LastIDByte ^= 1;
714   OS.pwrite(&LastIDByte, 1, FileOffset + IDOffset + BuildID.size() - 1);
715 
716   outs() << "BOLT-INFO: patched build-id (flipped last bit)\n";
717 }
718 
719 Error RewriteInstance::run() {
720   assert(BC && "failed to create a binary context");
721 
722   outs() << "BOLT-INFO: Target architecture: "
723          << Triple::getArchTypeName(
724                 (llvm::Triple::ArchType)InputFile->getArch())
725          << "\n";
726   outs() << "BOLT-INFO: BOLT version: " << BoltRevision << "\n";
727 
728   if (Error E = discoverStorage())
729     return E;
730   if (Error E = readSpecialSections())
731     return E;
732   adjustCommandLineOptions();
733   discoverFileObjects();
734 
735   preprocessProfileData();
736 
737   // Skip disassembling if we have a translation table and we are running an
738   // aggregation job.
739   if (opts::AggregateOnly && BAT->enabledFor(InputFile)) {
740     processProfileData();
741     return Error::success();
742   }
743 
744   selectFunctionsToProcess();
745 
746   readDebugInfo();
747 
748   disassembleFunctions();
749 
750   processProfileDataPreCFG();
751 
752   buildFunctionsCFG();
753 
754   processProfileData();
755 
756   postProcessFunctions();
757 
758   if (opts::DiffOnly)
759     return Error::success();
760 
761   runOptimizationPasses();
762 
763   emitAndLink();
764 
765   updateMetadata();
766 
767   if (opts::LinuxKernelMode) {
768     errs() << "BOLT-WARNING: not writing the output file for Linux Kernel\n";
769     return Error::success();
770   } else if (opts::OutputFilename == "/dev/null") {
771     outs() << "BOLT-INFO: skipping writing final binary to disk\n";
772     return Error::success();
773   }
774 
775   // Rewrite allocatable contents and copy non-allocatable parts with mods.
776   rewriteFile();
777   return Error::success();
778 }
779 
780 void RewriteInstance::discoverFileObjects() {
781   NamedRegionTimer T("discoverFileObjects", "discover file objects",
782                      TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
783   FileSymRefs.clear();
784   BC->getBinaryFunctions().clear();
785   BC->clearBinaryData();
786 
787   // For local symbols we want to keep track of associated FILE symbol name for
788   // disambiguation by combined name.
789   StringRef FileSymbolName;
790   bool SeenFileName = false;
791   struct SymbolRefHash {
792     size_t operator()(SymbolRef const &S) const {
793       return std::hash<decltype(DataRefImpl::p)>{}(S.getRawDataRefImpl().p);
794     }
795   };
796   std::unordered_map<SymbolRef, StringRef, SymbolRefHash> SymbolToFileName;
797   for (const ELFSymbolRef &Symbol : InputFile->symbols()) {
798     Expected<StringRef> NameOrError = Symbol.getName();
799     if (NameOrError && NameOrError->startswith("__asan_init")) {
800       errs() << "BOLT-ERROR: input file was compiled or linked with sanitizer "
801                 "support. Cannot optimize.\n";
802       exit(1);
803     }
804     if (NameOrError && NameOrError->startswith("__llvm_coverage_mapping")) {
805       errs() << "BOLT-ERROR: input file was compiled or linked with coverage "
806                 "support. Cannot optimize.\n";
807       exit(1);
808     }
809 
810     if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Undefined)
811       continue;
812 
813     if (cantFail(Symbol.getType()) == SymbolRef::ST_File) {
814       StringRef Name =
815           cantFail(std::move(NameOrError), "cannot get symbol name for file");
816       // Ignore Clang LTO artificial FILE symbol as it is not always generated,
817       // and this uncertainty is causing havoc in function name matching.
818       if (Name == "ld-temp.o")
819         continue;
820       FileSymbolName = Name;
821       SeenFileName = true;
822       continue;
823     }
824     if (!FileSymbolName.empty() &&
825         !(cantFail(Symbol.getFlags()) & SymbolRef::SF_Global))
826       SymbolToFileName[Symbol] = FileSymbolName;
827   }
828 
829   // Sort symbols in the file by value. Ignore symbols from non-allocatable
830   // sections.
831   auto isSymbolInMemory = [this](const SymbolRef &Sym) {
832     if (cantFail(Sym.getType()) == SymbolRef::ST_File)
833       return false;
834     if (cantFail(Sym.getFlags()) & SymbolRef::SF_Absolute)
835       return true;
836     if (cantFail(Sym.getFlags()) & SymbolRef::SF_Undefined)
837       return false;
838     BinarySection Section(*BC, *cantFail(Sym.getSection()));
839     return Section.isAllocatable();
840   };
841   std::vector<SymbolRef> SortedFileSymbols;
842   std::copy_if(InputFile->symbol_begin(), InputFile->symbol_end(),
843                std::back_inserter(SortedFileSymbols), isSymbolInMemory);
844   auto CompareSymbols = [this](const SymbolRef &A, const SymbolRef &B) {
845     // Marker symbols have the highest precedence, while
846     // SECTIONs have the lowest.
847     auto AddressA = cantFail(A.getAddress());
848     auto AddressB = cantFail(B.getAddress());
849     if (AddressA != AddressB)
850       return AddressA < AddressB;
851 
852     bool AMarker = BC->isMarker(A);
853     bool BMarker = BC->isMarker(B);
854     if (AMarker || BMarker) {
855       return AMarker && !BMarker;
856     }
857 
858     auto AType = cantFail(A.getType());
859     auto BType = cantFail(B.getType());
860     if (AType == SymbolRef::ST_Function && BType != SymbolRef::ST_Function)
861       return true;
862     if (BType == SymbolRef::ST_Debug && AType != SymbolRef::ST_Debug)
863       return true;
864 
865     return false;
866   };
867 
868   std::stable_sort(SortedFileSymbols.begin(), SortedFileSymbols.end(),
869                    CompareSymbols);
870 
871   auto LastSymbol = SortedFileSymbols.end() - 1;
872 
873   // For aarch64, the ABI defines mapping symbols so we identify data in the
874   // code section (see IHI0056B). $d identifies data contents.
875   // Compilers usually merge multiple data objects in a single $d-$x interval,
876   // but we need every data object to be marked with $d. Because of that we
877   // create a vector of MarkerSyms with all locations of data objects.
878 
879   struct MarkerSym {
880     uint64_t Address;
881     MarkerSymType Type;
882   };
883 
884   std::vector<MarkerSym> SortedMarkerSymbols;
885   auto addExtraDataMarkerPerSymbol =
886       [this](const std::vector<SymbolRef> &SortedFileSymbols,
887              std::vector<MarkerSym> &SortedMarkerSymbols) {
888         bool IsData = false;
889         uint64_t LastAddr = 0;
890         for (auto Sym = SortedFileSymbols.begin();
891              Sym < SortedFileSymbols.end(); ++Sym) {
892           uint64_t Address = cantFail(Sym->getAddress());
893           if (LastAddr == Address) // don't repeat markers
894             continue;
895 
896           MarkerSymType MarkerType = BC->getMarkerType(*Sym);
897           if (MarkerType != MarkerSymType::NONE) {
898             SortedMarkerSymbols.push_back(MarkerSym{Address, MarkerType});
899             LastAddr = Address;
900             IsData = MarkerType == MarkerSymType::DATA;
901             continue;
902           }
903 
904           if (IsData) {
905             SortedMarkerSymbols.push_back(
906                 MarkerSym{cantFail(Sym->getAddress()), MarkerSymType::DATA});
907             LastAddr = Address;
908           }
909         }
910       };
911 
912   if (BC->isAArch64()) {
913     addExtraDataMarkerPerSymbol(SortedFileSymbols, SortedMarkerSymbols);
914     LastSymbol = std::stable_partition(
915         SortedFileSymbols.begin(), SortedFileSymbols.end(),
916         [this](const SymbolRef &Symbol) { return !BC->isMarker(Symbol); });
917     --LastSymbol;
918   }
919 
920   BinaryFunction *PreviousFunction = nullptr;
921   unsigned AnonymousId = 0;
922 
923   const auto SortedSymbolsEnd = std::next(LastSymbol);
924   for (auto ISym = SortedFileSymbols.begin(); ISym != SortedSymbolsEnd;
925        ++ISym) {
926     const SymbolRef &Symbol = *ISym;
927     // Keep undefined symbols for pretty printing?
928     if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Undefined)
929       continue;
930 
931     const SymbolRef::Type SymbolType = cantFail(Symbol.getType());
932 
933     if (SymbolType == SymbolRef::ST_File)
934       continue;
935 
936     StringRef SymName = cantFail(Symbol.getName(), "cannot get symbol name");
937     uint64_t Address =
938         cantFail(Symbol.getAddress(), "cannot get symbol address");
939     if (Address == 0) {
940       if (opts::Verbosity >= 1 && SymbolType == SymbolRef::ST_Function)
941         errs() << "BOLT-WARNING: function with 0 address seen\n";
942       continue;
943     }
944 
945     // Ignore input hot markers
946     if (SymName == "__hot_start" || SymName == "__hot_end")
947       continue;
948 
949     FileSymRefs[Address] = Symbol;
950 
951     // Skip section symbols that will be registered by disassemblePLT().
952     if ((cantFail(Symbol.getType()) == SymbolRef::ST_Debug)) {
953       ErrorOr<BinarySection &> BSection = BC->getSectionForAddress(Address);
954       if (BSection && getPLTSectionInfo(BSection->getName()))
955         continue;
956     }
957 
958     /// It is possible we are seeing a globalized local. LLVM might treat it as
959     /// a local if it has a "private global" prefix, e.g. ".L". Thus we have to
960     /// change the prefix to enforce global scope of the symbol.
961     std::string Name = SymName.startswith(BC->AsmInfo->getPrivateGlobalPrefix())
962                            ? "PG" + std::string(SymName)
963                            : std::string(SymName);
964 
965     // Disambiguate all local symbols before adding to symbol table.
966     // Since we don't know if we will see a global with the same name,
967     // always modify the local name.
968     //
969     // NOTE: the naming convention for local symbols should match
970     //       the one we use for profile data.
971     std::string UniqueName;
972     std::string AlternativeName;
973     if (Name.empty()) {
974       UniqueName = "ANONYMOUS." + std::to_string(AnonymousId++);
975     } else if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Global) {
976       assert(!BC->getBinaryDataByName(Name) && "global name not unique");
977       UniqueName = Name;
978     } else {
979       // If we have a local file name, we should create 2 variants for the
980       // function name. The reason is that perf profile might have been
981       // collected on a binary that did not have the local file name (e.g. as
982       // a side effect of stripping debug info from the binary):
983       //
984       //   primary:     <function>/<id>
985       //   alternative: <function>/<file>/<id2>
986       //
987       // The <id> field is used for disambiguation of local symbols since there
988       // could be identical function names coming from identical file names
989       // (e.g. from different directories).
990       std::string AltPrefix;
991       auto SFI = SymbolToFileName.find(Symbol);
992       if (SymbolType == SymbolRef::ST_Function && SFI != SymbolToFileName.end())
993         AltPrefix = Name + "/" + std::string(SFI->second);
994 
995       UniqueName = NR.uniquify(Name);
996       if (!AltPrefix.empty())
997         AlternativeName = NR.uniquify(AltPrefix);
998     }
999 
1000     uint64_t SymbolSize = ELFSymbolRef(Symbol).getSize();
1001     uint64_t SymbolAlignment = Symbol.getAlignment();
1002     unsigned SymbolFlags = cantFail(Symbol.getFlags());
1003 
1004     auto registerName = [&](uint64_t FinalSize) {
1005       // Register names even if it's not a function, e.g. for an entry point.
1006       BC->registerNameAtAddress(UniqueName, Address, FinalSize, SymbolAlignment,
1007                                 SymbolFlags);
1008       if (!AlternativeName.empty())
1009         BC->registerNameAtAddress(AlternativeName, Address, FinalSize,
1010                                   SymbolAlignment, SymbolFlags);
1011     };
1012 
1013     section_iterator Section =
1014         cantFail(Symbol.getSection(), "cannot get symbol section");
1015     if (Section == InputFile->section_end()) {
1016       // Could be an absolute symbol. Could record for pretty printing.
1017       LLVM_DEBUG(if (opts::Verbosity > 1) {
1018         dbgs() << "BOLT-INFO: absolute sym " << UniqueName << "\n";
1019       });
1020       registerName(SymbolSize);
1021       continue;
1022     }
1023 
1024     LLVM_DEBUG(dbgs() << "BOLT-DEBUG: considering symbol " << UniqueName
1025                       << " for function\n");
1026 
1027     if (!Section->isText()) {
1028       assert(SymbolType != SymbolRef::ST_Function &&
1029              "unexpected function inside non-code section");
1030       LLVM_DEBUG(dbgs() << "BOLT-DEBUG: rejecting as symbol is not in code\n");
1031       registerName(SymbolSize);
1032       continue;
1033     }
1034 
1035     // Assembly functions could be ST_NONE with 0 size. Check that the
1036     // corresponding section is a code section and they are not inside any
1037     // other known function to consider them.
1038     //
1039     // Sometimes assembly functions are not marked as functions and neither are
1040     // their local labels. The only way to tell them apart is to look at
1041     // symbol scope - global vs local.
1042     if (PreviousFunction && SymbolType != SymbolRef::ST_Function) {
1043       if (PreviousFunction->containsAddress(Address)) {
1044         if (PreviousFunction->isSymbolValidInScope(Symbol, SymbolSize)) {
1045           LLVM_DEBUG(dbgs()
1046                      << "BOLT-DEBUG: symbol is a function local symbol\n");
1047         } else if (Address == PreviousFunction->getAddress() && !SymbolSize) {
1048           LLVM_DEBUG(dbgs() << "BOLT-DEBUG: ignoring symbol as a marker\n");
1049         } else if (opts::Verbosity > 1) {
1050           errs() << "BOLT-WARNING: symbol " << UniqueName
1051                  << " seen in the middle of function " << *PreviousFunction
1052                  << ". Could be a new entry.\n";
1053         }
1054         registerName(SymbolSize);
1055         continue;
1056       } else if (PreviousFunction->getSize() == 0 &&
1057                  PreviousFunction->isSymbolValidInScope(Symbol, SymbolSize)) {
1058         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: symbol is a function local symbol\n");
1059         registerName(SymbolSize);
1060         continue;
1061       }
1062     }
1063 
1064     if (PreviousFunction && PreviousFunction->containsAddress(Address) &&
1065         PreviousFunction->getAddress() != Address) {
1066       if (PreviousFunction->isSymbolValidInScope(Symbol, SymbolSize)) {
1067         if (opts::Verbosity >= 1)
1068           outs() << "BOLT-INFO: skipping possibly another entry for function "
1069                  << *PreviousFunction << " : " << UniqueName << '\n';
1070       } else {
1071         outs() << "BOLT-INFO: using " << UniqueName << " as another entry to "
1072                << "function " << *PreviousFunction << '\n';
1073 
1074         registerName(0);
1075 
1076         PreviousFunction->addEntryPointAtOffset(Address -
1077                                                 PreviousFunction->getAddress());
1078 
1079         // Remove the symbol from FileSymRefs so that we can skip it from
1080         // in the future.
1081         auto SI = FileSymRefs.find(Address);
1082         assert(SI != FileSymRefs.end() && "symbol expected to be present");
1083         assert(SI->second == Symbol && "wrong symbol found");
1084         FileSymRefs.erase(SI);
1085       }
1086       registerName(SymbolSize);
1087       continue;
1088     }
1089 
1090     // Checkout for conflicts with function data from FDEs.
1091     bool IsSimple = true;
1092     auto FDEI = CFIRdWrt->getFDEs().lower_bound(Address);
1093     if (FDEI != CFIRdWrt->getFDEs().end()) {
1094       const dwarf::FDE &FDE = *FDEI->second;
1095       if (FDEI->first != Address) {
1096         // There's no matching starting address in FDE. Make sure the previous
1097         // FDE does not contain this address.
1098         if (FDEI != CFIRdWrt->getFDEs().begin()) {
1099           --FDEI;
1100           const dwarf::FDE &PrevFDE = *FDEI->second;
1101           uint64_t PrevStart = PrevFDE.getInitialLocation();
1102           uint64_t PrevLength = PrevFDE.getAddressRange();
1103           if (Address > PrevStart && Address < PrevStart + PrevLength) {
1104             errs() << "BOLT-ERROR: function " << UniqueName
1105                    << " is in conflict with FDE ["
1106                    << Twine::utohexstr(PrevStart) << ", "
1107                    << Twine::utohexstr(PrevStart + PrevLength)
1108                    << "). Skipping.\n";
1109             IsSimple = false;
1110           }
1111         }
1112       } else if (FDE.getAddressRange() != SymbolSize) {
1113         if (SymbolSize) {
1114           // Function addresses match but sizes differ.
1115           errs() << "BOLT-WARNING: sizes differ for function " << UniqueName
1116                  << ". FDE : " << FDE.getAddressRange()
1117                  << "; symbol table : " << SymbolSize << ". Using max size.\n";
1118         }
1119         SymbolSize = std::max(SymbolSize, FDE.getAddressRange());
1120         if (BC->getBinaryDataAtAddress(Address)) {
1121           BC->setBinaryDataSize(Address, SymbolSize);
1122         } else {
1123           LLVM_DEBUG(dbgs() << "BOLT-DEBUG: No BD @ 0x"
1124                             << Twine::utohexstr(Address) << "\n");
1125         }
1126       }
1127     }
1128 
1129     BinaryFunction *BF = nullptr;
1130     // Since function may not have yet obtained its real size, do a search
1131     // using the list of registered functions instead of calling
1132     // getBinaryFunctionAtAddress().
1133     auto BFI = BC->getBinaryFunctions().find(Address);
1134     if (BFI != BC->getBinaryFunctions().end()) {
1135       BF = &BFI->second;
1136       // Duplicate the function name. Make sure everything matches before we add
1137       // an alternative name.
1138       if (SymbolSize != BF->getSize()) {
1139         if (opts::Verbosity >= 1) {
1140           if (SymbolSize && BF->getSize())
1141             errs() << "BOLT-WARNING: size mismatch for duplicate entries "
1142                    << *BF << " and " << UniqueName << '\n';
1143           outs() << "BOLT-INFO: adjusting size of function " << *BF << " old "
1144                  << BF->getSize() << " new " << SymbolSize << "\n";
1145         }
1146         BF->setSize(std::max(SymbolSize, BF->getSize()));
1147         BC->setBinaryDataSize(Address, BF->getSize());
1148       }
1149       BF->addAlternativeName(UniqueName);
1150     } else {
1151       ErrorOr<BinarySection &> Section = BC->getSectionForAddress(Address);
1152       // Skip symbols from invalid sections
1153       if (!Section) {
1154         errs() << "BOLT-WARNING: " << UniqueName << " (0x"
1155                << Twine::utohexstr(Address) << ") does not have any section\n";
1156         continue;
1157       }
1158       assert(Section && "section for functions must be registered");
1159 
1160       // Skip symbols from zero-sized sections.
1161       if (!Section->getSize())
1162         continue;
1163 
1164       BF = BC->createBinaryFunction(UniqueName, *Section, Address, SymbolSize);
1165       if (!IsSimple)
1166         BF->setSimple(false);
1167     }
1168     if (!AlternativeName.empty())
1169       BF->addAlternativeName(AlternativeName);
1170 
1171     registerName(SymbolSize);
1172     PreviousFunction = BF;
1173   }
1174 
1175   // Read dynamic relocation first as their presence affects the way we process
1176   // static relocations. E.g. we will ignore a static relocation at an address
1177   // that is a subject to dynamic relocation processing.
1178   processDynamicRelocations();
1179 
1180   // Process PLT section.
1181   disassemblePLT();
1182 
1183   // See if we missed any functions marked by FDE.
1184   for (const auto &FDEI : CFIRdWrt->getFDEs()) {
1185     const uint64_t Address = FDEI.first;
1186     const dwarf::FDE *FDE = FDEI.second;
1187     const BinaryFunction *BF = BC->getBinaryFunctionAtAddress(Address);
1188     if (BF)
1189       continue;
1190 
1191     BF = BC->getBinaryFunctionContainingAddress(Address);
1192     if (BF) {
1193       errs() << "BOLT-WARNING: FDE [0x" << Twine::utohexstr(Address) << ", 0x"
1194              << Twine::utohexstr(Address + FDE->getAddressRange())
1195              << ") conflicts with function " << *BF << '\n';
1196       continue;
1197     }
1198 
1199     if (opts::Verbosity >= 1)
1200       errs() << "BOLT-WARNING: FDE [0x" << Twine::utohexstr(Address) << ", 0x"
1201              << Twine::utohexstr(Address + FDE->getAddressRange())
1202              << ") has no corresponding symbol table entry\n";
1203 
1204     ErrorOr<BinarySection &> Section = BC->getSectionForAddress(Address);
1205     assert(Section && "cannot get section for address from FDE");
1206     std::string FunctionName =
1207         "__BOLT_FDE_FUNCat" + Twine::utohexstr(Address).str();
1208     BC->createBinaryFunction(FunctionName, *Section, Address,
1209                              FDE->getAddressRange());
1210   }
1211 
1212   BC->setHasSymbolsWithFileName(SeenFileName);
1213 
1214   // Now that all the functions were created - adjust their boundaries.
1215   adjustFunctionBoundaries();
1216 
1217   // Annotate functions with code/data markers in AArch64
1218   for (auto ISym = SortedMarkerSymbols.begin();
1219        ISym != SortedMarkerSymbols.end(); ++ISym) {
1220 
1221     auto *BF =
1222         BC->getBinaryFunctionContainingAddress(ISym->Address, true, true);
1223 
1224     if (!BF) {
1225       // Stray marker
1226       continue;
1227     }
1228     const auto EntryOffset = ISym->Address - BF->getAddress();
1229     if (ISym->Type == MarkerSymType::CODE) {
1230       BF->markCodeAtOffset(EntryOffset);
1231       continue;
1232     }
1233     if (ISym->Type == MarkerSymType::DATA) {
1234       BF->markDataAtOffset(EntryOffset);
1235       BC->AddressToConstantIslandMap[ISym->Address] = BF;
1236       continue;
1237     }
1238     llvm_unreachable("Unknown marker");
1239   }
1240 
1241   if (opts::LinuxKernelMode) {
1242     // Read all special linux kernel sections and their relocations
1243     processLKSections();
1244   } else {
1245     // Read all relocations now that we have binary functions mapped.
1246     processRelocations();
1247   }
1248 }
1249 
1250 void RewriteInstance::createPLTBinaryFunction(uint64_t TargetAddress,
1251                                               uint64_t EntryAddress,
1252                                               uint64_t EntrySize) {
1253   if (!TargetAddress)
1254     return;
1255 
1256   auto setPLTSymbol = [&](BinaryFunction *BF, StringRef Name) {
1257     const unsigned PtrSize = BC->AsmInfo->getCodePointerSize();
1258     MCSymbol *TargetSymbol = BC->registerNameAtAddress(
1259         Name.str() + "@GOT", TargetAddress, PtrSize, PtrSize);
1260     BF->setPLTSymbol(TargetSymbol);
1261   };
1262 
1263   BinaryFunction *BF = BC->getBinaryFunctionAtAddress(EntryAddress);
1264   if (BF && BC->isAArch64()) {
1265     // Handle IFUNC trampoline
1266     setPLTSymbol(BF, BF->getOneName());
1267     return;
1268   }
1269 
1270   const Relocation *Rel = BC->getDynamicRelocationAt(TargetAddress);
1271   if (!Rel || !Rel->Symbol)
1272     return;
1273 
1274   ErrorOr<BinarySection &> Section = BC->getSectionForAddress(EntryAddress);
1275   assert(Section && "cannot get section for address");
1276   BF = BC->createBinaryFunction(Rel->Symbol->getName().str() + "@PLT", *Section,
1277                                 EntryAddress, 0, EntrySize,
1278                                 Section->getAlignment());
1279   setPLTSymbol(BF, Rel->Symbol->getName());
1280 }
1281 
1282 void RewriteInstance::disassemblePLTSectionAArch64(BinarySection &Section) {
1283   const uint64_t SectionAddress = Section.getAddress();
1284   const uint64_t SectionSize = Section.getSize();
1285   StringRef PLTContents = Section.getContents();
1286   ArrayRef<uint8_t> PLTData(
1287       reinterpret_cast<const uint8_t *>(PLTContents.data()), SectionSize);
1288 
1289   auto disassembleInstruction = [&](uint64_t InstrOffset, MCInst &Instruction,
1290                                     uint64_t &InstrSize) {
1291     const uint64_t InstrAddr = SectionAddress + InstrOffset;
1292     if (!BC->DisAsm->getInstruction(Instruction, InstrSize,
1293                                     PLTData.slice(InstrOffset), InstrAddr,
1294                                     nulls())) {
1295       errs() << "BOLT-ERROR: unable to disassemble instruction in PLT section "
1296              << Section.getName() << " at offset 0x"
1297              << Twine::utohexstr(InstrOffset) << '\n';
1298       exit(1);
1299     }
1300   };
1301 
1302   uint64_t InstrOffset = 0;
1303   // Locate new plt entry
1304   while (InstrOffset < SectionSize) {
1305     InstructionListType Instructions;
1306     MCInst Instruction;
1307     uint64_t EntryOffset = InstrOffset;
1308     uint64_t EntrySize = 0;
1309     uint64_t InstrSize;
1310     // Loop through entry instructions
1311     while (InstrOffset < SectionSize) {
1312       disassembleInstruction(InstrOffset, Instruction, InstrSize);
1313       EntrySize += InstrSize;
1314       if (!BC->MIB->isIndirectBranch(Instruction)) {
1315         Instructions.emplace_back(Instruction);
1316         InstrOffset += InstrSize;
1317         continue;
1318       }
1319 
1320       const uint64_t EntryAddress = SectionAddress + EntryOffset;
1321       const uint64_t TargetAddress = BC->MIB->analyzePLTEntry(
1322           Instruction, Instructions.begin(), Instructions.end(), EntryAddress);
1323 
1324       createPLTBinaryFunction(TargetAddress, EntryAddress, EntrySize);
1325       break;
1326     }
1327 
1328     // Branch instruction
1329     InstrOffset += InstrSize;
1330 
1331     // Skip nops if any
1332     while (InstrOffset < SectionSize) {
1333       disassembleInstruction(InstrOffset, Instruction, InstrSize);
1334       if (!BC->MIB->isNoop(Instruction))
1335         break;
1336 
1337       InstrOffset += InstrSize;
1338     }
1339   }
1340 }
1341 
1342 void RewriteInstance::disassemblePLTSectionX86(BinarySection &Section,
1343                                                uint64_t EntrySize) {
1344   const uint64_t SectionAddress = Section.getAddress();
1345   const uint64_t SectionSize = Section.getSize();
1346   StringRef PLTContents = Section.getContents();
1347   ArrayRef<uint8_t> PLTData(
1348       reinterpret_cast<const uint8_t *>(PLTContents.data()), SectionSize);
1349 
1350   auto disassembleInstruction = [&](uint64_t InstrOffset, MCInst &Instruction,
1351                                     uint64_t &InstrSize) {
1352     const uint64_t InstrAddr = SectionAddress + InstrOffset;
1353     if (!BC->DisAsm->getInstruction(Instruction, InstrSize,
1354                                     PLTData.slice(InstrOffset), InstrAddr,
1355                                     nulls())) {
1356       errs() << "BOLT-ERROR: unable to disassemble instruction in PLT section "
1357              << Section.getName() << " at offset 0x"
1358              << Twine::utohexstr(InstrOffset) << '\n';
1359       exit(1);
1360     }
1361   };
1362 
1363   for (uint64_t EntryOffset = 0; EntryOffset + EntrySize <= SectionSize;
1364        EntryOffset += EntrySize) {
1365     MCInst Instruction;
1366     uint64_t InstrSize, InstrOffset = EntryOffset;
1367     while (InstrOffset < EntryOffset + EntrySize) {
1368       disassembleInstruction(InstrOffset, Instruction, InstrSize);
1369       // Check if the entry size needs adjustment.
1370       if (EntryOffset == 0 && BC->MIB->isTerminateBranch(Instruction) &&
1371           EntrySize == 8)
1372         EntrySize = 16;
1373 
1374       if (BC->MIB->isIndirectBranch(Instruction))
1375         break;
1376 
1377       InstrOffset += InstrSize;
1378     }
1379 
1380     if (InstrOffset + InstrSize > EntryOffset + EntrySize)
1381       continue;
1382 
1383     uint64_t TargetAddress;
1384     if (!BC->MIB->evaluateMemOperandTarget(Instruction, TargetAddress,
1385                                            SectionAddress + InstrOffset,
1386                                            InstrSize)) {
1387       errs() << "BOLT-ERROR: error evaluating PLT instruction at offset 0x"
1388              << Twine::utohexstr(SectionAddress + InstrOffset) << '\n';
1389       exit(1);
1390     }
1391 
1392     createPLTBinaryFunction(TargetAddress, SectionAddress + EntryOffset,
1393                             EntrySize);
1394   }
1395 }
1396 
1397 void RewriteInstance::disassemblePLT() {
1398   auto analyzeOnePLTSection = [&](BinarySection &Section, uint64_t EntrySize) {
1399     if (BC->isAArch64())
1400       return disassemblePLTSectionAArch64(Section);
1401     return disassemblePLTSectionX86(Section, EntrySize);
1402   };
1403 
1404   for (BinarySection &Section : BC->allocatableSections()) {
1405     const PLTSectionInfo *PLTSI = getPLTSectionInfo(Section.getName());
1406     if (!PLTSI)
1407       continue;
1408 
1409     analyzeOnePLTSection(Section, PLTSI->EntrySize);
1410     // If we did not register any function at the start of the section,
1411     // then it must be a general PLT entry. Add a function at the location.
1412     if (BC->getBinaryFunctions().find(Section.getAddress()) ==
1413         BC->getBinaryFunctions().end()) {
1414       BinaryFunction *BF = BC->createBinaryFunction(
1415           "__BOLT_PSEUDO_" + Section.getName().str(), Section,
1416           Section.getAddress(), 0, PLTSI->EntrySize, Section.getAlignment());
1417       BF->setPseudo(true);
1418     }
1419   }
1420 }
1421 
1422 void RewriteInstance::adjustFunctionBoundaries() {
1423   for (auto BFI = BC->getBinaryFunctions().begin(),
1424             BFE = BC->getBinaryFunctions().end();
1425        BFI != BFE; ++BFI) {
1426     BinaryFunction &Function = BFI->second;
1427     const BinaryFunction *NextFunction = nullptr;
1428     if (std::next(BFI) != BFE)
1429       NextFunction = &std::next(BFI)->second;
1430 
1431     // Check if it's a fragment of a function.
1432     Optional<StringRef> FragName =
1433         Function.hasRestoredNameRegex(".*\\.cold(\\.[0-9]+)?");
1434     if (FragName) {
1435       static bool PrintedWarning = false;
1436       if (BC->HasRelocations && !PrintedWarning) {
1437         errs() << "BOLT-WARNING: split function detected on input : "
1438                << *FragName << ". The support is limited in relocation mode.\n";
1439         PrintedWarning = true;
1440       }
1441       Function.IsFragment = true;
1442     }
1443 
1444     // Check if there's a symbol or a function with a larger address in the
1445     // same section. If there is - it determines the maximum size for the
1446     // current function. Otherwise, it is the size of a containing section
1447     // the defines it.
1448     //
1449     // NOTE: ignore some symbols that could be tolerated inside the body
1450     //       of a function.
1451     auto NextSymRefI = FileSymRefs.upper_bound(Function.getAddress());
1452     while (NextSymRefI != FileSymRefs.end()) {
1453       SymbolRef &Symbol = NextSymRefI->second;
1454       const uint64_t SymbolAddress = NextSymRefI->first;
1455       const uint64_t SymbolSize = ELFSymbolRef(Symbol).getSize();
1456 
1457       if (NextFunction && SymbolAddress >= NextFunction->getAddress())
1458         break;
1459 
1460       if (!Function.isSymbolValidInScope(Symbol, SymbolSize))
1461         break;
1462 
1463       // This is potentially another entry point into the function.
1464       uint64_t EntryOffset = NextSymRefI->first - Function.getAddress();
1465       LLVM_DEBUG(dbgs() << "BOLT-DEBUG: adding entry point to function "
1466                         << Function << " at offset 0x"
1467                         << Twine::utohexstr(EntryOffset) << '\n');
1468       Function.addEntryPointAtOffset(EntryOffset);
1469 
1470       ++NextSymRefI;
1471     }
1472 
1473     // Function runs at most till the end of the containing section.
1474     uint64_t NextObjectAddress = Function.getOriginSection()->getEndAddress();
1475     // Or till the next object marked by a symbol.
1476     if (NextSymRefI != FileSymRefs.end())
1477       NextObjectAddress = std::min(NextSymRefI->first, NextObjectAddress);
1478 
1479     // Or till the next function not marked by a symbol.
1480     if (NextFunction)
1481       NextObjectAddress =
1482           std::min(NextFunction->getAddress(), NextObjectAddress);
1483 
1484     const uint64_t MaxSize = NextObjectAddress - Function.getAddress();
1485     if (MaxSize < Function.getSize()) {
1486       errs() << "BOLT-ERROR: symbol seen in the middle of the function "
1487              << Function << ". Skipping.\n";
1488       Function.setSimple(false);
1489       Function.setMaxSize(Function.getSize());
1490       continue;
1491     }
1492     Function.setMaxSize(MaxSize);
1493     if (!Function.getSize() && Function.isSimple()) {
1494       // Some assembly functions have their size set to 0, use the max
1495       // size as their real size.
1496       if (opts::Verbosity >= 1)
1497         outs() << "BOLT-INFO: setting size of function " << Function << " to "
1498                << Function.getMaxSize() << " (was 0)\n";
1499       Function.setSize(Function.getMaxSize());
1500     }
1501   }
1502 }
1503 
1504 void RewriteInstance::relocateEHFrameSection() {
1505   assert(EHFrameSection && "non-empty .eh_frame section expected");
1506 
1507   DWARFDataExtractor DE(EHFrameSection->getContents(),
1508                         BC->AsmInfo->isLittleEndian(),
1509                         BC->AsmInfo->getCodePointerSize());
1510   auto createReloc = [&](uint64_t Value, uint64_t Offset, uint64_t DwarfType) {
1511     if (DwarfType == dwarf::DW_EH_PE_omit)
1512       return;
1513 
1514     // Only fix references that are relative to other locations.
1515     if (!(DwarfType & dwarf::DW_EH_PE_pcrel) &&
1516         !(DwarfType & dwarf::DW_EH_PE_textrel) &&
1517         !(DwarfType & dwarf::DW_EH_PE_funcrel) &&
1518         !(DwarfType & dwarf::DW_EH_PE_datarel))
1519       return;
1520 
1521     if (!(DwarfType & dwarf::DW_EH_PE_sdata4))
1522       return;
1523 
1524     uint64_t RelType;
1525     switch (DwarfType & 0x0f) {
1526     default:
1527       llvm_unreachable("unsupported DWARF encoding type");
1528     case dwarf::DW_EH_PE_sdata4:
1529     case dwarf::DW_EH_PE_udata4:
1530       RelType = Relocation::getPC32();
1531       Offset -= 4;
1532       break;
1533     case dwarf::DW_EH_PE_sdata8:
1534     case dwarf::DW_EH_PE_udata8:
1535       RelType = Relocation::getPC64();
1536       Offset -= 8;
1537       break;
1538     }
1539 
1540     // Create a relocation against an absolute value since the goal is to
1541     // preserve the contents of the section independent of the new values
1542     // of referenced symbols.
1543     EHFrameSection->addRelocation(Offset, nullptr, RelType, Value);
1544   };
1545 
1546   Error E = EHFrameParser::parse(DE, EHFrameSection->getAddress(), createReloc);
1547   check_error(std::move(E), "failed to patch EH frame");
1548 }
1549 
1550 ArrayRef<uint8_t> RewriteInstance::getLSDAData() {
1551   return ArrayRef<uint8_t>(LSDASection->getData(),
1552                            LSDASection->getContents().size());
1553 }
1554 
1555 uint64_t RewriteInstance::getLSDAAddress() { return LSDASection->getAddress(); }
1556 
1557 Error RewriteInstance::readSpecialSections() {
1558   NamedRegionTimer T("readSpecialSections", "read special sections",
1559                      TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
1560 
1561   bool HasTextRelocations = false;
1562   bool HasDebugInfo = false;
1563 
1564   // Process special sections.
1565   for (const SectionRef &Section : InputFile->sections()) {
1566     Expected<StringRef> SectionNameOrErr = Section.getName();
1567     check_error(SectionNameOrErr.takeError(), "cannot get section name");
1568     StringRef SectionName = *SectionNameOrErr;
1569 
1570     // Only register sections with names.
1571     if (!SectionName.empty()) {
1572       if (Error E = Section.getContents().takeError())
1573         return E;
1574       BC->registerSection(Section);
1575       LLVM_DEBUG(
1576           dbgs() << "BOLT-DEBUG: registering section " << SectionName << " @ 0x"
1577                  << Twine::utohexstr(Section.getAddress()) << ":0x"
1578                  << Twine::utohexstr(Section.getAddress() + Section.getSize())
1579                  << "\n");
1580       if (isDebugSection(SectionName))
1581         HasDebugInfo = true;
1582       if (isKSymtabSection(SectionName))
1583         opts::LinuxKernelMode = true;
1584     }
1585   }
1586 
1587   if (HasDebugInfo && !opts::UpdateDebugSections && !opts::AggregateOnly) {
1588     errs() << "BOLT-WARNING: debug info will be stripped from the binary. "
1589               "Use -update-debug-sections to keep it.\n";
1590   }
1591 
1592   HasTextRelocations = (bool)BC->getUniqueSectionByName(".rela.text");
1593   LSDASection = BC->getUniqueSectionByName(".gcc_except_table");
1594   EHFrameSection = BC->getUniqueSectionByName(".eh_frame");
1595   GOTPLTSection = BC->getUniqueSectionByName(".got.plt");
1596   RelaPLTSection = BC->getUniqueSectionByName(".rela.plt");
1597   RelaDynSection = BC->getUniqueSectionByName(".rela.dyn");
1598   BuildIDSection = BC->getUniqueSectionByName(".note.gnu.build-id");
1599   SDTSection = BC->getUniqueSectionByName(".note.stapsdt");
1600   PseudoProbeDescSection = BC->getUniqueSectionByName(".pseudo_probe_desc");
1601   PseudoProbeSection = BC->getUniqueSectionByName(".pseudo_probe");
1602 
1603   if (ErrorOr<BinarySection &> BATSec =
1604           BC->getUniqueSectionByName(BoltAddressTranslation::SECTION_NAME)) {
1605     // Do not read BAT when plotting a heatmap
1606     if (!opts::HeatmapMode) {
1607       if (std::error_code EC = BAT->parse(BATSec->getContents())) {
1608         errs() << "BOLT-ERROR: failed to parse BOLT address translation "
1609                   "table.\n";
1610         exit(1);
1611       }
1612     }
1613   }
1614 
1615   if (opts::PrintSections) {
1616     outs() << "BOLT-INFO: Sections from original binary:\n";
1617     BC->printSections(outs());
1618   }
1619 
1620   if (opts::RelocationMode == cl::BOU_TRUE && !HasTextRelocations) {
1621     errs() << "BOLT-ERROR: relocations against code are missing from the input "
1622               "file. Cannot proceed in relocations mode (-relocs).\n";
1623     exit(1);
1624   }
1625 
1626   BC->HasRelocations =
1627       HasTextRelocations && (opts::RelocationMode != cl::BOU_FALSE);
1628 
1629   // Force non-relocation mode for heatmap generation
1630   if (opts::HeatmapMode)
1631     BC->HasRelocations = false;
1632 
1633   if (BC->HasRelocations)
1634     outs() << "BOLT-INFO: enabling " << (opts::StrictMode ? "strict " : "")
1635            << "relocation mode\n";
1636 
1637   // Read EH frame for function boundaries info.
1638   Expected<const DWARFDebugFrame *> EHFrameOrError = BC->DwCtx->getEHFrame();
1639   if (!EHFrameOrError)
1640     report_error("expected valid eh_frame section", EHFrameOrError.takeError());
1641   CFIRdWrt.reset(new CFIReaderWriter(*EHFrameOrError.get()));
1642 
1643   // Parse build-id
1644   parseBuildID();
1645   if (Optional<std::string> FileBuildID = getPrintableBuildID())
1646     BC->setFileBuildID(*FileBuildID);
1647 
1648   parseSDTNotes();
1649 
1650   // Read .dynamic/PT_DYNAMIC.
1651   return readELFDynamic();
1652 }
1653 
1654 void RewriteInstance::adjustCommandLineOptions() {
1655   if (BC->isAArch64() && !BC->HasRelocations)
1656     errs() << "BOLT-WARNING: non-relocation mode for AArch64 is not fully "
1657               "supported\n";
1658 
1659   if (RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary())
1660     RtLibrary->adjustCommandLineOptions(*BC);
1661 
1662   if (opts::AlignMacroOpFusion != MFT_NONE && !BC->isX86()) {
1663     outs() << "BOLT-INFO: disabling -align-macro-fusion on non-x86 platform\n";
1664     opts::AlignMacroOpFusion = MFT_NONE;
1665   }
1666 
1667   if (BC->isX86() && BC->MAB->allowAutoPadding()) {
1668     if (!BC->HasRelocations) {
1669       errs() << "BOLT-ERROR: cannot apply mitigations for Intel JCC erratum in "
1670                 "non-relocation mode\n";
1671       exit(1);
1672     }
1673     outs() << "BOLT-WARNING: using mitigation for Intel JCC erratum, layout "
1674               "may take several minutes\n";
1675     opts::AlignMacroOpFusion = MFT_NONE;
1676   }
1677 
1678   if (opts::AlignMacroOpFusion != MFT_NONE && !BC->HasRelocations) {
1679     outs() << "BOLT-INFO: disabling -align-macro-fusion in non-relocation "
1680               "mode\n";
1681     opts::AlignMacroOpFusion = MFT_NONE;
1682   }
1683 
1684   if (opts::SplitEH && !BC->HasRelocations) {
1685     errs() << "BOLT-WARNING: disabling -split-eh in non-relocation mode\n";
1686     opts::SplitEH = false;
1687   }
1688 
1689   if (opts::SplitEH && !BC->HasFixedLoadAddress) {
1690     errs() << "BOLT-WARNING: disabling -split-eh for shared object\n";
1691     opts::SplitEH = false;
1692   }
1693 
1694   if (opts::StrictMode && !BC->HasRelocations) {
1695     errs() << "BOLT-WARNING: disabling strict mode (-strict) in non-relocation "
1696               "mode\n";
1697     opts::StrictMode = false;
1698   }
1699 
1700   if (BC->HasRelocations && opts::AggregateOnly &&
1701       !opts::StrictMode.getNumOccurrences()) {
1702     outs() << "BOLT-INFO: enabling strict relocation mode for aggregation "
1703               "purposes\n";
1704     opts::StrictMode = true;
1705   }
1706 
1707   if (BC->isX86() && BC->HasRelocations &&
1708       opts::AlignMacroOpFusion == MFT_HOT && !ProfileReader) {
1709     outs() << "BOLT-INFO: enabling -align-macro-fusion=all since no profile "
1710               "was specified\n";
1711     opts::AlignMacroOpFusion = MFT_ALL;
1712   }
1713 
1714   if (!BC->HasRelocations &&
1715       opts::ReorderFunctions != ReorderFunctions::RT_NONE) {
1716     errs() << "BOLT-ERROR: function reordering only works when "
1717            << "relocations are enabled\n";
1718     exit(1);
1719   }
1720 
1721   if (opts::ReorderFunctions != ReorderFunctions::RT_NONE &&
1722       !opts::HotText.getNumOccurrences()) {
1723     opts::HotText = true;
1724   } else if (opts::HotText && !BC->HasRelocations) {
1725     errs() << "BOLT-WARNING: hot text is disabled in non-relocation mode\n";
1726     opts::HotText = false;
1727   }
1728 
1729   if (opts::HotText && opts::HotTextMoveSections.getNumOccurrences() == 0) {
1730     opts::HotTextMoveSections.addValue(".stub");
1731     opts::HotTextMoveSections.addValue(".mover");
1732     opts::HotTextMoveSections.addValue(".never_hugify");
1733   }
1734 
1735   if (opts::UseOldText && !BC->OldTextSectionAddress) {
1736     errs() << "BOLT-WARNING: cannot use old .text as the section was not found"
1737               "\n";
1738     opts::UseOldText = false;
1739   }
1740   if (opts::UseOldText && !BC->HasRelocations) {
1741     errs() << "BOLT-WARNING: cannot use old .text in non-relocation mode\n";
1742     opts::UseOldText = false;
1743   }
1744 
1745   if (!opts::AlignText.getNumOccurrences())
1746     opts::AlignText = BC->PageAlign;
1747 
1748   if (opts::AlignText < opts::AlignFunctions)
1749     opts::AlignText = (unsigned)opts::AlignFunctions;
1750 
1751   if (BC->isX86() && opts::Lite.getNumOccurrences() == 0 && !opts::StrictMode &&
1752       !opts::UseOldText)
1753     opts::Lite = true;
1754 
1755   if (opts::Lite && opts::UseOldText) {
1756     errs() << "BOLT-WARNING: cannot combine -lite with -use-old-text. "
1757               "Disabling -use-old-text.\n";
1758     opts::UseOldText = false;
1759   }
1760 
1761   if (opts::Lite && opts::StrictMode) {
1762     errs() << "BOLT-ERROR: -strict and -lite cannot be used at the same time\n";
1763     exit(1);
1764   }
1765 
1766   if (opts::Lite)
1767     outs() << "BOLT-INFO: enabling lite mode\n";
1768 
1769   if (!opts::SaveProfile.empty() && BAT->enabledFor(InputFile)) {
1770     errs() << "BOLT-ERROR: unable to save profile in YAML format for input "
1771               "file processed by BOLT. Please remove -w option and use branch "
1772               "profile.\n";
1773     exit(1);
1774   }
1775 }
1776 
1777 namespace {
1778 template <typename ELFT>
1779 int64_t getRelocationAddend(const ELFObjectFile<ELFT> *Obj,
1780                             const RelocationRef &RelRef) {
1781   using ELFShdrTy = typename ELFT::Shdr;
1782   using Elf_Rela = typename ELFT::Rela;
1783   int64_t Addend = 0;
1784   const ELFFile<ELFT> &EF = Obj->getELFFile();
1785   DataRefImpl Rel = RelRef.getRawDataRefImpl();
1786   const ELFShdrTy *RelocationSection = cantFail(EF.getSection(Rel.d.a));
1787   switch (RelocationSection->sh_type) {
1788   default:
1789     llvm_unreachable("unexpected relocation section type");
1790   case ELF::SHT_REL:
1791     break;
1792   case ELF::SHT_RELA: {
1793     const Elf_Rela *RelA = Obj->getRela(Rel);
1794     Addend = RelA->r_addend;
1795     break;
1796   }
1797   }
1798 
1799   return Addend;
1800 }
1801 
1802 int64_t getRelocationAddend(const ELFObjectFileBase *Obj,
1803                             const RelocationRef &Rel) {
1804   if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
1805     return getRelocationAddend(ELF32LE, Rel);
1806   if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
1807     return getRelocationAddend(ELF64LE, Rel);
1808   if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
1809     return getRelocationAddend(ELF32BE, Rel);
1810   auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
1811   return getRelocationAddend(ELF64BE, Rel);
1812 }
1813 
1814 template <typename ELFT>
1815 uint32_t getRelocationSymbol(const ELFObjectFile<ELFT> *Obj,
1816                              const RelocationRef &RelRef) {
1817   using ELFShdrTy = typename ELFT::Shdr;
1818   uint32_t Symbol = 0;
1819   const ELFFile<ELFT> &EF = Obj->getELFFile();
1820   DataRefImpl Rel = RelRef.getRawDataRefImpl();
1821   const ELFShdrTy *RelocationSection = cantFail(EF.getSection(Rel.d.a));
1822   switch (RelocationSection->sh_type) {
1823   default:
1824     llvm_unreachable("unexpected relocation section type");
1825   case ELF::SHT_REL:
1826     Symbol = Obj->getRel(Rel)->getSymbol(EF.isMips64EL());
1827     break;
1828   case ELF::SHT_RELA:
1829     Symbol = Obj->getRela(Rel)->getSymbol(EF.isMips64EL());
1830     break;
1831   }
1832 
1833   return Symbol;
1834 }
1835 
1836 uint32_t getRelocationSymbol(const ELFObjectFileBase *Obj,
1837                              const RelocationRef &Rel) {
1838   if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
1839     return getRelocationSymbol(ELF32LE, Rel);
1840   if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
1841     return getRelocationSymbol(ELF64LE, Rel);
1842   if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
1843     return getRelocationSymbol(ELF32BE, Rel);
1844   auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
1845   return getRelocationSymbol(ELF64BE, Rel);
1846 }
1847 } // anonymous namespace
1848 
1849 bool RewriteInstance::analyzeRelocation(
1850     const RelocationRef &Rel, uint64_t RType, std::string &SymbolName,
1851     bool &IsSectionRelocation, uint64_t &SymbolAddress, int64_t &Addend,
1852     uint64_t &ExtractedValue, bool &Skip) const {
1853   Skip = false;
1854   if (!Relocation::isSupported(RType))
1855     return false;
1856 
1857   const bool IsAArch64 = BC->isAArch64();
1858 
1859   const size_t RelSize = Relocation::getSizeForType(RType);
1860 
1861   ErrorOr<uint64_t> Value =
1862       BC->getUnsignedValueAtAddress(Rel.getOffset(), RelSize);
1863   assert(Value && "failed to extract relocated value");
1864   if ((Skip = Relocation::skipRelocationProcess(RType, *Value)))
1865     return true;
1866 
1867   ExtractedValue = Relocation::extractValue(RType, *Value, Rel.getOffset());
1868   Addend = getRelocationAddend(InputFile, Rel);
1869 
1870   const bool IsPCRelative = Relocation::isPCRelative(RType);
1871   const uint64_t PCRelOffset = IsPCRelative && !IsAArch64 ? Rel.getOffset() : 0;
1872   bool SkipVerification = false;
1873   auto SymbolIter = Rel.getSymbol();
1874   if (SymbolIter == InputFile->symbol_end()) {
1875     SymbolAddress = ExtractedValue - Addend + PCRelOffset;
1876     MCSymbol *RelSymbol =
1877         BC->getOrCreateGlobalSymbol(SymbolAddress, "RELSYMat");
1878     SymbolName = std::string(RelSymbol->getName());
1879     IsSectionRelocation = false;
1880   } else {
1881     const SymbolRef &Symbol = *SymbolIter;
1882     SymbolName = std::string(cantFail(Symbol.getName()));
1883     SymbolAddress = cantFail(Symbol.getAddress());
1884     SkipVerification = (cantFail(Symbol.getType()) == SymbolRef::ST_Other);
1885     // Section symbols are marked as ST_Debug.
1886     IsSectionRelocation = (cantFail(Symbol.getType()) == SymbolRef::ST_Debug);
1887     // Check for PLT entry registered with symbol name
1888     if (!SymbolAddress && IsAArch64) {
1889       const BinaryData *BD = BC->getPLTBinaryDataByName(SymbolName);
1890       SymbolAddress = BD ? BD->getAddress() : 0;
1891     }
1892   }
1893   // For PIE or dynamic libs, the linker may choose not to put the relocation
1894   // result at the address if it is a X86_64_64 one because it will emit a
1895   // dynamic relocation (X86_RELATIVE) for the dynamic linker and loader to
1896   // resolve it at run time. The static relocation result goes as the addend
1897   // of the dynamic relocation in this case. We can't verify these cases.
1898   // FIXME: perhaps we can try to find if it really emitted a corresponding
1899   // RELATIVE relocation at this offset with the correct value as the addend.
1900   if (!BC->HasFixedLoadAddress && RelSize == 8)
1901     SkipVerification = true;
1902 
1903   if (IsSectionRelocation && !IsAArch64) {
1904     ErrorOr<BinarySection &> Section = BC->getSectionForAddress(SymbolAddress);
1905     assert(Section && "section expected for section relocation");
1906     SymbolName = "section " + std::string(Section->getName());
1907     // Convert section symbol relocations to regular relocations inside
1908     // non-section symbols.
1909     if (Section->containsAddress(ExtractedValue) && !IsPCRelative) {
1910       SymbolAddress = ExtractedValue;
1911       Addend = 0;
1912     } else {
1913       Addend = ExtractedValue - (SymbolAddress - PCRelOffset);
1914     }
1915   }
1916 
1917   // If no symbol has been found or if it is a relocation requiring the
1918   // creation of a GOT entry, do not link against the symbol but against
1919   // whatever address was extracted from the instruction itself. We are
1920   // not creating a GOT entry as this was already processed by the linker.
1921   // For GOT relocs, do not subtract addend as the addend does not refer
1922   // to this instruction's target, but it refers to the target in the GOT
1923   // entry.
1924   if (Relocation::isGOT(RType)) {
1925     Addend = 0;
1926     SymbolAddress = ExtractedValue + PCRelOffset;
1927   } else if (Relocation::isTLS(RType)) {
1928     SkipVerification = true;
1929   } else if (!SymbolAddress) {
1930     assert(!IsSectionRelocation);
1931     if (ExtractedValue || Addend == 0 || IsPCRelative) {
1932       SymbolAddress =
1933           truncateToSize(ExtractedValue - Addend + PCRelOffset, RelSize);
1934     } else {
1935       // This is weird case.  The extracted value is zero but the addend is
1936       // non-zero and the relocation is not pc-rel.  Using the previous logic,
1937       // the SymbolAddress would end up as a huge number.  Seen in
1938       // exceptions_pic.test.
1939       LLVM_DEBUG(dbgs() << "BOLT-DEBUG: relocation @ 0x"
1940                         << Twine::utohexstr(Rel.getOffset())
1941                         << " value does not match addend for "
1942                         << "relocation to undefined symbol.\n");
1943       return true;
1944     }
1945   }
1946 
1947   auto verifyExtractedValue = [&]() {
1948     if (SkipVerification)
1949       return true;
1950 
1951     if (IsAArch64)
1952       return true;
1953 
1954     if (SymbolName == "__hot_start" || SymbolName == "__hot_end")
1955       return true;
1956 
1957     if (RType == ELF::R_X86_64_PLT32)
1958       return true;
1959 
1960     return truncateToSize(ExtractedValue, RelSize) ==
1961            truncateToSize(SymbolAddress + Addend - PCRelOffset, RelSize);
1962   };
1963 
1964   (void)verifyExtractedValue;
1965   assert(verifyExtractedValue() && "mismatched extracted relocation value");
1966 
1967   return true;
1968 }
1969 
1970 void RewriteInstance::processDynamicRelocations() {
1971   // Read relocations for PLT - DT_JMPREL.
1972   if (PLTRelocationsSize > 0) {
1973     ErrorOr<BinarySection &> PLTRelSectionOrErr =
1974         BC->getSectionForAddress(*PLTRelocationsAddress);
1975     if (!PLTRelSectionOrErr)
1976       report_error("unable to find section corresponding to DT_JMPREL",
1977                    PLTRelSectionOrErr.getError());
1978     if (PLTRelSectionOrErr->getSize() != PLTRelocationsSize)
1979       report_error("section size mismatch for DT_PLTRELSZ",
1980                    errc::executable_format_error);
1981     readDynamicRelocations(PLTRelSectionOrErr->getSectionRef(),
1982                            /*IsJmpRel*/ true);
1983   }
1984 
1985   // The rest of dynamic relocations - DT_RELA.
1986   if (DynamicRelocationsSize > 0) {
1987     ErrorOr<BinarySection &> DynamicRelSectionOrErr =
1988         BC->getSectionForAddress(*DynamicRelocationsAddress);
1989     if (!DynamicRelSectionOrErr)
1990       report_error("unable to find section corresponding to DT_RELA",
1991                    DynamicRelSectionOrErr.getError());
1992     if (DynamicRelSectionOrErr->getSize() != DynamicRelocationsSize)
1993       report_error("section size mismatch for DT_RELASZ",
1994                    errc::executable_format_error);
1995     readDynamicRelocations(DynamicRelSectionOrErr->getSectionRef(),
1996                            /*IsJmpRel*/ false);
1997   }
1998 }
1999 
2000 void RewriteInstance::processRelocations() {
2001   if (!BC->HasRelocations)
2002     return;
2003 
2004   for (const SectionRef &Section : InputFile->sections()) {
2005     if (cantFail(Section.getRelocatedSection()) != InputFile->section_end() &&
2006         !BinarySection(*BC, Section).isAllocatable())
2007       readRelocations(Section);
2008   }
2009 
2010   if (NumFailedRelocations)
2011     errs() << "BOLT-WARNING: Failed to analyze " << NumFailedRelocations
2012            << " relocations\n";
2013 }
2014 
2015 void RewriteInstance::insertLKMarker(uint64_t PC, uint64_t SectionOffset,
2016                                      int32_t PCRelativeOffset,
2017                                      bool IsPCRelative, StringRef SectionName) {
2018   BC->LKMarkers[PC].emplace_back(LKInstructionMarkerInfo{
2019       SectionOffset, PCRelativeOffset, IsPCRelative, SectionName});
2020 }
2021 
2022 void RewriteInstance::processLKSections() {
2023   assert(opts::LinuxKernelMode &&
2024          "process Linux Kernel special sections and their relocations only in "
2025          "linux kernel mode.\n");
2026 
2027   processLKExTable();
2028   processLKPCIFixup();
2029   processLKKSymtab();
2030   processLKKSymtab(true);
2031   processLKBugTable();
2032   processLKSMPLocks();
2033 }
2034 
2035 /// Process __ex_table section of Linux Kernel.
2036 /// This section contains information regarding kernel level exception
2037 /// handling (https://www.kernel.org/doc/html/latest/x86/exception-tables.html).
2038 /// More documentation is in arch/x86/include/asm/extable.h.
2039 ///
2040 /// The section is the list of the following structures:
2041 ///
2042 ///   struct exception_table_entry {
2043 ///     int insn;
2044 ///     int fixup;
2045 ///     int handler;
2046 ///   };
2047 ///
2048 void RewriteInstance::processLKExTable() {
2049   ErrorOr<BinarySection &> SectionOrError =
2050       BC->getUniqueSectionByName("__ex_table");
2051   if (!SectionOrError)
2052     return;
2053 
2054   const uint64_t SectionSize = SectionOrError->getSize();
2055   const uint64_t SectionAddress = SectionOrError->getAddress();
2056   assert((SectionSize % 12) == 0 &&
2057          "The size of the __ex_table section should be a multiple of 12");
2058   for (uint64_t I = 0; I < SectionSize; I += 4) {
2059     const uint64_t EntryAddress = SectionAddress + I;
2060     ErrorOr<uint64_t> Offset = BC->getSignedValueAtAddress(EntryAddress, 4);
2061     assert(Offset && "failed reading PC-relative offset for __ex_table");
2062     int32_t SignedOffset = *Offset;
2063     const uint64_t RefAddress = EntryAddress + SignedOffset;
2064 
2065     BinaryFunction *ContainingBF =
2066         BC->getBinaryFunctionContainingAddress(RefAddress);
2067     if (!ContainingBF)
2068       continue;
2069 
2070     MCSymbol *ReferencedSymbol = ContainingBF->getSymbol();
2071     const uint64_t FunctionOffset = RefAddress - ContainingBF->getAddress();
2072     switch (I % 12) {
2073     default:
2074       llvm_unreachable("bad alignment of __ex_table");
2075       break;
2076     case 0:
2077       // insn
2078       insertLKMarker(RefAddress, I, SignedOffset, true, "__ex_table");
2079       break;
2080     case 4:
2081       // fixup
2082       if (FunctionOffset)
2083         ReferencedSymbol = ContainingBF->addEntryPointAtOffset(FunctionOffset);
2084       BC->addRelocation(EntryAddress, ReferencedSymbol, Relocation::getPC32(),
2085                         0, *Offset);
2086       break;
2087     case 8:
2088       // handler
2089       assert(!FunctionOffset &&
2090              "__ex_table handler entry should point to function start");
2091       BC->addRelocation(EntryAddress, ReferencedSymbol, Relocation::getPC32(),
2092                         0, *Offset);
2093       break;
2094     }
2095   }
2096 }
2097 
2098 /// Process .pci_fixup section of Linux Kernel.
2099 /// This section contains a list of entries for different PCI devices and their
2100 /// corresponding hook handler (code pointer where the fixup
2101 /// code resides, usually on x86_64 it is an entry PC relative 32 bit offset).
2102 /// Documentation is in include/linux/pci.h.
2103 void RewriteInstance::processLKPCIFixup() {
2104   ErrorOr<BinarySection &> SectionOrError =
2105       BC->getUniqueSectionByName(".pci_fixup");
2106   assert(SectionOrError &&
2107          ".pci_fixup section not found in Linux Kernel binary");
2108   const uint64_t SectionSize = SectionOrError->getSize();
2109   const uint64_t SectionAddress = SectionOrError->getAddress();
2110   assert((SectionSize % 16) == 0 && ".pci_fixup size is not a multiple of 16");
2111 
2112   for (uint64_t I = 12; I + 4 <= SectionSize; I += 16) {
2113     const uint64_t PC = SectionAddress + I;
2114     ErrorOr<uint64_t> Offset = BC->getSignedValueAtAddress(PC, 4);
2115     assert(Offset && "cannot read value from .pci_fixup");
2116     const int32_t SignedOffset = *Offset;
2117     const uint64_t HookupAddress = PC + SignedOffset;
2118     BinaryFunction *HookupFunction =
2119         BC->getBinaryFunctionAtAddress(HookupAddress);
2120     assert(HookupFunction && "expected function for entry in .pci_fixup");
2121     BC->addRelocation(PC, HookupFunction->getSymbol(), Relocation::getPC32(), 0,
2122                       *Offset);
2123   }
2124 }
2125 
2126 /// Process __ksymtab[_gpl] sections of Linux Kernel.
2127 /// This section lists all the vmlinux symbols that kernel modules can access.
2128 ///
2129 /// All the entries are 4 bytes each and hence we can read them by one by one
2130 /// and ignore the ones that are not pointing to the .text section. All pointers
2131 /// are PC relative offsets. Always, points to the beginning of the function.
2132 void RewriteInstance::processLKKSymtab(bool IsGPL) {
2133   StringRef SectionName = "__ksymtab";
2134   if (IsGPL)
2135     SectionName = "__ksymtab_gpl";
2136   ErrorOr<BinarySection &> SectionOrError =
2137       BC->getUniqueSectionByName(SectionName);
2138   assert(SectionOrError &&
2139          "__ksymtab[_gpl] section not found in Linux Kernel binary");
2140   const uint64_t SectionSize = SectionOrError->getSize();
2141   const uint64_t SectionAddress = SectionOrError->getAddress();
2142   assert((SectionSize % 4) == 0 &&
2143          "The size of the __ksymtab[_gpl] section should be a multiple of 4");
2144 
2145   for (uint64_t I = 0; I < SectionSize; I += 4) {
2146     const uint64_t EntryAddress = SectionAddress + I;
2147     ErrorOr<uint64_t> Offset = BC->getSignedValueAtAddress(EntryAddress, 4);
2148     assert(Offset && "Reading valid PC-relative offset for a ksymtab entry");
2149     const int32_t SignedOffset = *Offset;
2150     const uint64_t RefAddress = EntryAddress + SignedOffset;
2151     BinaryFunction *BF = BC->getBinaryFunctionAtAddress(RefAddress);
2152     if (!BF)
2153       continue;
2154 
2155     BC->addRelocation(EntryAddress, BF->getSymbol(), Relocation::getPC32(), 0,
2156                       *Offset);
2157   }
2158 }
2159 
2160 /// Process __bug_table section.
2161 /// This section contains information useful for kernel debugging.
2162 /// Each entry in the section is a struct bug_entry that contains a pointer to
2163 /// the ud2 instruction corresponding to the bug, corresponding file name (both
2164 /// pointers use PC relative offset addressing), line number, and flags.
2165 /// The definition of the struct bug_entry can be found in
2166 /// `include/asm-generic/bug.h`
2167 void RewriteInstance::processLKBugTable() {
2168   ErrorOr<BinarySection &> SectionOrError =
2169       BC->getUniqueSectionByName("__bug_table");
2170   if (!SectionOrError)
2171     return;
2172 
2173   const uint64_t SectionSize = SectionOrError->getSize();
2174   const uint64_t SectionAddress = SectionOrError->getAddress();
2175   assert((SectionSize % 12) == 0 &&
2176          "The size of the __bug_table section should be a multiple of 12");
2177   for (uint64_t I = 0; I < SectionSize; I += 12) {
2178     const uint64_t EntryAddress = SectionAddress + I;
2179     ErrorOr<uint64_t> Offset = BC->getSignedValueAtAddress(EntryAddress, 4);
2180     assert(Offset &&
2181            "Reading valid PC-relative offset for a __bug_table entry");
2182     const int32_t SignedOffset = *Offset;
2183     const uint64_t RefAddress = EntryAddress + SignedOffset;
2184     assert(BC->getBinaryFunctionContainingAddress(RefAddress) &&
2185            "__bug_table entries should point to a function");
2186 
2187     insertLKMarker(RefAddress, I, SignedOffset, true, "__bug_table");
2188   }
2189 }
2190 
2191 /// .smp_locks section contains PC-relative references to instructions with LOCK
2192 /// prefix. The prefix can be converted to NOP at boot time on non-SMP systems.
2193 void RewriteInstance::processLKSMPLocks() {
2194   ErrorOr<BinarySection &> SectionOrError =
2195       BC->getUniqueSectionByName(".smp_locks");
2196   if (!SectionOrError)
2197     return;
2198 
2199   uint64_t SectionSize = SectionOrError->getSize();
2200   const uint64_t SectionAddress = SectionOrError->getAddress();
2201   assert((SectionSize % 4) == 0 &&
2202          "The size of the .smp_locks section should be a multiple of 4");
2203 
2204   for (uint64_t I = 0; I < SectionSize; I += 4) {
2205     const uint64_t EntryAddress = SectionAddress + I;
2206     ErrorOr<uint64_t> Offset = BC->getSignedValueAtAddress(EntryAddress, 4);
2207     assert(Offset && "Reading valid PC-relative offset for a .smp_locks entry");
2208     int32_t SignedOffset = *Offset;
2209     uint64_t RefAddress = EntryAddress + SignedOffset;
2210 
2211     BinaryFunction *ContainingBF =
2212         BC->getBinaryFunctionContainingAddress(RefAddress);
2213     if (!ContainingBF)
2214       continue;
2215 
2216     insertLKMarker(RefAddress, I, SignedOffset, true, ".smp_locks");
2217   }
2218 }
2219 
2220 void RewriteInstance::readDynamicRelocations(const SectionRef &Section,
2221                                              bool IsJmpRel) {
2222   assert(BinarySection(*BC, Section).isAllocatable() && "allocatable expected");
2223 
2224   LLVM_DEBUG({
2225     StringRef SectionName = cantFail(Section.getName());
2226     dbgs() << "BOLT-DEBUG: reading relocations for section " << SectionName
2227            << ":\n";
2228   });
2229 
2230   for (const RelocationRef &Rel : Section.relocations()) {
2231     const uint64_t RType = Rel.getType();
2232     if (Relocation::isNone(RType))
2233       continue;
2234 
2235     StringRef SymbolName = "<none>";
2236     MCSymbol *Symbol = nullptr;
2237     uint64_t SymbolAddress = 0;
2238     const uint64_t Addend = getRelocationAddend(InputFile, Rel);
2239 
2240     symbol_iterator SymbolIter = Rel.getSymbol();
2241     if (SymbolIter != InputFile->symbol_end()) {
2242       SymbolName = cantFail(SymbolIter->getName());
2243       BinaryData *BD = BC->getBinaryDataByName(SymbolName);
2244       Symbol = BD ? BD->getSymbol()
2245                   : BC->getOrCreateUndefinedGlobalSymbol(SymbolName);
2246       SymbolAddress = cantFail(SymbolIter->getAddress());
2247       (void)SymbolAddress;
2248     }
2249 
2250     LLVM_DEBUG(
2251       SmallString<16> TypeName;
2252       Rel.getTypeName(TypeName);
2253       dbgs() << "BOLT-DEBUG: dynamic relocation at 0x"
2254              << Twine::utohexstr(Rel.getOffset()) << " : " << TypeName
2255              << " : " << SymbolName << " : " <<  Twine::utohexstr(SymbolAddress)
2256              << " : + 0x" << Twine::utohexstr(Addend) << '\n'
2257     );
2258 
2259     if (IsJmpRel)
2260       IsJmpRelocation[RType] = true;
2261 
2262     if (Symbol)
2263       SymbolIndex[Symbol] = getRelocationSymbol(InputFile, Rel);
2264 
2265     BC->addDynamicRelocation(Rel.getOffset(), Symbol, RType, Addend);
2266   }
2267 }
2268 
2269 void RewriteInstance::readRelocations(const SectionRef &Section) {
2270   LLVM_DEBUG({
2271     StringRef SectionName = cantFail(Section.getName());
2272     dbgs() << "BOLT-DEBUG: reading relocations for section " << SectionName
2273            << ":\n";
2274   });
2275   if (BinarySection(*BC, Section).isAllocatable()) {
2276     LLVM_DEBUG(dbgs() << "BOLT-DEBUG: ignoring runtime relocations\n");
2277     return;
2278   }
2279   section_iterator SecIter = cantFail(Section.getRelocatedSection());
2280   assert(SecIter != InputFile->section_end() && "relocated section expected");
2281   SectionRef RelocatedSection = *SecIter;
2282 
2283   StringRef RelocatedSectionName = cantFail(RelocatedSection.getName());
2284   LLVM_DEBUG(dbgs() << "BOLT-DEBUG: relocated section is "
2285                     << RelocatedSectionName << '\n');
2286 
2287   if (!BinarySection(*BC, RelocatedSection).isAllocatable()) {
2288     LLVM_DEBUG(dbgs() << "BOLT-DEBUG: ignoring relocations against "
2289                       << "non-allocatable section\n");
2290     return;
2291   }
2292   const bool SkipRelocs = StringSwitch<bool>(RelocatedSectionName)
2293                               .Cases(".plt", ".rela.plt", ".got.plt",
2294                                      ".eh_frame", ".gcc_except_table", true)
2295                               .Default(false);
2296   if (SkipRelocs) {
2297     LLVM_DEBUG(
2298         dbgs() << "BOLT-DEBUG: ignoring relocations against known section\n");
2299     return;
2300   }
2301 
2302   const bool IsAArch64 = BC->isAArch64();
2303   const bool IsFromCode = RelocatedSection.isText();
2304 
2305   auto printRelocationInfo = [&](const RelocationRef &Rel,
2306                                  StringRef SymbolName,
2307                                  uint64_t SymbolAddress,
2308                                  uint64_t Addend,
2309                                  uint64_t ExtractedValue) {
2310     SmallString<16> TypeName;
2311     Rel.getTypeName(TypeName);
2312     const uint64_t Address = SymbolAddress + Addend;
2313     ErrorOr<BinarySection &> Section = BC->getSectionForAddress(SymbolAddress);
2314     dbgs() << "Relocation: offset = 0x"
2315            << Twine::utohexstr(Rel.getOffset())
2316            << "; type = " << TypeName
2317            << "; value = 0x" << Twine::utohexstr(ExtractedValue)
2318            << "; symbol = " << SymbolName
2319            << " (" << (Section ? Section->getName() : "") << ")"
2320            << "; symbol address = 0x" << Twine::utohexstr(SymbolAddress)
2321            << "; addend = 0x" << Twine::utohexstr(Addend)
2322            << "; address = 0x" << Twine::utohexstr(Address)
2323            << "; in = ";
2324     if (BinaryFunction *Func = BC->getBinaryFunctionContainingAddress(
2325             Rel.getOffset(), false, IsAArch64))
2326       dbgs() << Func->getPrintName() << "\n";
2327     else
2328       dbgs() << BC->getSectionForAddress(Rel.getOffset())->getName() << "\n";
2329   };
2330 
2331   for (const RelocationRef &Rel : Section.relocations()) {
2332     SmallString<16> TypeName;
2333     Rel.getTypeName(TypeName);
2334     uint64_t RType = Rel.getType();
2335     if (Relocation::skipRelocationType(RType))
2336       continue;
2337 
2338     // Adjust the relocation type as the linker might have skewed it.
2339     if (BC->isX86() && (RType & ELF::R_X86_64_converted_reloc_bit)) {
2340       if (opts::Verbosity >= 1)
2341         dbgs() << "BOLT-WARNING: ignoring R_X86_64_converted_reloc_bit\n";
2342       RType &= ~ELF::R_X86_64_converted_reloc_bit;
2343     }
2344 
2345     if (Relocation::isTLS(RType)) {
2346       // No special handling required for TLS relocations on X86.
2347       if (BC->isX86())
2348         continue;
2349 
2350       // The non-got related TLS relocations on AArch64 also could be skipped.
2351       if (!Relocation::isGOT(RType))
2352         continue;
2353     }
2354 
2355     if (!IsAArch64 && BC->getDynamicRelocationAt(Rel.getOffset())) {
2356       LLVM_DEBUG(
2357           dbgs() << "BOLT-DEBUG: address 0x"
2358                  << Twine::utohexstr(Rel.getOffset())
2359                  << " has a dynamic relocation against it. Ignoring static "
2360                     "relocation.\n");
2361       continue;
2362     }
2363 
2364     std::string SymbolName;
2365     uint64_t SymbolAddress;
2366     int64_t Addend;
2367     uint64_t ExtractedValue;
2368     bool IsSectionRelocation;
2369     bool Skip;
2370     if (!analyzeRelocation(Rel, RType, SymbolName, IsSectionRelocation,
2371                            SymbolAddress, Addend, ExtractedValue, Skip)) {
2372       LLVM_DEBUG(dbgs() << "BOLT-WARNING: failed to analyze relocation @ "
2373                         << "offset = 0x" << Twine::utohexstr(Rel.getOffset())
2374                         << "; type name = " << TypeName << '\n');
2375       ++NumFailedRelocations;
2376       continue;
2377     }
2378 
2379     if (Skip) {
2380       LLVM_DEBUG(dbgs() << "BOLT-DEBUG: skipping relocation @ offset = 0x"
2381                         << Twine::utohexstr(Rel.getOffset())
2382                         << "; type name = " << TypeName << '\n');
2383       continue;
2384     }
2385 
2386     const uint64_t Address = SymbolAddress + Addend;
2387 
2388     LLVM_DEBUG(dbgs() << "BOLT-DEBUG: "; printRelocationInfo(
2389                    Rel, SymbolName, SymbolAddress, Addend, ExtractedValue));
2390 
2391     BinaryFunction *ContainingBF = nullptr;
2392     if (IsFromCode) {
2393       ContainingBF =
2394           BC->getBinaryFunctionContainingAddress(Rel.getOffset(),
2395                                                  /*CheckPastEnd*/ false,
2396                                                  /*UseMaxSize*/ true);
2397       assert(ContainingBF && "cannot find function for address in code");
2398       if (!IsAArch64 && !ContainingBF->containsAddress(Rel.getOffset())) {
2399         if (opts::Verbosity >= 1)
2400           outs() << "BOLT-INFO: " << *ContainingBF
2401                  << " has relocations in padding area\n";
2402         ContainingBF->setSize(ContainingBF->getMaxSize());
2403         ContainingBF->setSimple(false);
2404         continue;
2405       }
2406     }
2407 
2408     MCSymbol *ReferencedSymbol = nullptr;
2409     if (!IsSectionRelocation)
2410       if (BinaryData *BD = BC->getBinaryDataByName(SymbolName))
2411         ReferencedSymbol = BD->getSymbol();
2412 
2413     ErrorOr<BinarySection &> ReferencedSection =
2414         BC->getSectionForAddress(SymbolAddress);
2415 
2416     const bool IsToCode = ReferencedSection && ReferencedSection->isText();
2417 
2418     // Special handling of PC-relative relocations.
2419     if (!IsAArch64 && Relocation::isPCRelative(RType)) {
2420       if (!IsFromCode && IsToCode) {
2421         // PC-relative relocations from data to code are tricky since the
2422         // original information is typically lost after linking, even with
2423         // '--emit-relocs'. Such relocations are normally used by PIC-style
2424         // jump tables and they reference both the jump table and jump
2425         // targets by computing the difference between the two. If we blindly
2426         // apply the relocation, it will appear that it references an arbitrary
2427         // location in the code, possibly in a different function from the one
2428         // containing the jump table.
2429         //
2430         // For that reason, we only register the fact that there is a
2431         // PC-relative relocation at a given address against the code.
2432         // The actual referenced label/address will be determined during jump
2433         // table analysis.
2434         BC->addPCRelativeDataRelocation(Rel.getOffset());
2435       } else if (ContainingBF && !IsSectionRelocation && ReferencedSymbol) {
2436         // If we know the referenced symbol, register the relocation from
2437         // the code. It's required  to properly handle cases where
2438         // "symbol + addend" references an object different from "symbol".
2439         ContainingBF->addRelocation(Rel.getOffset(), ReferencedSymbol, RType,
2440                                     Addend, ExtractedValue);
2441       } else {
2442         LLVM_DEBUG(
2443             dbgs() << "BOLT-DEBUG: not creating PC-relative relocation at 0x"
2444                    << Twine::utohexstr(Rel.getOffset()) << " for " << SymbolName
2445                    << "\n");
2446       }
2447 
2448       continue;
2449     }
2450 
2451     bool ForceRelocation = BC->forceSymbolRelocations(SymbolName);
2452     if (BC->isAArch64() && Relocation::isGOT(RType))
2453       ForceRelocation = true;
2454 
2455     if (!ReferencedSection && !ForceRelocation) {
2456       LLVM_DEBUG(
2457           dbgs() << "BOLT-DEBUG: cannot determine referenced section.\n");
2458       continue;
2459     }
2460 
2461     // Occasionally we may see a reference past the last byte of the function
2462     // typically as a result of __builtin_unreachable(). Check it here.
2463     BinaryFunction *ReferencedBF = BC->getBinaryFunctionContainingAddress(
2464         Address, /*CheckPastEnd*/ true, /*UseMaxSize*/ IsAArch64);
2465 
2466     if (!IsSectionRelocation) {
2467       if (BinaryFunction *BF =
2468               BC->getBinaryFunctionContainingAddress(SymbolAddress)) {
2469         if (BF != ReferencedBF) {
2470           // It's possible we are referencing a function without referencing any
2471           // code, e.g. when taking a bitmask action on a function address.
2472           errs() << "BOLT-WARNING: non-standard function reference (e.g. "
2473                     "bitmask) detected against function "
2474                  << *BF;
2475           if (IsFromCode)
2476             errs() << " from function " << *ContainingBF << '\n';
2477           else
2478             errs() << " from data section at 0x"
2479                    << Twine::utohexstr(Rel.getOffset()) << '\n';
2480           LLVM_DEBUG(printRelocationInfo(Rel, SymbolName, SymbolAddress, Addend,
2481                                          ExtractedValue));
2482           ReferencedBF = BF;
2483         }
2484       }
2485     } else if (ReferencedBF) {
2486       assert(ReferencedSection && "section expected for section relocation");
2487       if (*ReferencedBF->getOriginSection() != *ReferencedSection) {
2488         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: ignoring false function reference\n");
2489         ReferencedBF = nullptr;
2490       }
2491     }
2492 
2493     // Workaround for a member function pointer de-virtualization bug. We check
2494     // if a non-pc-relative relocation in the code is pointing to (fptr - 1).
2495     if (IsToCode && ContainingBF && !Relocation::isPCRelative(RType) &&
2496         (!ReferencedBF || (ReferencedBF->getAddress() != Address))) {
2497       if (const BinaryFunction *RogueBF =
2498               BC->getBinaryFunctionAtAddress(Address + 1)) {
2499         // Do an extra check that the function was referenced previously.
2500         // It's a linear search, but it should rarely happen.
2501         bool Found = false;
2502         for (const auto &RelKV : ContainingBF->Relocations) {
2503           const Relocation &Rel = RelKV.second;
2504           if (Rel.Symbol == RogueBF->getSymbol() &&
2505               !Relocation::isPCRelative(Rel.Type)) {
2506             Found = true;
2507             break;
2508           }
2509         }
2510 
2511         if (Found) {
2512           errs() << "BOLT-WARNING: detected possible compiler "
2513                     "de-virtualization bug: -1 addend used with "
2514                     "non-pc-relative relocation against function "
2515                  << *RogueBF << " in function " << *ContainingBF << '\n';
2516           continue;
2517         }
2518       }
2519     }
2520 
2521     if (ForceRelocation) {
2522       std::string Name = Relocation::isGOT(RType) ? "Zero" : SymbolName;
2523       ReferencedSymbol = BC->registerNameAtAddress(Name, 0, 0, 0);
2524       SymbolAddress = 0;
2525       if (Relocation::isGOT(RType))
2526         Addend = Address;
2527       LLVM_DEBUG(dbgs() << "BOLT-DEBUG: forcing relocation against symbol "
2528                         << SymbolName << " with addend " << Addend << '\n');
2529     } else if (ReferencedBF) {
2530       ReferencedSymbol = ReferencedBF->getSymbol();
2531       uint64_t RefFunctionOffset = 0;
2532 
2533       // Adjust the point of reference to a code location inside a function.
2534       if (ReferencedBF->containsAddress(Address, /*UseMaxSize = */true)) {
2535         RefFunctionOffset = Address - ReferencedBF->getAddress();
2536         if (RefFunctionOffset) {
2537           if (ContainingBF && ContainingBF != ReferencedBF) {
2538             ReferencedSymbol =
2539                 ReferencedBF->addEntryPointAtOffset(RefFunctionOffset);
2540           } else {
2541             ReferencedSymbol =
2542                 ReferencedBF->getOrCreateLocalLabel(Address,
2543                                                     /*CreatePastEnd =*/true);
2544             ReferencedBF->registerReferencedOffset(RefFunctionOffset);
2545           }
2546           if (opts::Verbosity > 1 &&
2547               !BinarySection(*BC, RelocatedSection).isReadOnly())
2548             errs() << "BOLT-WARNING: writable reference into the middle of "
2549                    << "the function " << *ReferencedBF
2550                    << " detected at address 0x"
2551                    << Twine::utohexstr(Rel.getOffset()) << '\n';
2552         }
2553         SymbolAddress = Address;
2554         Addend = 0;
2555       }
2556       LLVM_DEBUG(
2557         dbgs() << "  referenced function " << *ReferencedBF;
2558         if (Address != ReferencedBF->getAddress())
2559           dbgs() << " at offset 0x" << Twine::utohexstr(RefFunctionOffset);
2560         dbgs() << '\n'
2561       );
2562     } else {
2563       if (IsToCode && SymbolAddress) {
2564         // This can happen e.g. with PIC-style jump tables.
2565         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: no corresponding function for "
2566                              "relocation against code\n");
2567       }
2568 
2569       // In AArch64 there are zero reasons to keep a reference to the
2570       // "original" symbol plus addend. The original symbol is probably just a
2571       // section symbol. If we are here, this means we are probably accessing
2572       // data, so it is imperative to keep the original address.
2573       if (IsAArch64) {
2574         SymbolName = ("SYMBOLat0x" + Twine::utohexstr(Address)).str();
2575         SymbolAddress = Address;
2576         Addend = 0;
2577       }
2578 
2579       if (BinaryData *BD = BC->getBinaryDataContainingAddress(SymbolAddress)) {
2580         // Note: this assertion is trying to check sanity of BinaryData objects
2581         // but AArch64 has inferred and incomplete object locations coming from
2582         // GOT/TLS or any other non-trivial relocation (that requires creation
2583         // of sections and whose symbol address is not really what should be
2584         // encoded in the instruction). So we essentially disabled this check
2585         // for AArch64 and live with bogus names for objects.
2586         assert((IsAArch64 || IsSectionRelocation ||
2587                 BD->nameStartsWith(SymbolName) ||
2588                 BD->nameStartsWith("PG" + SymbolName) ||
2589                 (BD->nameStartsWith("ANONYMOUS") &&
2590                  (BD->getSectionName().startswith(".plt") ||
2591                   BD->getSectionName().endswith(".plt")))) &&
2592                "BOLT symbol names of all non-section relocations must match "
2593                "up with symbol names referenced in the relocation");
2594 
2595         if (IsSectionRelocation)
2596           BC->markAmbiguousRelocations(*BD, Address);
2597 
2598         ReferencedSymbol = BD->getSymbol();
2599         Addend += (SymbolAddress - BD->getAddress());
2600         SymbolAddress = BD->getAddress();
2601         assert(Address == SymbolAddress + Addend);
2602       } else {
2603         // These are mostly local data symbols but undefined symbols
2604         // in relocation sections can get through here too, from .plt.
2605         assert(
2606             (IsAArch64 || IsSectionRelocation ||
2607              BC->getSectionNameForAddress(SymbolAddress)->startswith(".plt")) &&
2608             "known symbols should not resolve to anonymous locals");
2609 
2610         if (IsSectionRelocation) {
2611           ReferencedSymbol =
2612               BC->getOrCreateGlobalSymbol(SymbolAddress, "SYMBOLat");
2613         } else {
2614           SymbolRef Symbol = *Rel.getSymbol();
2615           const uint64_t SymbolSize =
2616               IsAArch64 ? 0 : ELFSymbolRef(Symbol).getSize();
2617           const uint64_t SymbolAlignment =
2618               IsAArch64 ? 1 : Symbol.getAlignment();
2619           const uint32_t SymbolFlags = cantFail(Symbol.getFlags());
2620           std::string Name;
2621           if (SymbolFlags & SymbolRef::SF_Global) {
2622             Name = SymbolName;
2623           } else {
2624             if (StringRef(SymbolName)
2625                     .startswith(BC->AsmInfo->getPrivateGlobalPrefix()))
2626               Name = NR.uniquify("PG" + SymbolName);
2627             else
2628               Name = NR.uniquify(SymbolName);
2629           }
2630           ReferencedSymbol = BC->registerNameAtAddress(
2631               Name, SymbolAddress, SymbolSize, SymbolAlignment, SymbolFlags);
2632         }
2633 
2634         if (IsSectionRelocation) {
2635           BinaryData *BD = BC->getBinaryDataByName(ReferencedSymbol->getName());
2636           BC->markAmbiguousRelocations(*BD, Address);
2637         }
2638       }
2639     }
2640 
2641     auto checkMaxDataRelocations = [&]() {
2642       ++NumDataRelocations;
2643       if (opts::MaxDataRelocations &&
2644           NumDataRelocations + 1 == opts::MaxDataRelocations) {
2645         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: processing ending on data relocation "
2646                           << NumDataRelocations << ": ");
2647         printRelocationInfo(Rel, ReferencedSymbol->getName(), SymbolAddress,
2648                             Addend, ExtractedValue);
2649       }
2650 
2651       return (!opts::MaxDataRelocations ||
2652               NumDataRelocations < opts::MaxDataRelocations);
2653     };
2654 
2655     if ((ReferencedSection && refersToReorderedSection(ReferencedSection)) ||
2656         (opts::ForceToDataRelocations && checkMaxDataRelocations()))
2657       ForceRelocation = true;
2658 
2659     if (IsFromCode) {
2660       ContainingBF->addRelocation(Rel.getOffset(), ReferencedSymbol, RType,
2661                                   Addend, ExtractedValue);
2662     } else if (IsToCode || ForceRelocation) {
2663       BC->addRelocation(Rel.getOffset(), ReferencedSymbol, RType, Addend,
2664                         ExtractedValue);
2665     } else {
2666       LLVM_DEBUG(
2667           dbgs() << "BOLT-DEBUG: ignoring relocation from data to data\n");
2668     }
2669   }
2670 }
2671 
2672 void RewriteInstance::selectFunctionsToProcess() {
2673   // Extend the list of functions to process or skip from a file.
2674   auto populateFunctionNames = [](cl::opt<std::string> &FunctionNamesFile,
2675                                   cl::list<std::string> &FunctionNames) {
2676     if (FunctionNamesFile.empty())
2677       return;
2678     std::ifstream FuncsFile(FunctionNamesFile, std::ios::in);
2679     std::string FuncName;
2680     while (std::getline(FuncsFile, FuncName))
2681       FunctionNames.push_back(FuncName);
2682   };
2683   populateFunctionNames(opts::FunctionNamesFile, opts::ForceFunctionNames);
2684   populateFunctionNames(opts::SkipFunctionNamesFile, opts::SkipFunctionNames);
2685   populateFunctionNames(opts::FunctionNamesFileNR, opts::ForceFunctionNamesNR);
2686 
2687   // Make a set of functions to process to speed up lookups.
2688   std::unordered_set<std::string> ForceFunctionsNR(
2689       opts::ForceFunctionNamesNR.begin(), opts::ForceFunctionNamesNR.end());
2690 
2691   if ((!opts::ForceFunctionNames.empty() ||
2692        !opts::ForceFunctionNamesNR.empty()) &&
2693       !opts::SkipFunctionNames.empty()) {
2694     errs() << "BOLT-ERROR: cannot select functions to process and skip at the "
2695               "same time. Please use only one type of selection.\n";
2696     exit(1);
2697   }
2698 
2699   uint64_t LiteThresholdExecCount = 0;
2700   if (opts::LiteThresholdPct) {
2701     if (opts::LiteThresholdPct > 100)
2702       opts::LiteThresholdPct = 100;
2703 
2704     std::vector<const BinaryFunction *> TopFunctions;
2705     for (auto &BFI : BC->getBinaryFunctions()) {
2706       const BinaryFunction &Function = BFI.second;
2707       if (ProfileReader->mayHaveProfileData(Function))
2708         TopFunctions.push_back(&Function);
2709     }
2710     std::sort(TopFunctions.begin(), TopFunctions.end(),
2711               [](const BinaryFunction *A, const BinaryFunction *B) {
2712                 return
2713                     A->getKnownExecutionCount() < B->getKnownExecutionCount();
2714               });
2715 
2716     size_t Index = TopFunctions.size() * opts::LiteThresholdPct / 100;
2717     if (Index)
2718       --Index;
2719     LiteThresholdExecCount = TopFunctions[Index]->getKnownExecutionCount();
2720     outs() << "BOLT-INFO: limiting processing to functions with at least "
2721            << LiteThresholdExecCount << " invocations\n";
2722   }
2723   LiteThresholdExecCount = std::max(
2724       LiteThresholdExecCount, static_cast<uint64_t>(opts::LiteThresholdCount));
2725 
2726   uint64_t NumFunctionsToProcess = 0;
2727   auto shouldProcess = [&](const BinaryFunction &Function) {
2728     if (opts::MaxFunctions && NumFunctionsToProcess > opts::MaxFunctions)
2729       return false;
2730 
2731     // If the list is not empty, only process functions from the list.
2732     if (!opts::ForceFunctionNames.empty() || !ForceFunctionsNR.empty()) {
2733       // Regex check (-funcs and -funcs-file options).
2734       for (std::string &Name : opts::ForceFunctionNames)
2735         if (Function.hasNameRegex(Name))
2736           return true;
2737 
2738       // Non-regex check (-funcs-no-regex and -funcs-file-no-regex).
2739       Optional<StringRef> Match =
2740           Function.forEachName([&ForceFunctionsNR](StringRef Name) {
2741             return ForceFunctionsNR.count(Name.str());
2742           });
2743       return Match.hasValue();
2744     }
2745 
2746     for (std::string &Name : opts::SkipFunctionNames)
2747       if (Function.hasNameRegex(Name))
2748         return false;
2749 
2750     if (opts::Lite) {
2751       if (ProfileReader && !ProfileReader->mayHaveProfileData(Function))
2752         return false;
2753 
2754       if (Function.getKnownExecutionCount() < LiteThresholdExecCount)
2755         return false;
2756     }
2757 
2758     return true;
2759   };
2760 
2761   for (auto &BFI : BC->getBinaryFunctions()) {
2762     BinaryFunction &Function = BFI.second;
2763 
2764     // Pseudo functions are explicitly marked by us not to be processed.
2765     if (Function.isPseudo()) {
2766       Function.IsIgnored = true;
2767       Function.HasExternalRefRelocations = true;
2768       continue;
2769     }
2770 
2771     if (!shouldProcess(Function)) {
2772       LLVM_DEBUG(dbgs() << "BOLT-INFO: skipping processing of function "
2773                         << Function << " per user request\n");
2774       Function.setIgnored();
2775     } else {
2776       ++NumFunctionsToProcess;
2777       if (opts::MaxFunctions && NumFunctionsToProcess == opts::MaxFunctions)
2778         outs() << "BOLT-INFO: processing ending on " << Function << '\n';
2779     }
2780   }
2781 }
2782 
2783 void RewriteInstance::readDebugInfo() {
2784   NamedRegionTimer T("readDebugInfo", "read debug info", TimerGroupName,
2785                      TimerGroupDesc, opts::TimeRewrite);
2786   if (!opts::UpdateDebugSections)
2787     return;
2788 
2789   BC->preprocessDebugInfo();
2790 }
2791 
2792 void RewriteInstance::preprocessProfileData() {
2793   if (!ProfileReader)
2794     return;
2795 
2796   NamedRegionTimer T("preprocessprofile", "pre-process profile data",
2797                      TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
2798 
2799   outs() << "BOLT-INFO: pre-processing profile using "
2800          << ProfileReader->getReaderName() << '\n';
2801 
2802   if (BAT->enabledFor(InputFile)) {
2803     outs() << "BOLT-INFO: profile collection done on a binary already "
2804               "processed by BOLT\n";
2805     ProfileReader->setBAT(&*BAT);
2806   }
2807 
2808   if (Error E = ProfileReader->preprocessProfile(*BC.get()))
2809     report_error("cannot pre-process profile", std::move(E));
2810 
2811   if (!BC->hasSymbolsWithFileName() && ProfileReader->hasLocalsWithFileName() &&
2812       !opts::AllowStripped) {
2813     errs() << "BOLT-ERROR: input binary does not have local file symbols "
2814               "but profile data includes function names with embedded file "
2815               "names. It appears that the input binary was stripped while a "
2816               "profiled binary was not. If you know what you are doing and "
2817               "wish to proceed, use -allow-stripped option.\n";
2818     exit(1);
2819   }
2820 }
2821 
2822 void RewriteInstance::processProfileDataPreCFG() {
2823   if (!ProfileReader)
2824     return;
2825 
2826   NamedRegionTimer T("processprofile-precfg", "process profile data pre-CFG",
2827                      TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
2828 
2829   if (Error E = ProfileReader->readProfilePreCFG(*BC.get()))
2830     report_error("cannot read profile pre-CFG", std::move(E));
2831 }
2832 
2833 void RewriteInstance::processProfileData() {
2834   if (!ProfileReader)
2835     return;
2836 
2837   NamedRegionTimer T("processprofile", "process profile data", TimerGroupName,
2838                      TimerGroupDesc, opts::TimeRewrite);
2839 
2840   if (Error E = ProfileReader->readProfile(*BC.get()))
2841     report_error("cannot read profile", std::move(E));
2842 
2843   if (!opts::SaveProfile.empty()) {
2844     YAMLProfileWriter PW(opts::SaveProfile);
2845     PW.writeProfile(*this);
2846   }
2847 
2848   // Release memory used by profile reader.
2849   ProfileReader.reset();
2850 
2851   if (opts::AggregateOnly)
2852     exit(0);
2853 }
2854 
2855 void RewriteInstance::disassembleFunctions() {
2856   NamedRegionTimer T("disassembleFunctions", "disassemble functions",
2857                      TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
2858   for (auto &BFI : BC->getBinaryFunctions()) {
2859     BinaryFunction &Function = BFI.second;
2860 
2861     ErrorOr<ArrayRef<uint8_t>> FunctionData = Function.getData();
2862     if (!FunctionData) {
2863       errs() << "BOLT-ERROR: corresponding section is non-executable or "
2864              << "empty for function " << Function << '\n';
2865       exit(1);
2866     }
2867 
2868     // Treat zero-sized functions as non-simple ones.
2869     if (Function.getSize() == 0) {
2870       Function.setSimple(false);
2871       continue;
2872     }
2873 
2874     // Offset of the function in the file.
2875     const auto *FileBegin =
2876         reinterpret_cast<const uint8_t *>(InputFile->getData().data());
2877     Function.setFileOffset(FunctionData->begin() - FileBegin);
2878 
2879     if (!shouldDisassemble(Function)) {
2880       NamedRegionTimer T("scan", "scan functions", "buildfuncs",
2881                          "Scan Binary Functions", opts::TimeBuild);
2882       Function.scanExternalRefs();
2883       Function.setSimple(false);
2884       continue;
2885     }
2886 
2887     if (!Function.disassemble()) {
2888       if (opts::processAllFunctions())
2889         BC->exitWithBugReport("function cannot be properly disassembled. "
2890                               "Unable to continue in relocation mode.",
2891                               Function);
2892       if (opts::Verbosity >= 1)
2893         outs() << "BOLT-INFO: could not disassemble function " << Function
2894                << ". Will ignore.\n";
2895       // Forcefully ignore the function.
2896       Function.setIgnored();
2897       continue;
2898     }
2899 
2900     if (opts::PrintAll || opts::PrintDisasm)
2901       Function.print(outs(), "after disassembly", true);
2902 
2903     BC->processInterproceduralReferences(Function);
2904   }
2905 
2906   BC->clearJumpTableOffsets();
2907   BC->populateJumpTables();
2908   BC->skipMarkedFragments();
2909 
2910   for (auto &BFI : BC->getBinaryFunctions()) {
2911     BinaryFunction &Function = BFI.second;
2912 
2913     if (!shouldDisassemble(Function))
2914       continue;
2915 
2916     Function.postProcessEntryPoints();
2917     Function.postProcessJumpTables();
2918   }
2919 
2920   BC->adjustCodePadding();
2921 
2922   for (auto &BFI : BC->getBinaryFunctions()) {
2923     BinaryFunction &Function = BFI.second;
2924 
2925     if (!shouldDisassemble(Function))
2926       continue;
2927 
2928     if (!Function.isSimple()) {
2929       assert((!BC->HasRelocations || Function.getSize() == 0 ||
2930               Function.hasSplitJumpTable()) &&
2931              "unexpected non-simple function in relocation mode");
2932       continue;
2933     }
2934 
2935     // Fill in CFI information for this function
2936     if (!Function.trapsOnEntry() && !CFIRdWrt->fillCFIInfoFor(Function)) {
2937       if (BC->HasRelocations) {
2938         BC->exitWithBugReport("unable to fill CFI.", Function);
2939       } else {
2940         errs() << "BOLT-WARNING: unable to fill CFI for function " << Function
2941                << ". Skipping.\n";
2942         Function.setSimple(false);
2943         continue;
2944       }
2945     }
2946 
2947     // Parse LSDA.
2948     if (Function.getLSDAAddress() != 0)
2949       Function.parseLSDA(getLSDAData(), getLSDAAddress());
2950   }
2951 }
2952 
2953 void RewriteInstance::buildFunctionsCFG() {
2954   NamedRegionTimer T("buildCFG", "buildCFG", "buildfuncs",
2955                      "Build Binary Functions", opts::TimeBuild);
2956 
2957   // Create annotation indices to allow lock-free execution
2958   BC->MIB->getOrCreateAnnotationIndex("JTIndexReg");
2959   BC->MIB->getOrCreateAnnotationIndex("NOP");
2960   BC->MIB->getOrCreateAnnotationIndex("Size");
2961 
2962   ParallelUtilities::WorkFuncWithAllocTy WorkFun =
2963       [&](BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy AllocId) {
2964         if (!BF.buildCFG(AllocId))
2965           return;
2966 
2967         if (opts::PrintAll) {
2968           auto L = BC->scopeLock();
2969           BF.print(outs(), "while building cfg", true);
2970         }
2971       };
2972 
2973   ParallelUtilities::PredicateTy SkipPredicate = [&](const BinaryFunction &BF) {
2974     return !shouldDisassemble(BF) || !BF.isSimple();
2975   };
2976 
2977   ParallelUtilities::runOnEachFunctionWithUniqueAllocId(
2978       *BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun,
2979       SkipPredicate, "disassembleFunctions-buildCFG",
2980       /*ForceSequential*/ opts::SequentialDisassembly || opts::PrintAll);
2981 
2982   BC->postProcessSymbolTable();
2983 }
2984 
2985 void RewriteInstance::postProcessFunctions() {
2986   BC->TotalScore = 0;
2987   BC->SumExecutionCount = 0;
2988   for (auto &BFI : BC->getBinaryFunctions()) {
2989     BinaryFunction &Function = BFI.second;
2990 
2991     if (Function.empty())
2992       continue;
2993 
2994     Function.postProcessCFG();
2995 
2996     if (opts::PrintAll || opts::PrintCFG)
2997       Function.print(outs(), "after building cfg", true);
2998 
2999     if (opts::DumpDotAll)
3000       Function.dumpGraphForPass("00_build-cfg");
3001 
3002     if (opts::PrintLoopInfo) {
3003       Function.calculateLoopInfo();
3004       Function.printLoopInfo(outs());
3005     }
3006 
3007     BC->TotalScore += Function.getFunctionScore();
3008     BC->SumExecutionCount += Function.getKnownExecutionCount();
3009   }
3010 
3011   if (opts::PrintGlobals) {
3012     outs() << "BOLT-INFO: Global symbols:\n";
3013     BC->printGlobalSymbols(outs());
3014   }
3015 }
3016 
3017 void RewriteInstance::runOptimizationPasses() {
3018   NamedRegionTimer T("runOptimizationPasses", "run optimization passes",
3019                      TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
3020   BinaryFunctionPassManager::runAllPasses(*BC);
3021 }
3022 
3023 namespace {
3024 
3025 class BOLTSymbolResolver : public JITSymbolResolver {
3026   BinaryContext &BC;
3027 
3028 public:
3029   BOLTSymbolResolver(BinaryContext &BC) : BC(BC) {}
3030 
3031   // We are responsible for all symbols
3032   Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override {
3033     return Symbols;
3034   }
3035 
3036   // Some of our symbols may resolve to zero and this should not be an error
3037   bool allowsZeroSymbols() override { return true; }
3038 
3039   /// Resolves the address of each symbol requested
3040   void lookup(const LookupSet &Symbols,
3041               OnResolvedFunction OnResolved) override {
3042     JITSymbolResolver::LookupResult AllResults;
3043 
3044     if (BC.EFMM->ObjectsLoaded) {
3045       for (const StringRef &Symbol : Symbols) {
3046         std::string SymName = Symbol.str();
3047         LLVM_DEBUG(dbgs() << "BOLT: looking for " << SymName << "\n");
3048         // Resolve to a PLT entry if possible
3049         if (const BinaryData *I = BC.getPLTBinaryDataByName(SymName)) {
3050           AllResults[Symbol] =
3051               JITEvaluatedSymbol(I->getAddress(), JITSymbolFlags());
3052           continue;
3053         }
3054         OnResolved(make_error<StringError>(
3055             "Symbol not found required by runtime: " + Symbol,
3056             inconvertibleErrorCode()));
3057         return;
3058       }
3059       OnResolved(std::move(AllResults));
3060       return;
3061     }
3062 
3063     for (const StringRef &Symbol : Symbols) {
3064       std::string SymName = Symbol.str();
3065       LLVM_DEBUG(dbgs() << "BOLT: looking for " << SymName << "\n");
3066 
3067       if (BinaryData *I = BC.getBinaryDataByName(SymName)) {
3068         uint64_t Address = I->isMoved() && !I->isJumpTable()
3069                                ? I->getOutputAddress()
3070                                : I->getAddress();
3071         LLVM_DEBUG(dbgs() << "Resolved to address 0x"
3072                           << Twine::utohexstr(Address) << "\n");
3073         AllResults[Symbol] = JITEvaluatedSymbol(Address, JITSymbolFlags());
3074         continue;
3075       }
3076       LLVM_DEBUG(dbgs() << "Resolved to address 0x0\n");
3077       AllResults[Symbol] = JITEvaluatedSymbol(0, JITSymbolFlags());
3078     }
3079 
3080     OnResolved(std::move(AllResults));
3081   }
3082 };
3083 
3084 } // anonymous namespace
3085 
3086 void RewriteInstance::emitAndLink() {
3087   NamedRegionTimer T("emitAndLink", "emit and link", TimerGroupName,
3088                      TimerGroupDesc, opts::TimeRewrite);
3089   std::error_code EC;
3090 
3091   // This is an object file, which we keep for debugging purposes.
3092   // Once we decide it's useless, we should create it in memory.
3093   SmallString<128> OutObjectPath;
3094   sys::fs::getPotentiallyUniqueTempFileName("output", "o", OutObjectPath);
3095   std::unique_ptr<ToolOutputFile> TempOut =
3096       std::make_unique<ToolOutputFile>(OutObjectPath, EC, sys::fs::OF_None);
3097   check_error(EC, "cannot create output object file");
3098 
3099   std::unique_ptr<buffer_ostream> BOS =
3100       std::make_unique<buffer_ostream>(TempOut->os());
3101   raw_pwrite_stream *OS = BOS.get();
3102 
3103   // Implicitly MCObjectStreamer takes ownership of MCAsmBackend (MAB)
3104   // and MCCodeEmitter (MCE). ~MCObjectStreamer() will delete these
3105   // two instances.
3106   std::unique_ptr<MCStreamer> Streamer = BC->createStreamer(*OS);
3107 
3108   if (EHFrameSection) {
3109     if (opts::UseOldText || opts::StrictMode) {
3110       // The section is going to be regenerated from scratch.
3111       // Empty the contents, but keep the section reference.
3112       EHFrameSection->clearContents();
3113     } else {
3114       // Make .eh_frame relocatable.
3115       relocateEHFrameSection();
3116     }
3117   }
3118 
3119   emitBinaryContext(*Streamer, *BC, getOrgSecPrefix());
3120 
3121   Streamer->finish();
3122   if (Streamer->getContext().hadError()) {
3123     errs() << "BOLT-ERROR: Emission failed.\n";
3124     exit(1);
3125   }
3126 
3127   //////////////////////////////////////////////////////////////////////////////
3128   // Assign addresses to new sections.
3129   //////////////////////////////////////////////////////////////////////////////
3130 
3131   // Get output object as ObjectFile.
3132   std::unique_ptr<MemoryBuffer> ObjectMemBuffer =
3133       MemoryBuffer::getMemBuffer(BOS->str(), "in-memory object file", false);
3134   std::unique_ptr<object::ObjectFile> Obj = cantFail(
3135       object::ObjectFile::createObjectFile(ObjectMemBuffer->getMemBufferRef()),
3136       "error creating in-memory object");
3137 
3138   BOLTSymbolResolver Resolver = BOLTSymbolResolver(*BC);
3139 
3140   MCAsmLayout FinalLayout(
3141       static_cast<MCObjectStreamer *>(Streamer.get())->getAssembler());
3142 
3143   RTDyld.reset(new decltype(RTDyld)::element_type(*BC->EFMM, Resolver));
3144   RTDyld->setProcessAllSections(false);
3145   RTDyld->loadObject(*Obj);
3146 
3147   // Assign addresses to all sections. If key corresponds to the object
3148   // created by ourselves, call our regular mapping function. If we are
3149   // loading additional objects as part of runtime libraries for
3150   // instrumentation, treat them as extra sections.
3151   mapFileSections(*RTDyld);
3152 
3153   RTDyld->finalizeWithMemoryManagerLocking();
3154   if (RTDyld->hasError()) {
3155     errs() << "BOLT-ERROR: RTDyld failed: " << RTDyld->getErrorString() << "\n";
3156     exit(1);
3157   }
3158 
3159   // Update output addresses based on the new section map and
3160   // layout. Only do this for the object created by ourselves.
3161   updateOutputValues(FinalLayout);
3162 
3163   if (opts::UpdateDebugSections)
3164     DebugInfoRewriter->updateLineTableOffsets(FinalLayout);
3165 
3166   if (RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary())
3167     RtLibrary->link(*BC, ToolPath, *RTDyld, [this](RuntimeDyld &R) {
3168       this->mapExtraSections(*RTDyld);
3169     });
3170 
3171   // Once the code is emitted, we can rename function sections to actual
3172   // output sections and de-register sections used for emission.
3173   for (BinaryFunction *Function : BC->getAllBinaryFunctions()) {
3174     ErrorOr<BinarySection &> Section = Function->getCodeSection();
3175     if (Section &&
3176         (Function->getImageAddress() == 0 || Function->getImageSize() == 0))
3177       continue;
3178 
3179     // Restore origin section for functions that were emitted or supposed to
3180     // be emitted to patch sections.
3181     if (Section)
3182       BC->deregisterSection(*Section);
3183     assert(Function->getOriginSectionName() && "expected origin section");
3184     Function->CodeSectionName = std::string(*Function->getOriginSectionName());
3185     if (Function->isSplit()) {
3186       if (ErrorOr<BinarySection &> ColdSection = Function->getColdCodeSection())
3187         BC->deregisterSection(*ColdSection);
3188       Function->ColdCodeSectionName = std::string(getBOLTTextSectionName());
3189     }
3190   }
3191 
3192   if (opts::PrintCacheMetrics) {
3193     outs() << "BOLT-INFO: cache metrics after emitting functions:\n";
3194     CacheMetrics::printAll(BC->getSortedFunctions());
3195   }
3196 
3197   if (opts::KeepTmp) {
3198     TempOut->keep();
3199     outs() << "BOLT-INFO: intermediary output object file saved for debugging "
3200               "purposes: "
3201            << OutObjectPath << "\n";
3202   }
3203 }
3204 
3205 void RewriteInstance::updateMetadata() {
3206   updateSDTMarkers();
3207   updateLKMarkers();
3208   parsePseudoProbe();
3209   updatePseudoProbes();
3210 
3211   if (opts::UpdateDebugSections) {
3212     NamedRegionTimer T("updateDebugInfo", "update debug info", TimerGroupName,
3213                        TimerGroupDesc, opts::TimeRewrite);
3214     DebugInfoRewriter->updateDebugInfo();
3215   }
3216 
3217   if (opts::WriteBoltInfoSection)
3218     addBoltInfoSection();
3219 }
3220 
3221 void RewriteInstance::updatePseudoProbes() {
3222   // check if there is pseudo probe section decoded
3223   if (BC->ProbeDecoder.getAddress2ProbesMap().empty())
3224     return;
3225   // input address converted to output
3226   AddressProbesMap &Address2ProbesMap = BC->ProbeDecoder.getAddress2ProbesMap();
3227   const GUIDProbeFunctionMap &GUID2Func =
3228       BC->ProbeDecoder.getGUID2FuncDescMap();
3229 
3230   for (auto &AP : Address2ProbesMap) {
3231     BinaryFunction *F = BC->getBinaryFunctionContainingAddress(AP.first);
3232     // If F is removed, eliminate all probes inside it from inline tree
3233     // Setting probes' addresses as INT64_MAX means elimination
3234     if (!F) {
3235       for (MCDecodedPseudoProbe &Probe : AP.second)
3236         Probe.setAddress(INT64_MAX);
3237       continue;
3238     }
3239     // If F is not emitted, the function will remain in the same address as its
3240     // input
3241     if (!F->isEmitted())
3242       continue;
3243 
3244     uint64_t Offset = AP.first - F->getAddress();
3245     const BinaryBasicBlock *BB = F->getBasicBlockContainingOffset(Offset);
3246     uint64_t BlkOutputAddress = BB->getOutputAddressRange().first;
3247     // Check if block output address is defined.
3248     // If not, such block is removed from binary. Then remove the probes from
3249     // inline tree
3250     if (BlkOutputAddress == 0) {
3251       for (MCDecodedPseudoProbe &Probe : AP.second)
3252         Probe.setAddress(INT64_MAX);
3253       continue;
3254     }
3255 
3256     unsigned ProbeTrack = AP.second.size();
3257     std::list<MCDecodedPseudoProbe>::iterator Probe = AP.second.begin();
3258     while (ProbeTrack != 0) {
3259       if (Probe->isBlock()) {
3260         Probe->setAddress(BlkOutputAddress);
3261       } else if (Probe->isCall()) {
3262         // A call probe may be duplicated due to ICP
3263         // Go through output of InputOffsetToAddressMap to collect all related
3264         // probes
3265         const InputOffsetToAddressMapTy &Offset2Addr =
3266             F->getInputOffsetToAddressMap();
3267         auto CallOutputAddresses = Offset2Addr.equal_range(Offset);
3268         auto CallOutputAddress = CallOutputAddresses.first;
3269         if (CallOutputAddress == CallOutputAddresses.second) {
3270           Probe->setAddress(INT64_MAX);
3271         } else {
3272           Probe->setAddress(CallOutputAddress->second);
3273           CallOutputAddress = std::next(CallOutputAddress);
3274         }
3275 
3276         while (CallOutputAddress != CallOutputAddresses.second) {
3277           AP.second.push_back(*Probe);
3278           AP.second.back().setAddress(CallOutputAddress->second);
3279           Probe->getInlineTreeNode()->addProbes(&(AP.second.back()));
3280           CallOutputAddress = std::next(CallOutputAddress);
3281         }
3282       }
3283       Probe = std::next(Probe);
3284       ProbeTrack--;
3285     }
3286   }
3287 
3288   if (opts::PrintPseudoProbes == opts::PrintPseudoProbesOptions::PPP_All ||
3289       opts::PrintPseudoProbes ==
3290           opts::PrintPseudoProbesOptions::PPP_Probes_Address_Conversion) {
3291     outs() << "Pseudo Probe Address Conversion results:\n";
3292     // table that correlates address to block
3293     std::unordered_map<uint64_t, StringRef> Addr2BlockNames;
3294     for (auto &F : BC->getBinaryFunctions())
3295       for (BinaryBasicBlock &BinaryBlock : F.second)
3296         Addr2BlockNames[BinaryBlock.getOutputAddressRange().first] =
3297             BinaryBlock.getName();
3298 
3299     // scan all addresses -> correlate probe to block when print out
3300     std::vector<uint64_t> Addresses;
3301     for (auto &Entry : Address2ProbesMap)
3302       Addresses.push_back(Entry.first);
3303     std::sort(Addresses.begin(), Addresses.end());
3304     for (uint64_t Key : Addresses) {
3305       for (MCDecodedPseudoProbe &Probe : Address2ProbesMap[Key]) {
3306         if (Probe.getAddress() == INT64_MAX)
3307           outs() << "Deleted Probe: ";
3308         else
3309           outs() << "Address: " << format_hex(Probe.getAddress(), 8) << " ";
3310         Probe.print(outs(), GUID2Func, true);
3311         // print block name only if the probe is block type and undeleted.
3312         if (Probe.isBlock() && Probe.getAddress() != INT64_MAX)
3313           outs() << format_hex(Probe.getAddress(), 8) << " Probe is in "
3314                  << Addr2BlockNames[Probe.getAddress()] << "\n";
3315       }
3316     }
3317     outs() << "=======================================\n";
3318   }
3319 
3320   // encode pseudo probes with updated addresses
3321   encodePseudoProbes();
3322 }
3323 
3324 template <typename F>
3325 static void emitLEB128IntValue(F encode, uint64_t Value,
3326                                SmallString<8> &Contents) {
3327   SmallString<128> Tmp;
3328   raw_svector_ostream OSE(Tmp);
3329   encode(Value, OSE);
3330   Contents.append(OSE.str().begin(), OSE.str().end());
3331 }
3332 
3333 void RewriteInstance::encodePseudoProbes() {
3334   // Buffer for new pseudo probes section
3335   SmallString<8> Contents;
3336   MCDecodedPseudoProbe *LastProbe = nullptr;
3337 
3338   auto EmitInt = [&](uint64_t Value, uint32_t Size) {
3339     const bool IsLittleEndian = BC->AsmInfo->isLittleEndian();
3340     uint64_t Swapped = support::endian::byte_swap(
3341         Value, IsLittleEndian ? support::little : support::big);
3342     unsigned Index = IsLittleEndian ? 0 : 8 - Size;
3343     auto Entry = StringRef(reinterpret_cast<char *>(&Swapped) + Index, Size);
3344     Contents.append(Entry.begin(), Entry.end());
3345   };
3346 
3347   auto EmitULEB128IntValue = [&](uint64_t Value) {
3348     SmallString<128> Tmp;
3349     raw_svector_ostream OSE(Tmp);
3350     encodeULEB128(Value, OSE, 0);
3351     Contents.append(OSE.str().begin(), OSE.str().end());
3352   };
3353 
3354   auto EmitSLEB128IntValue = [&](int64_t Value) {
3355     SmallString<128> Tmp;
3356     raw_svector_ostream OSE(Tmp);
3357     encodeSLEB128(Value, OSE);
3358     Contents.append(OSE.str().begin(), OSE.str().end());
3359   };
3360 
3361   // Emit indiviual pseudo probes in a inline tree node
3362   // Probe index, type, attribute, address type and address are encoded
3363   // Address of the first probe is absolute.
3364   // Other probes' address are represented by delta
3365   auto EmitDecodedPseudoProbe = [&](MCDecodedPseudoProbe *&CurProbe) {
3366     EmitULEB128IntValue(CurProbe->getIndex());
3367     uint8_t PackedType = CurProbe->getType() | (CurProbe->getAttributes() << 4);
3368     uint8_t Flag =
3369         LastProbe ? ((int8_t)MCPseudoProbeFlag::AddressDelta << 7) : 0;
3370     EmitInt(Flag | PackedType, 1);
3371     if (LastProbe) {
3372       // Emit the delta between the address label and LastProbe.
3373       int64_t Delta = CurProbe->getAddress() - LastProbe->getAddress();
3374       EmitSLEB128IntValue(Delta);
3375     } else {
3376       // Emit absolute address for encoding the first pseudo probe.
3377       uint32_t AddrSize = BC->AsmInfo->getCodePointerSize();
3378       EmitInt(CurProbe->getAddress(), AddrSize);
3379     }
3380   };
3381 
3382   std::map<InlineSite, MCDecodedPseudoProbeInlineTree *,
3383            std::greater<InlineSite>>
3384       Inlinees;
3385 
3386   // DFS of inline tree to emit pseudo probes in all tree node
3387   // Inline site index of a probe is emitted first.
3388   // Then tree node Guid, size of pseudo probes and children nodes, and detail
3389   // of contained probes are emitted Deleted probes are skipped Root node is not
3390   // encoded to binaries. It's a "wrapper" of inline trees of each function.
3391   std::list<std::pair<uint64_t, MCDecodedPseudoProbeInlineTree *>> NextNodes;
3392   const MCDecodedPseudoProbeInlineTree &Root =
3393       BC->ProbeDecoder.getDummyInlineRoot();
3394   for (auto Child = Root.getChildren().begin();
3395        Child != Root.getChildren().end(); ++Child)
3396     Inlinees[Child->first] = Child->second.get();
3397 
3398   for (auto Inlinee : Inlinees)
3399     // INT64_MAX is "placeholder" of unused callsite index field in the pair
3400     NextNodes.push_back({INT64_MAX, Inlinee.second});
3401 
3402   Inlinees.clear();
3403 
3404   while (!NextNodes.empty()) {
3405     uint64_t ProbeIndex = NextNodes.back().first;
3406     MCDecodedPseudoProbeInlineTree *Cur = NextNodes.back().second;
3407     NextNodes.pop_back();
3408 
3409     if (Cur->Parent && !Cur->Parent->isRoot())
3410       // Emit probe inline site
3411       EmitULEB128IntValue(ProbeIndex);
3412 
3413     // Emit probes grouped by GUID.
3414     LLVM_DEBUG({
3415       dbgs().indent(MCPseudoProbeTable::DdgPrintIndent);
3416       dbgs() << "GUID: " << Cur->Guid << "\n";
3417     });
3418     // Emit Guid
3419     EmitInt(Cur->Guid, 8);
3420     // Emit number of probes in this node
3421     uint64_t Deleted = 0;
3422     for (MCDecodedPseudoProbe *&Probe : Cur->getProbes())
3423       if (Probe->getAddress() == INT64_MAX)
3424         Deleted++;
3425     LLVM_DEBUG(dbgs() << "Deleted Probes:" << Deleted << "\n");
3426     uint64_t ProbesSize = Cur->getProbes().size() - Deleted;
3427     EmitULEB128IntValue(ProbesSize);
3428     // Emit number of direct inlinees
3429     EmitULEB128IntValue(Cur->getChildren().size());
3430     // Emit probes in this group
3431     for (MCDecodedPseudoProbe *&Probe : Cur->getProbes()) {
3432       if (Probe->getAddress() == INT64_MAX)
3433         continue;
3434       EmitDecodedPseudoProbe(Probe);
3435       LastProbe = Probe;
3436     }
3437 
3438     for (auto Child = Cur->getChildren().begin();
3439          Child != Cur->getChildren().end(); ++Child)
3440       Inlinees[Child->first] = Child->second.get();
3441     for (const auto &Inlinee : Inlinees) {
3442       assert(Cur->Guid != 0 && "non root tree node must have nonzero Guid");
3443       NextNodes.push_back({std::get<1>(Inlinee.first), Inlinee.second});
3444       LLVM_DEBUG({
3445         dbgs().indent(MCPseudoProbeTable::DdgPrintIndent);
3446         dbgs() << "InlineSite: " << std::get<1>(Inlinee.first) << "\n";
3447       });
3448     }
3449     Inlinees.clear();
3450   }
3451 
3452   // Create buffer for new contents for the section
3453   // Freed when parent section is destroyed
3454   uint8_t *Output = new uint8_t[Contents.str().size()];
3455   memcpy(Output, Contents.str().data(), Contents.str().size());
3456   addToDebugSectionsToOverwrite(".pseudo_probe");
3457   BC->registerOrUpdateSection(".pseudo_probe", PseudoProbeSection->getELFType(),
3458                               PseudoProbeSection->getELFFlags(), Output,
3459                               Contents.str().size(), 1);
3460   if (opts::PrintPseudoProbes == opts::PrintPseudoProbesOptions::PPP_All ||
3461       opts::PrintPseudoProbes ==
3462           opts::PrintPseudoProbesOptions::PPP_Encoded_Probes) {
3463     // create a dummy decoder;
3464     MCPseudoProbeDecoder DummyDecoder;
3465     StringRef DescContents = PseudoProbeDescSection->getContents();
3466     DummyDecoder.buildGUID2FuncDescMap(
3467         reinterpret_cast<const uint8_t *>(DescContents.data()),
3468         DescContents.size());
3469     StringRef ProbeContents = PseudoProbeSection->getOutputContents();
3470     DummyDecoder.buildAddress2ProbeMap(
3471         reinterpret_cast<const uint8_t *>(ProbeContents.data()),
3472         ProbeContents.size());
3473     DummyDecoder.printProbesForAllAddresses(outs());
3474   }
3475 }
3476 
3477 void RewriteInstance::updateSDTMarkers() {
3478   NamedRegionTimer T("updateSDTMarkers", "update SDT markers", TimerGroupName,
3479                      TimerGroupDesc, opts::TimeRewrite);
3480 
3481   if (!SDTSection)
3482     return;
3483   SDTSection->registerPatcher(std::make_unique<SimpleBinaryPatcher>());
3484 
3485   SimpleBinaryPatcher *SDTNotePatcher =
3486       static_cast<SimpleBinaryPatcher *>(SDTSection->getPatcher());
3487   for (auto &SDTInfoKV : BC->SDTMarkers) {
3488     const uint64_t OriginalAddress = SDTInfoKV.first;
3489     SDTMarkerInfo &SDTInfo = SDTInfoKV.second;
3490     const BinaryFunction *F =
3491         BC->getBinaryFunctionContainingAddress(OriginalAddress);
3492     if (!F)
3493       continue;
3494     const uint64_t NewAddress =
3495         F->translateInputToOutputAddress(OriginalAddress);
3496     SDTNotePatcher->addLE64Patch(SDTInfo.PCOffset, NewAddress);
3497   }
3498 }
3499 
3500 void RewriteInstance::updateLKMarkers() {
3501   if (BC->LKMarkers.size() == 0)
3502     return;
3503 
3504   NamedRegionTimer T("updateLKMarkers", "update LK markers", TimerGroupName,
3505                      TimerGroupDesc, opts::TimeRewrite);
3506 
3507   std::unordered_map<std::string, uint64_t> PatchCounts;
3508   for (std::pair<const uint64_t, std::vector<LKInstructionMarkerInfo>>
3509            &LKMarkerInfoKV : BC->LKMarkers) {
3510     const uint64_t OriginalAddress = LKMarkerInfoKV.first;
3511     const BinaryFunction *BF =
3512         BC->getBinaryFunctionContainingAddress(OriginalAddress, false, true);
3513     if (!BF)
3514       continue;
3515 
3516     uint64_t NewAddress = BF->translateInputToOutputAddress(OriginalAddress);
3517     if (NewAddress == 0)
3518       continue;
3519 
3520     // Apply base address.
3521     if (OriginalAddress >= 0xffffffff00000000 && NewAddress < 0xffffffff)
3522       NewAddress = NewAddress + 0xffffffff00000000;
3523 
3524     if (OriginalAddress == NewAddress)
3525       continue;
3526 
3527     for (LKInstructionMarkerInfo &LKMarkerInfo : LKMarkerInfoKV.second) {
3528       StringRef SectionName = LKMarkerInfo.SectionName;
3529       SimpleBinaryPatcher *LKPatcher;
3530       ErrorOr<BinarySection &> BSec = BC->getUniqueSectionByName(SectionName);
3531       assert(BSec && "missing section info for kernel section");
3532       if (!BSec->getPatcher())
3533         BSec->registerPatcher(std::make_unique<SimpleBinaryPatcher>());
3534       LKPatcher = static_cast<SimpleBinaryPatcher *>(BSec->getPatcher());
3535       PatchCounts[std::string(SectionName)]++;
3536       if (LKMarkerInfo.IsPCRelative)
3537         LKPatcher->addLE32Patch(LKMarkerInfo.SectionOffset,
3538                                 NewAddress - OriginalAddress +
3539                                     LKMarkerInfo.PCRelativeOffset);
3540       else
3541         LKPatcher->addLE64Patch(LKMarkerInfo.SectionOffset, NewAddress);
3542     }
3543   }
3544   outs() << "BOLT-INFO: patching linux kernel sections. Total patches per "
3545             "section are as follows:\n";
3546   for (const std::pair<const std::string, uint64_t> &KV : PatchCounts)
3547     outs() << "  Section: " << KV.first << ", patch-counts: " << KV.second
3548            << '\n';
3549 }
3550 
3551 void RewriteInstance::mapFileSections(RuntimeDyld &RTDyld) {
3552   mapCodeSections(RTDyld);
3553   mapDataSections(RTDyld);
3554 }
3555 
3556 std::vector<BinarySection *> RewriteInstance::getCodeSections() {
3557   std::vector<BinarySection *> CodeSections;
3558   for (BinarySection &Section : BC->textSections())
3559     if (Section.hasValidSectionID())
3560       CodeSections.emplace_back(&Section);
3561 
3562   auto compareSections = [&](const BinarySection *A, const BinarySection *B) {
3563     // Place movers before anything else.
3564     if (A->getName() == BC->getHotTextMoverSectionName())
3565       return true;
3566     if (B->getName() == BC->getHotTextMoverSectionName())
3567       return false;
3568 
3569     // Depending on the option, put main text at the beginning or at the end.
3570     if (opts::HotFunctionsAtEnd)
3571       return B->getName() == BC->getMainCodeSectionName();
3572     else
3573       return A->getName() == BC->getMainCodeSectionName();
3574   };
3575 
3576   // Determine the order of sections.
3577   std::stable_sort(CodeSections.begin(), CodeSections.end(), compareSections);
3578 
3579   return CodeSections;
3580 }
3581 
3582 void RewriteInstance::mapCodeSections(RuntimeDyld &RTDyld) {
3583   if (BC->HasRelocations) {
3584     ErrorOr<BinarySection &> TextSection =
3585         BC->getUniqueSectionByName(BC->getMainCodeSectionName());
3586     assert(TextSection && ".text section not found in output");
3587     assert(TextSection->hasValidSectionID() && ".text section should be valid");
3588 
3589     // Map sections for functions with pre-assigned addresses.
3590     for (BinaryFunction *InjectedFunction : BC->getInjectedBinaryFunctions()) {
3591       const uint64_t OutputAddress = InjectedFunction->getOutputAddress();
3592       if (!OutputAddress)
3593         continue;
3594 
3595       ErrorOr<BinarySection &> FunctionSection =
3596           InjectedFunction->getCodeSection();
3597       assert(FunctionSection && "function should have section");
3598       FunctionSection->setOutputAddress(OutputAddress);
3599       RTDyld.reassignSectionAddress(FunctionSection->getSectionID(),
3600                                     OutputAddress);
3601       InjectedFunction->setImageAddress(FunctionSection->getAllocAddress());
3602       InjectedFunction->setImageSize(FunctionSection->getOutputSize());
3603     }
3604 
3605     // Populate the list of sections to be allocated.
3606     std::vector<BinarySection *> CodeSections = getCodeSections();
3607 
3608     // Remove sections that were pre-allocated (patch sections).
3609     CodeSections.erase(
3610         std::remove_if(CodeSections.begin(), CodeSections.end(),
3611                        [](BinarySection *Section) {
3612                          return Section->getOutputAddress();
3613                        }),
3614         CodeSections.end());
3615     LLVM_DEBUG(dbgs() << "Code sections in the order of output:\n";
3616       for (const BinarySection *Section : CodeSections)
3617         dbgs() << Section->getName() << '\n';
3618     );
3619 
3620     uint64_t PaddingSize = 0; // size of padding required at the end
3621 
3622     // Allocate sections starting at a given Address.
3623     auto allocateAt = [&](uint64_t Address) {
3624       for (BinarySection *Section : CodeSections) {
3625         Address = alignTo(Address, Section->getAlignment());
3626         Section->setOutputAddress(Address);
3627         Address += Section->getOutputSize();
3628       }
3629 
3630       // Make sure we allocate enough space for huge pages.
3631       if (opts::HotText) {
3632         uint64_t HotTextEnd =
3633             TextSection->getOutputAddress() + TextSection->getOutputSize();
3634         HotTextEnd = alignTo(HotTextEnd, BC->PageAlign);
3635         if (HotTextEnd > Address) {
3636           PaddingSize = HotTextEnd - Address;
3637           Address = HotTextEnd;
3638         }
3639       }
3640       return Address;
3641     };
3642 
3643     // Check if we can fit code in the original .text
3644     bool AllocationDone = false;
3645     if (opts::UseOldText) {
3646       const uint64_t CodeSize =
3647           allocateAt(BC->OldTextSectionAddress) - BC->OldTextSectionAddress;
3648 
3649       if (CodeSize <= BC->OldTextSectionSize) {
3650         outs() << "BOLT-INFO: using original .text for new code with 0x"
3651                << Twine::utohexstr(opts::AlignText) << " alignment\n";
3652         AllocationDone = true;
3653       } else {
3654         errs() << "BOLT-WARNING: original .text too small to fit the new code"
3655                << " using 0x" << Twine::utohexstr(opts::AlignText)
3656                << " alignment. " << CodeSize << " bytes needed, have "
3657                << BC->OldTextSectionSize << " bytes available.\n";
3658         opts::UseOldText = false;
3659       }
3660     }
3661 
3662     if (!AllocationDone)
3663       NextAvailableAddress = allocateAt(NextAvailableAddress);
3664 
3665     // Do the mapping for ORC layer based on the allocation.
3666     for (BinarySection *Section : CodeSections) {
3667       LLVM_DEBUG(
3668           dbgs() << "BOLT: mapping " << Section->getName() << " at 0x"
3669                  << Twine::utohexstr(Section->getAllocAddress()) << " to 0x"
3670                  << Twine::utohexstr(Section->getOutputAddress()) << '\n');
3671       RTDyld.reassignSectionAddress(Section->getSectionID(),
3672                                     Section->getOutputAddress());
3673       Section->setOutputFileOffset(
3674           getFileOffsetForAddress(Section->getOutputAddress()));
3675     }
3676 
3677     // Check if we need to insert a padding section for hot text.
3678     if (PaddingSize && !opts::UseOldText)
3679       outs() << "BOLT-INFO: padding code to 0x"
3680              << Twine::utohexstr(NextAvailableAddress)
3681              << " to accommodate hot text\n";
3682 
3683     return;
3684   }
3685 
3686   // Processing in non-relocation mode.
3687   uint64_t NewTextSectionStartAddress = NextAvailableAddress;
3688 
3689   for (auto &BFI : BC->getBinaryFunctions()) {
3690     BinaryFunction &Function = BFI.second;
3691     if (!Function.isEmitted())
3692       continue;
3693 
3694     bool TooLarge = false;
3695     ErrorOr<BinarySection &> FuncSection = Function.getCodeSection();
3696     assert(FuncSection && "cannot find section for function");
3697     FuncSection->setOutputAddress(Function.getAddress());
3698     LLVM_DEBUG(dbgs() << "BOLT: mapping 0x"
3699                       << Twine::utohexstr(FuncSection->getAllocAddress())
3700                       << " to 0x" << Twine::utohexstr(Function.getAddress())
3701                       << '\n');
3702     RTDyld.reassignSectionAddress(FuncSection->getSectionID(),
3703                                   Function.getAddress());
3704     Function.setImageAddress(FuncSection->getAllocAddress());
3705     Function.setImageSize(FuncSection->getOutputSize());
3706     if (Function.getImageSize() > Function.getMaxSize()) {
3707       TooLarge = true;
3708       FailedAddresses.emplace_back(Function.getAddress());
3709     }
3710 
3711     // Map jump tables if updating in-place.
3712     if (opts::JumpTables == JTS_BASIC) {
3713       for (auto &JTI : Function.JumpTables) {
3714         JumpTable *JT = JTI.second;
3715         BinarySection &Section = JT->getOutputSection();
3716         Section.setOutputAddress(JT->getAddress());
3717         Section.setOutputFileOffset(getFileOffsetForAddress(JT->getAddress()));
3718         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: mapping " << Section.getName()
3719                           << " to 0x" << Twine::utohexstr(JT->getAddress())
3720                           << '\n');
3721         RTDyld.reassignSectionAddress(Section.getSectionID(), JT->getAddress());
3722       }
3723     }
3724 
3725     if (!Function.isSplit())
3726       continue;
3727 
3728     ErrorOr<BinarySection &> ColdSection = Function.getColdCodeSection();
3729     assert(ColdSection && "cannot find section for cold part");
3730     // Cold fragments are aligned at 16 bytes.
3731     NextAvailableAddress = alignTo(NextAvailableAddress, 16);
3732     BinaryFunction::FragmentInfo &ColdPart = Function.cold();
3733     if (TooLarge) {
3734       // The corresponding FDE will refer to address 0.
3735       ColdPart.setAddress(0);
3736       ColdPart.setImageAddress(0);
3737       ColdPart.setImageSize(0);
3738       ColdPart.setFileOffset(0);
3739     } else {
3740       ColdPart.setAddress(NextAvailableAddress);
3741       ColdPart.setImageAddress(ColdSection->getAllocAddress());
3742       ColdPart.setImageSize(ColdSection->getOutputSize());
3743       ColdPart.setFileOffset(getFileOffsetForAddress(NextAvailableAddress));
3744       ColdSection->setOutputAddress(ColdPart.getAddress());
3745     }
3746 
3747     LLVM_DEBUG(dbgs() << "BOLT: mapping cold fragment 0x"
3748                       << Twine::utohexstr(ColdPart.getImageAddress())
3749                       << " to 0x" << Twine::utohexstr(ColdPart.getAddress())
3750                       << " with size "
3751                       << Twine::utohexstr(ColdPart.getImageSize()) << '\n');
3752     RTDyld.reassignSectionAddress(ColdSection->getSectionID(),
3753                                   ColdPart.getAddress());
3754 
3755     NextAvailableAddress += ColdPart.getImageSize();
3756   }
3757 
3758   // Add the new text section aggregating all existing code sections.
3759   // This is pseudo-section that serves a purpose of creating a corresponding
3760   // entry in section header table.
3761   int64_t NewTextSectionSize =
3762       NextAvailableAddress - NewTextSectionStartAddress;
3763   if (NewTextSectionSize) {
3764     const unsigned Flags = BinarySection::getFlags(/*IsReadOnly=*/true,
3765                                                    /*IsText=*/true,
3766                                                    /*IsAllocatable=*/true);
3767     BinarySection &Section =
3768       BC->registerOrUpdateSection(getBOLTTextSectionName(),
3769                                   ELF::SHT_PROGBITS,
3770                                   Flags,
3771                                   /*Data=*/nullptr,
3772                                   NewTextSectionSize,
3773                                   16);
3774     Section.setOutputAddress(NewTextSectionStartAddress);
3775     Section.setOutputFileOffset(
3776         getFileOffsetForAddress(NewTextSectionStartAddress));
3777   }
3778 }
3779 
3780 void RewriteInstance::mapDataSections(RuntimeDyld &RTDyld) {
3781   // Map special sections to their addresses in the output image.
3782   // These are the sections that we generate via MCStreamer.
3783   // The order is important.
3784   std::vector<std::string> Sections = {
3785       ".eh_frame", Twine(getOrgSecPrefix(), ".eh_frame").str(),
3786       ".gcc_except_table", ".rodata", ".rodata.cold"};
3787   if (RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary())
3788     RtLibrary->addRuntimeLibSections(Sections);
3789 
3790   for (std::string &SectionName : Sections) {
3791     ErrorOr<BinarySection &> Section = BC->getUniqueSectionByName(SectionName);
3792     if (!Section || !Section->isAllocatable() || !Section->isFinalized())
3793       continue;
3794     NextAvailableAddress =
3795         alignTo(NextAvailableAddress, Section->getAlignment());
3796     LLVM_DEBUG(dbgs() << "BOLT: mapping section " << SectionName << " (0x"
3797                       << Twine::utohexstr(Section->getAllocAddress())
3798                       << ") to 0x" << Twine::utohexstr(NextAvailableAddress)
3799                       << ":0x"
3800                       << Twine::utohexstr(NextAvailableAddress +
3801                                           Section->getOutputSize())
3802                       << '\n');
3803 
3804     RTDyld.reassignSectionAddress(Section->getSectionID(),
3805                                   NextAvailableAddress);
3806     Section->setOutputAddress(NextAvailableAddress);
3807     Section->setOutputFileOffset(getFileOffsetForAddress(NextAvailableAddress));
3808 
3809     NextAvailableAddress += Section->getOutputSize();
3810   }
3811 
3812   // Handling for sections with relocations.
3813   for (BinarySection &Section : BC->sections()) {
3814     if (!Section.hasSectionRef())
3815       continue;
3816 
3817     StringRef SectionName = Section.getName();
3818     ErrorOr<BinarySection &> OrgSection =
3819         BC->getUniqueSectionByName((getOrgSecPrefix() + SectionName).str());
3820     if (!OrgSection ||
3821         !OrgSection->isAllocatable() ||
3822         !OrgSection->isFinalized() ||
3823         !OrgSection->hasValidSectionID())
3824       continue;
3825 
3826     if (OrgSection->getOutputAddress()) {
3827       LLVM_DEBUG(dbgs() << "BOLT-DEBUG: section " << SectionName
3828                         << " is already mapped at 0x"
3829                         << Twine::utohexstr(OrgSection->getOutputAddress())
3830                         << '\n');
3831       continue;
3832     }
3833     LLVM_DEBUG(
3834         dbgs() << "BOLT: mapping original section " << SectionName << " (0x"
3835                << Twine::utohexstr(OrgSection->getAllocAddress()) << ") to 0x"
3836                << Twine::utohexstr(Section.getAddress()) << '\n');
3837 
3838     RTDyld.reassignSectionAddress(OrgSection->getSectionID(),
3839                                   Section.getAddress());
3840 
3841     OrgSection->setOutputAddress(Section.getAddress());
3842     OrgSection->setOutputFileOffset(Section.getContents().data() -
3843                                     InputFile->getData().data());
3844   }
3845 }
3846 
3847 void RewriteInstance::mapExtraSections(RuntimeDyld &RTDyld) {
3848   for (BinarySection &Section : BC->allocatableSections()) {
3849     if (Section.getOutputAddress() || !Section.hasValidSectionID())
3850       continue;
3851     NextAvailableAddress =
3852         alignTo(NextAvailableAddress, Section.getAlignment());
3853     Section.setOutputAddress(NextAvailableAddress);
3854     NextAvailableAddress += Section.getOutputSize();
3855 
3856     LLVM_DEBUG(dbgs() << "BOLT: (extra) mapping " << Section.getName()
3857                       << " at 0x" << Twine::utohexstr(Section.getAllocAddress())
3858                       << " to 0x"
3859                       << Twine::utohexstr(Section.getOutputAddress()) << '\n');
3860 
3861     RTDyld.reassignSectionAddress(Section.getSectionID(),
3862                                   Section.getOutputAddress());
3863     Section.setOutputFileOffset(
3864         getFileOffsetForAddress(Section.getOutputAddress()));
3865   }
3866 }
3867 
3868 void RewriteInstance::updateOutputValues(const MCAsmLayout &Layout) {
3869   for (BinaryFunction *Function : BC->getAllBinaryFunctions())
3870     Function->updateOutputValues(Layout);
3871 }
3872 
3873 void RewriteInstance::patchELFPHDRTable() {
3874   auto ELF64LEFile = dyn_cast<ELF64LEObjectFile>(InputFile);
3875   if (!ELF64LEFile) {
3876     errs() << "BOLT-ERROR: only 64-bit LE ELF binaries are supported\n";
3877     exit(1);
3878   }
3879   const ELFFile<ELF64LE> &Obj = ELF64LEFile->getELFFile();
3880   raw_fd_ostream &OS = Out->os();
3881 
3882   // Write/re-write program headers.
3883   Phnum = Obj.getHeader().e_phnum;
3884   if (PHDRTableOffset) {
3885     // Writing new pheader table.
3886     Phnum += 1; // only adding one new segment
3887     // Segment size includes the size of the PHDR area.
3888     NewTextSegmentSize = NextAvailableAddress - PHDRTableAddress;
3889   } else {
3890     assert(!PHDRTableAddress && "unexpected address for program header table");
3891     // Update existing table.
3892     PHDRTableOffset = Obj.getHeader().e_phoff;
3893     NewTextSegmentSize = NextAvailableAddress - NewTextSegmentAddress;
3894   }
3895   OS.seek(PHDRTableOffset);
3896 
3897   bool ModdedGnuStack = false;
3898   (void)ModdedGnuStack;
3899   bool AddedSegment = false;
3900   (void)AddedSegment;
3901 
3902   auto createNewTextPhdr = [&]() {
3903     ELF64LEPhdrTy NewPhdr;
3904     NewPhdr.p_type = ELF::PT_LOAD;
3905     if (PHDRTableAddress) {
3906       NewPhdr.p_offset = PHDRTableOffset;
3907       NewPhdr.p_vaddr = PHDRTableAddress;
3908       NewPhdr.p_paddr = PHDRTableAddress;
3909     } else {
3910       NewPhdr.p_offset = NewTextSegmentOffset;
3911       NewPhdr.p_vaddr = NewTextSegmentAddress;
3912       NewPhdr.p_paddr = NewTextSegmentAddress;
3913     }
3914     NewPhdr.p_filesz = NewTextSegmentSize;
3915     NewPhdr.p_memsz = NewTextSegmentSize;
3916     NewPhdr.p_flags = ELF::PF_X | ELF::PF_R;
3917     // FIXME: Currently instrumentation is experimental and the runtime data
3918     // is emitted with code, thus everything needs to be writable
3919     if (opts::Instrument)
3920       NewPhdr.p_flags |= ELF::PF_W;
3921     NewPhdr.p_align = BC->PageAlign;
3922 
3923     return NewPhdr;
3924   };
3925 
3926   // Copy existing program headers with modifications.
3927   for (const ELF64LE::Phdr &Phdr : cantFail(Obj.program_headers())) {
3928     ELF64LE::Phdr NewPhdr = Phdr;
3929     if (PHDRTableAddress && Phdr.p_type == ELF::PT_PHDR) {
3930       NewPhdr.p_offset = PHDRTableOffset;
3931       NewPhdr.p_vaddr = PHDRTableAddress;
3932       NewPhdr.p_paddr = PHDRTableAddress;
3933       NewPhdr.p_filesz = sizeof(NewPhdr) * Phnum;
3934       NewPhdr.p_memsz = sizeof(NewPhdr) * Phnum;
3935     } else if (Phdr.p_type == ELF::PT_GNU_EH_FRAME) {
3936       ErrorOr<BinarySection &> EHFrameHdrSec =
3937           BC->getUniqueSectionByName(".eh_frame_hdr");
3938       if (EHFrameHdrSec && EHFrameHdrSec->isAllocatable() &&
3939           EHFrameHdrSec->isFinalized()) {
3940         NewPhdr.p_offset = EHFrameHdrSec->getOutputFileOffset();
3941         NewPhdr.p_vaddr = EHFrameHdrSec->getOutputAddress();
3942         NewPhdr.p_paddr = EHFrameHdrSec->getOutputAddress();
3943         NewPhdr.p_filesz = EHFrameHdrSec->getOutputSize();
3944         NewPhdr.p_memsz = EHFrameHdrSec->getOutputSize();
3945       }
3946     } else if (opts::UseGnuStack && Phdr.p_type == ELF::PT_GNU_STACK) {
3947       NewPhdr = createNewTextPhdr();
3948       ModdedGnuStack = true;
3949     } else if (!opts::UseGnuStack && Phdr.p_type == ELF::PT_DYNAMIC) {
3950       // Insert the new header before DYNAMIC.
3951       ELF64LE::Phdr NewTextPhdr = createNewTextPhdr();
3952       OS.write(reinterpret_cast<const char *>(&NewTextPhdr),
3953                sizeof(NewTextPhdr));
3954       AddedSegment = true;
3955     }
3956     OS.write(reinterpret_cast<const char *>(&NewPhdr), sizeof(NewPhdr));
3957   }
3958 
3959   if (!opts::UseGnuStack && !AddedSegment) {
3960     // Append the new header to the end of the table.
3961     ELF64LE::Phdr NewTextPhdr = createNewTextPhdr();
3962     OS.write(reinterpret_cast<const char *>(&NewTextPhdr), sizeof(NewTextPhdr));
3963   }
3964 
3965   assert((!opts::UseGnuStack || ModdedGnuStack) &&
3966          "could not find GNU_STACK program header to modify");
3967 }
3968 
3969 namespace {
3970 
3971 /// Write padding to \p OS such that its current \p Offset becomes aligned
3972 /// at \p Alignment. Return new (aligned) offset.
3973 uint64_t appendPadding(raw_pwrite_stream &OS, uint64_t Offset,
3974                        uint64_t Alignment) {
3975   if (!Alignment)
3976     return Offset;
3977 
3978   const uint64_t PaddingSize =
3979       offsetToAlignment(Offset, llvm::Align(Alignment));
3980   for (unsigned I = 0; I < PaddingSize; ++I)
3981     OS.write((unsigned char)0);
3982   return Offset + PaddingSize;
3983 }
3984 
3985 }
3986 
3987 void RewriteInstance::rewriteNoteSections() {
3988   auto ELF64LEFile = dyn_cast<ELF64LEObjectFile>(InputFile);
3989   if (!ELF64LEFile) {
3990     errs() << "BOLT-ERROR: only 64-bit LE ELF binaries are supported\n";
3991     exit(1);
3992   }
3993   const ELFFile<ELF64LE> &Obj = ELF64LEFile->getELFFile();
3994   raw_fd_ostream &OS = Out->os();
3995 
3996   uint64_t NextAvailableOffset = getFileOffsetForAddress(NextAvailableAddress);
3997   assert(NextAvailableOffset >= FirstNonAllocatableOffset &&
3998          "next available offset calculation failure");
3999   OS.seek(NextAvailableOffset);
4000 
4001   // Copy over non-allocatable section contents and update file offsets.
4002   for (const ELF64LE::Shdr &Section : cantFail(Obj.sections())) {
4003     if (Section.sh_type == ELF::SHT_NULL)
4004       continue;
4005     if (Section.sh_flags & ELF::SHF_ALLOC)
4006       continue;
4007 
4008     StringRef SectionName =
4009         cantFail(Obj.getSectionName(Section), "cannot get section name");
4010     ErrorOr<BinarySection &> BSec = BC->getUniqueSectionByName(SectionName);
4011 
4012     if (shouldStrip(Section, SectionName))
4013       continue;
4014 
4015     // Insert padding as needed.
4016     NextAvailableOffset =
4017         appendPadding(OS, NextAvailableOffset, Section.sh_addralign);
4018 
4019     // New section size.
4020     uint64_t Size = 0;
4021     bool DataWritten = false;
4022     uint8_t *SectionData = nullptr;
4023     // Copy over section contents unless it's one of the sections we overwrite.
4024     if (!willOverwriteSection(SectionName)) {
4025       Size = Section.sh_size;
4026       StringRef Dataref = InputFile->getData().substr(Section.sh_offset, Size);
4027       std::string Data;
4028       if (BSec && BSec->getPatcher()) {
4029         Data = BSec->getPatcher()->patchBinary(Dataref);
4030         Dataref = StringRef(Data);
4031       }
4032 
4033       // Section was expanded, so need to treat it as overwrite.
4034       if (Size != Dataref.size()) {
4035         BSec = BC->registerOrUpdateNoteSection(
4036             SectionName, copyByteArray(Dataref), Dataref.size());
4037         Size = 0;
4038       } else {
4039         OS << Dataref;
4040         DataWritten = true;
4041 
4042         // Add padding as the section extension might rely on the alignment.
4043         Size = appendPadding(OS, Size, Section.sh_addralign);
4044       }
4045     }
4046 
4047     // Perform section post-processing.
4048     if (BSec && !BSec->isAllocatable()) {
4049       assert(BSec->getAlignment() <= Section.sh_addralign &&
4050              "alignment exceeds value in file");
4051 
4052       if (BSec->getAllocAddress()) {
4053         assert(!DataWritten && "Writing section twice.");
4054         (void)DataWritten;
4055         SectionData = BSec->getOutputData();
4056 
4057         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: " << (Size ? "appending" : "writing")
4058                           << " contents to section " << SectionName << '\n');
4059         OS.write(reinterpret_cast<char *>(SectionData), BSec->getOutputSize());
4060         Size += BSec->getOutputSize();
4061       }
4062 
4063       BSec->setOutputFileOffset(NextAvailableOffset);
4064       BSec->flushPendingRelocations(OS,
4065         [this] (const MCSymbol *S) {
4066           return getNewValueForSymbol(S->getName());
4067         });
4068     }
4069 
4070     // Set/modify section info.
4071     BinarySection &NewSection =
4072       BC->registerOrUpdateNoteSection(SectionName,
4073                                       SectionData,
4074                                       Size,
4075                                       Section.sh_addralign,
4076                                       BSec ? BSec->isReadOnly() : false,
4077                                       BSec ? BSec->getELFType()
4078                                            : ELF::SHT_PROGBITS);
4079     NewSection.setOutputAddress(0);
4080     NewSection.setOutputFileOffset(NextAvailableOffset);
4081 
4082     NextAvailableOffset += Size;
4083   }
4084 
4085   // Write new note sections.
4086   for (BinarySection &Section : BC->nonAllocatableSections()) {
4087     if (Section.getOutputFileOffset() || !Section.getAllocAddress())
4088       continue;
4089 
4090     assert(!Section.hasPendingRelocations() && "cannot have pending relocs");
4091 
4092     NextAvailableOffset =
4093         appendPadding(OS, NextAvailableOffset, Section.getAlignment());
4094     Section.setOutputFileOffset(NextAvailableOffset);
4095 
4096     LLVM_DEBUG(
4097         dbgs() << "BOLT-DEBUG: writing out new section " << Section.getName()
4098                << " of size " << Section.getOutputSize() << " at offset 0x"
4099                << Twine::utohexstr(Section.getOutputFileOffset()) << '\n');
4100 
4101     OS.write(Section.getOutputContents().data(), Section.getOutputSize());
4102     NextAvailableOffset += Section.getOutputSize();
4103   }
4104 }
4105 
4106 template <typename ELFT>
4107 void RewriteInstance::finalizeSectionStringTable(ELFObjectFile<ELFT> *File) {
4108   using ELFShdrTy = typename ELFT::Shdr;
4109   const ELFFile<ELFT> &Obj = File->getELFFile();
4110 
4111   // Pre-populate section header string table.
4112   for (const ELFShdrTy &Section : cantFail(Obj.sections())) {
4113     StringRef SectionName =
4114         cantFail(Obj.getSectionName(Section), "cannot get section name");
4115     SHStrTab.add(SectionName);
4116     std::string OutputSectionName = getOutputSectionName(Obj, Section);
4117     if (OutputSectionName != SectionName)
4118       SHStrTabPool.emplace_back(std::move(OutputSectionName));
4119   }
4120   for (const std::string &Str : SHStrTabPool)
4121     SHStrTab.add(Str);
4122   for (const BinarySection &Section : BC->sections())
4123     SHStrTab.add(Section.getName());
4124   SHStrTab.finalize();
4125 
4126   const size_t SHStrTabSize = SHStrTab.getSize();
4127   uint8_t *DataCopy = new uint8_t[SHStrTabSize];
4128   memset(DataCopy, 0, SHStrTabSize);
4129   SHStrTab.write(DataCopy);
4130   BC->registerOrUpdateNoteSection(".shstrtab",
4131                                   DataCopy,
4132                                   SHStrTabSize,
4133                                   /*Alignment=*/1,
4134                                   /*IsReadOnly=*/true,
4135                                   ELF::SHT_STRTAB);
4136 }
4137 
4138 void RewriteInstance::addBoltInfoSection() {
4139   std::string DescStr;
4140   raw_string_ostream DescOS(DescStr);
4141 
4142   DescOS << "BOLT revision: " << BoltRevision << ", "
4143          << "command line:";
4144   for (int I = 0; I < Argc; ++I)
4145     DescOS << " " << Argv[I];
4146   DescOS.flush();
4147 
4148   // Encode as GNU GOLD VERSION so it is easily printable by 'readelf -n'
4149   const std::string BoltInfo =
4150       BinarySection::encodeELFNote("GNU", DescStr, 4 /*NT_GNU_GOLD_VERSION*/);
4151   BC->registerOrUpdateNoteSection(".note.bolt_info", copyByteArray(BoltInfo),
4152                                   BoltInfo.size(),
4153                                   /*Alignment=*/1,
4154                                   /*IsReadOnly=*/true, ELF::SHT_NOTE);
4155 }
4156 
4157 void RewriteInstance::addBATSection() {
4158   BC->registerOrUpdateNoteSection(BoltAddressTranslation::SECTION_NAME, nullptr,
4159                                   0,
4160                                   /*Alignment=*/1,
4161                                   /*IsReadOnly=*/true, ELF::SHT_NOTE);
4162 }
4163 
4164 void RewriteInstance::encodeBATSection() {
4165   std::string DescStr;
4166   raw_string_ostream DescOS(DescStr);
4167 
4168   BAT->write(DescOS);
4169   DescOS.flush();
4170 
4171   const std::string BoltInfo =
4172       BinarySection::encodeELFNote("BOLT", DescStr, BinarySection::NT_BOLT_BAT);
4173   BC->registerOrUpdateNoteSection(BoltAddressTranslation::SECTION_NAME,
4174                                   copyByteArray(BoltInfo), BoltInfo.size(),
4175                                   /*Alignment=*/1,
4176                                   /*IsReadOnly=*/true, ELF::SHT_NOTE);
4177 }
4178 
4179 template <typename ELFObjType, typename ELFShdrTy>
4180 std::string RewriteInstance::getOutputSectionName(const ELFObjType &Obj,
4181                                                   const ELFShdrTy &Section) {
4182   if (Section.sh_type == ELF::SHT_NULL)
4183     return "";
4184 
4185   StringRef SectionName =
4186       cantFail(Obj.getSectionName(Section), "cannot get section name");
4187 
4188   if ((Section.sh_flags & ELF::SHF_ALLOC) && willOverwriteSection(SectionName))
4189     return (getOrgSecPrefix() + SectionName).str();
4190 
4191   return std::string(SectionName);
4192 }
4193 
4194 template <typename ELFShdrTy>
4195 bool RewriteInstance::shouldStrip(const ELFShdrTy &Section,
4196                                   StringRef SectionName) {
4197   // Strip non-allocatable relocation sections.
4198   if (!(Section.sh_flags & ELF::SHF_ALLOC) && Section.sh_type == ELF::SHT_RELA)
4199     return true;
4200 
4201   // Strip debug sections if not updating them.
4202   if (isDebugSection(SectionName) && !opts::UpdateDebugSections)
4203     return true;
4204 
4205   // Strip symtab section if needed
4206   if (opts::RemoveSymtab && Section.sh_type == ELF::SHT_SYMTAB)
4207     return true;
4208 
4209   return false;
4210 }
4211 
4212 template <typename ELFT>
4213 std::vector<typename object::ELFObjectFile<ELFT>::Elf_Shdr>
4214 RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,
4215                                    std::vector<uint32_t> &NewSectionIndex) {
4216   using ELFShdrTy = typename ELFObjectFile<ELFT>::Elf_Shdr;
4217   const ELFFile<ELFT> &Obj = File->getELFFile();
4218   typename ELFT::ShdrRange Sections = cantFail(Obj.sections());
4219 
4220   // Keep track of section header entries together with their name.
4221   std::vector<std::pair<std::string, ELFShdrTy>> OutputSections;
4222   auto addSection = [&](const std::string &Name, const ELFShdrTy &Section) {
4223     ELFShdrTy NewSection = Section;
4224     NewSection.sh_name = SHStrTab.getOffset(Name);
4225     OutputSections.emplace_back(Name, std::move(NewSection));
4226   };
4227 
4228   // Copy over entries for original allocatable sections using modified name.
4229   for (const ELFShdrTy &Section : Sections) {
4230     // Always ignore this section.
4231     if (Section.sh_type == ELF::SHT_NULL) {
4232       OutputSections.emplace_back("", Section);
4233       continue;
4234     }
4235 
4236     if (!(Section.sh_flags & ELF::SHF_ALLOC))
4237       continue;
4238 
4239     addSection(getOutputSectionName(Obj, Section), Section);
4240   }
4241 
4242   for (const BinarySection &Section : BC->allocatableSections()) {
4243     if (!Section.isFinalized())
4244       continue;
4245 
4246     if (Section.getName().startswith(getOrgSecPrefix()) ||
4247         Section.isAnonymous()) {
4248       if (opts::Verbosity)
4249         outs() << "BOLT-INFO: not writing section header for section "
4250                << Section.getName() << '\n';
4251       continue;
4252     }
4253 
4254     if (opts::Verbosity >= 1)
4255       outs() << "BOLT-INFO: writing section header for " << Section.getName()
4256              << '\n';
4257     ELFShdrTy NewSection;
4258     NewSection.sh_type = ELF::SHT_PROGBITS;
4259     NewSection.sh_addr = Section.getOutputAddress();
4260     NewSection.sh_offset = Section.getOutputFileOffset();
4261     NewSection.sh_size = Section.getOutputSize();
4262     NewSection.sh_entsize = 0;
4263     NewSection.sh_flags = Section.getELFFlags();
4264     NewSection.sh_link = 0;
4265     NewSection.sh_info = 0;
4266     NewSection.sh_addralign = Section.getAlignment();
4267     addSection(std::string(Section.getName()), NewSection);
4268   }
4269 
4270   // Sort all allocatable sections by their offset.
4271   std::stable_sort(OutputSections.begin(), OutputSections.end(),
4272       [] (const std::pair<std::string, ELFShdrTy> &A,
4273           const std::pair<std::string, ELFShdrTy> &B) {
4274         return A.second.sh_offset < B.second.sh_offset;
4275       });
4276 
4277   // Fix section sizes to prevent overlapping.
4278   ELFShdrTy *PrevSection = nullptr;
4279   StringRef PrevSectionName;
4280   for (auto &SectionKV : OutputSections) {
4281     ELFShdrTy &Section = SectionKV.second;
4282 
4283     // TBSS section does not take file or memory space. Ignore it for layout
4284     // purposes.
4285     if (Section.sh_type == ELF::SHT_NOBITS && (Section.sh_flags & ELF::SHF_TLS))
4286       continue;
4287 
4288     if (PrevSection &&
4289         PrevSection->sh_addr + PrevSection->sh_size > Section.sh_addr) {
4290       if (opts::Verbosity > 1)
4291         outs() << "BOLT-INFO: adjusting size for section " << PrevSectionName
4292                << '\n';
4293       PrevSection->sh_size = Section.sh_addr > PrevSection->sh_addr
4294                                  ? Section.sh_addr - PrevSection->sh_addr
4295                                  : 0;
4296     }
4297 
4298     PrevSection = &Section;
4299     PrevSectionName = SectionKV.first;
4300   }
4301 
4302   uint64_t LastFileOffset = 0;
4303 
4304   // Copy over entries for non-allocatable sections performing necessary
4305   // adjustments.
4306   for (const ELFShdrTy &Section : Sections) {
4307     if (Section.sh_type == ELF::SHT_NULL)
4308       continue;
4309     if (Section.sh_flags & ELF::SHF_ALLOC)
4310       continue;
4311 
4312     StringRef SectionName =
4313         cantFail(Obj.getSectionName(Section), "cannot get section name");
4314 
4315     if (shouldStrip(Section, SectionName))
4316       continue;
4317 
4318     ErrorOr<BinarySection &> BSec = BC->getUniqueSectionByName(SectionName);
4319     assert(BSec && "missing section info for non-allocatable section");
4320 
4321     ELFShdrTy NewSection = Section;
4322     NewSection.sh_offset = BSec->getOutputFileOffset();
4323     NewSection.sh_size = BSec->getOutputSize();
4324 
4325     if (NewSection.sh_type == ELF::SHT_SYMTAB)
4326       NewSection.sh_info = NumLocalSymbols;
4327 
4328     addSection(std::string(SectionName), NewSection);
4329 
4330     LastFileOffset = BSec->getOutputFileOffset();
4331   }
4332 
4333   // Create entries for new non-allocatable sections.
4334   for (BinarySection &Section : BC->nonAllocatableSections()) {
4335     if (Section.getOutputFileOffset() <= LastFileOffset)
4336       continue;
4337 
4338     if (opts::Verbosity >= 1)
4339       outs() << "BOLT-INFO: writing section header for " << Section.getName()
4340              << '\n';
4341 
4342     ELFShdrTy NewSection;
4343     NewSection.sh_type = Section.getELFType();
4344     NewSection.sh_addr = 0;
4345     NewSection.sh_offset = Section.getOutputFileOffset();
4346     NewSection.sh_size = Section.getOutputSize();
4347     NewSection.sh_entsize = 0;
4348     NewSection.sh_flags = Section.getELFFlags();
4349     NewSection.sh_link = 0;
4350     NewSection.sh_info = 0;
4351     NewSection.sh_addralign = Section.getAlignment();
4352 
4353     addSection(std::string(Section.getName()), NewSection);
4354   }
4355 
4356   // Assign indices to sections.
4357   std::unordered_map<std::string, uint64_t> NameToIndex;
4358   for (uint32_t Index = 1; Index < OutputSections.size(); ++Index) {
4359     const std::string &SectionName = OutputSections[Index].first;
4360     NameToIndex[SectionName] = Index;
4361     if (ErrorOr<BinarySection &> Section =
4362             BC->getUniqueSectionByName(SectionName))
4363       Section->setIndex(Index);
4364   }
4365 
4366   // Update section index mapping
4367   NewSectionIndex.clear();
4368   NewSectionIndex.resize(Sections.size(), 0);
4369   for (const ELFShdrTy &Section : Sections) {
4370     if (Section.sh_type == ELF::SHT_NULL)
4371       continue;
4372 
4373     size_t OrgIndex = std::distance(Sections.begin(), &Section);
4374     std::string SectionName = getOutputSectionName(Obj, Section);
4375 
4376     // Some sections are stripped
4377     if (!NameToIndex.count(SectionName))
4378       continue;
4379 
4380     NewSectionIndex[OrgIndex] = NameToIndex[SectionName];
4381   }
4382 
4383   std::vector<ELFShdrTy> SectionsOnly(OutputSections.size());
4384   std::transform(OutputSections.begin(), OutputSections.end(),
4385                  SectionsOnly.begin(),
4386                  [](std::pair<std::string, ELFShdrTy> &SectionInfo) {
4387                    return SectionInfo.second;
4388                  });
4389 
4390   return SectionsOnly;
4391 }
4392 
4393 // Rewrite section header table inserting new entries as needed. The sections
4394 // header table size itself may affect the offsets of other sections,
4395 // so we are placing it at the end of the binary.
4396 //
4397 // As we rewrite entries we need to track how many sections were inserted
4398 // as it changes the sh_link value. We map old indices to new ones for
4399 // existing sections.
4400 template <typename ELFT>
4401 void RewriteInstance::patchELFSectionHeaderTable(ELFObjectFile<ELFT> *File) {
4402   using ELFShdrTy = typename ELFObjectFile<ELFT>::Elf_Shdr;
4403   using ELFEhdrTy = typename ELFObjectFile<ELFT>::Elf_Ehdr;
4404   raw_fd_ostream &OS = Out->os();
4405   const ELFFile<ELFT> &Obj = File->getELFFile();
4406 
4407   std::vector<uint32_t> NewSectionIndex;
4408   std::vector<ELFShdrTy> OutputSections =
4409       getOutputSections(File, NewSectionIndex);
4410   LLVM_DEBUG(
4411     dbgs() << "BOLT-DEBUG: old to new section index mapping:\n";
4412     for (uint64_t I = 0; I < NewSectionIndex.size(); ++I)
4413       dbgs() << "  " << I << " -> " << NewSectionIndex[I] << '\n';
4414   );
4415 
4416   // Align starting address for section header table.
4417   uint64_t SHTOffset = OS.tell();
4418   SHTOffset = appendPadding(OS, SHTOffset, sizeof(ELFShdrTy));
4419 
4420   // Write all section header entries while patching section references.
4421   for (ELFShdrTy &Section : OutputSections) {
4422     Section.sh_link = NewSectionIndex[Section.sh_link];
4423     if (Section.sh_type == ELF::SHT_REL || Section.sh_type == ELF::SHT_RELA) {
4424       if (Section.sh_info)
4425         Section.sh_info = NewSectionIndex[Section.sh_info];
4426     }
4427     OS.write(reinterpret_cast<const char *>(&Section), sizeof(Section));
4428   }
4429 
4430   // Fix ELF header.
4431   ELFEhdrTy NewEhdr = Obj.getHeader();
4432 
4433   if (BC->HasRelocations) {
4434     if (RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary())
4435       NewEhdr.e_entry = RtLibrary->getRuntimeStartAddress();
4436     else
4437       NewEhdr.e_entry = getNewFunctionAddress(NewEhdr.e_entry);
4438     assert((NewEhdr.e_entry || !Obj.getHeader().e_entry) &&
4439            "cannot find new address for entry point");
4440   }
4441   NewEhdr.e_phoff = PHDRTableOffset;
4442   NewEhdr.e_phnum = Phnum;
4443   NewEhdr.e_shoff = SHTOffset;
4444   NewEhdr.e_shnum = OutputSections.size();
4445   NewEhdr.e_shstrndx = NewSectionIndex[NewEhdr.e_shstrndx];
4446   OS.pwrite(reinterpret_cast<const char *>(&NewEhdr), sizeof(NewEhdr), 0);
4447 }
4448 
4449 template <typename ELFT, typename WriteFuncTy, typename StrTabFuncTy>
4450 void RewriteInstance::updateELFSymbolTable(
4451     ELFObjectFile<ELFT> *File, bool IsDynSym,
4452     const typename object::ELFObjectFile<ELFT>::Elf_Shdr &SymTabSection,
4453     const std::vector<uint32_t> &NewSectionIndex, WriteFuncTy Write,
4454     StrTabFuncTy AddToStrTab) {
4455   const ELFFile<ELFT> &Obj = File->getELFFile();
4456   using ELFSymTy = typename ELFObjectFile<ELFT>::Elf_Sym;
4457 
4458   StringRef StringSection =
4459       cantFail(Obj.getStringTableForSymtab(SymTabSection));
4460 
4461   unsigned NumHotTextSymsUpdated = 0;
4462   unsigned NumHotDataSymsUpdated = 0;
4463 
4464   std::map<const BinaryFunction *, uint64_t> IslandSizes;
4465   auto getConstantIslandSize = [&IslandSizes](const BinaryFunction &BF) {
4466     auto Itr = IslandSizes.find(&BF);
4467     if (Itr != IslandSizes.end())
4468       return Itr->second;
4469     return IslandSizes[&BF] = BF.estimateConstantIslandSize();
4470   };
4471 
4472   // Symbols for the new symbol table.
4473   std::vector<ELFSymTy> Symbols;
4474 
4475   auto getNewSectionIndex = [&](uint32_t OldIndex) {
4476     assert(OldIndex < NewSectionIndex.size() && "section index out of bounds");
4477     const uint32_t NewIndex = NewSectionIndex[OldIndex];
4478 
4479     // We may have stripped the section that dynsym was referencing due to
4480     // the linker bug. In that case return the old index avoiding marking
4481     // the symbol as undefined.
4482     if (IsDynSym && NewIndex != OldIndex && NewIndex == ELF::SHN_UNDEF)
4483       return OldIndex;
4484     return NewIndex;
4485   };
4486 
4487   // Add extra symbols for the function.
4488   //
4489   // Note that addExtraSymbols() could be called multiple times for the same
4490   // function with different FunctionSymbol matching the main function entry
4491   // point.
4492   auto addExtraSymbols = [&](const BinaryFunction &Function,
4493                              const ELFSymTy &FunctionSymbol) {
4494     if (Function.isFolded()) {
4495       BinaryFunction *ICFParent = Function.getFoldedIntoFunction();
4496       while (ICFParent->isFolded())
4497         ICFParent = ICFParent->getFoldedIntoFunction();
4498       ELFSymTy ICFSymbol = FunctionSymbol;
4499       SmallVector<char, 256> Buf;
4500       ICFSymbol.st_name =
4501           AddToStrTab(Twine(cantFail(FunctionSymbol.getName(StringSection)))
4502                           .concat(".icf.0")
4503                           .toStringRef(Buf));
4504       ICFSymbol.st_value = ICFParent->getOutputAddress();
4505       ICFSymbol.st_size = ICFParent->getOutputSize();
4506       ICFSymbol.st_shndx = ICFParent->getCodeSection()->getIndex();
4507       Symbols.emplace_back(ICFSymbol);
4508     }
4509     if (Function.isSplit() && Function.cold().getAddress()) {
4510       ELFSymTy NewColdSym = FunctionSymbol;
4511       SmallVector<char, 256> Buf;
4512       NewColdSym.st_name =
4513           AddToStrTab(Twine(cantFail(FunctionSymbol.getName(StringSection)))
4514                           .concat(".cold.0")
4515                           .toStringRef(Buf));
4516       NewColdSym.st_shndx = Function.getColdCodeSection()->getIndex();
4517       NewColdSym.st_value = Function.cold().getAddress();
4518       NewColdSym.st_size = Function.cold().getImageSize();
4519       NewColdSym.setBindingAndType(ELF::STB_LOCAL, ELF::STT_FUNC);
4520       Symbols.emplace_back(NewColdSym);
4521     }
4522     if (Function.hasConstantIsland()) {
4523       uint64_t DataMark = Function.getOutputDataAddress();
4524       uint64_t CISize = getConstantIslandSize(Function);
4525       uint64_t CodeMark = DataMark + CISize;
4526       ELFSymTy DataMarkSym = FunctionSymbol;
4527       DataMarkSym.st_name = AddToStrTab("$d");
4528       DataMarkSym.st_value = DataMark;
4529       DataMarkSym.st_size = 0;
4530       DataMarkSym.setType(ELF::STT_NOTYPE);
4531       DataMarkSym.setBinding(ELF::STB_LOCAL);
4532       ELFSymTy CodeMarkSym = DataMarkSym;
4533       CodeMarkSym.st_name = AddToStrTab("$x");
4534       CodeMarkSym.st_value = CodeMark;
4535       Symbols.emplace_back(DataMarkSym);
4536       Symbols.emplace_back(CodeMarkSym);
4537     }
4538     if (Function.hasConstantIsland() && Function.isSplit()) {
4539       uint64_t DataMark = Function.getOutputColdDataAddress();
4540       uint64_t CISize = getConstantIslandSize(Function);
4541       uint64_t CodeMark = DataMark + CISize;
4542       ELFSymTy DataMarkSym = FunctionSymbol;
4543       DataMarkSym.st_name = AddToStrTab("$d");
4544       DataMarkSym.st_value = DataMark;
4545       DataMarkSym.st_size = 0;
4546       DataMarkSym.setType(ELF::STT_NOTYPE);
4547       DataMarkSym.setBinding(ELF::STB_LOCAL);
4548       ELFSymTy CodeMarkSym = DataMarkSym;
4549       CodeMarkSym.st_name = AddToStrTab("$x");
4550       CodeMarkSym.st_value = CodeMark;
4551       Symbols.emplace_back(DataMarkSym);
4552       Symbols.emplace_back(CodeMarkSym);
4553     }
4554   };
4555 
4556   // For regular (non-dynamic) symbol table, exclude symbols referring
4557   // to non-allocatable sections.
4558   auto shouldStrip = [&](const ELFSymTy &Symbol) {
4559     if (Symbol.isAbsolute() || !Symbol.isDefined())
4560       return false;
4561 
4562     // If we cannot link the symbol to a section, leave it as is.
4563     Expected<const typename ELFT::Shdr *> Section =
4564         Obj.getSection(Symbol.st_shndx);
4565     if (!Section)
4566       return false;
4567 
4568     // Remove the section symbol iif the corresponding section was stripped.
4569     if (Symbol.getType() == ELF::STT_SECTION) {
4570       if (!getNewSectionIndex(Symbol.st_shndx))
4571         return true;
4572       return false;
4573     }
4574 
4575     // Symbols in non-allocatable sections are typically remnants of relocations
4576     // emitted under "-emit-relocs" linker option. Delete those as we delete
4577     // relocations against non-allocatable sections.
4578     if (!((*Section)->sh_flags & ELF::SHF_ALLOC))
4579       return true;
4580 
4581     return false;
4582   };
4583 
4584   for (const ELFSymTy &Symbol : cantFail(Obj.symbols(&SymTabSection))) {
4585     // For regular (non-dynamic) symbol table strip unneeded symbols.
4586     if (!IsDynSym && shouldStrip(Symbol))
4587       continue;
4588 
4589     const BinaryFunction *Function =
4590         BC->getBinaryFunctionAtAddress(Symbol.st_value);
4591     // Ignore false function references, e.g. when the section address matches
4592     // the address of the function.
4593     if (Function && Symbol.getType() == ELF::STT_SECTION)
4594       Function = nullptr;
4595 
4596     // For non-dynamic symtab, make sure the symbol section matches that of
4597     // the function. It can mismatch e.g. if the symbol is a section marker
4598     // in which case we treat the symbol separately from the function.
4599     // For dynamic symbol table, the section index could be wrong on the input,
4600     // and its value is ignored by the runtime if it's different from
4601     // SHN_UNDEF and SHN_ABS.
4602     if (!IsDynSym && Function &&
4603         Symbol.st_shndx !=
4604             Function->getOriginSection()->getSectionRef().getIndex())
4605       Function = nullptr;
4606 
4607     // Create a new symbol based on the existing symbol.
4608     ELFSymTy NewSymbol = Symbol;
4609 
4610     if (Function) {
4611       // If the symbol matched a function that was not emitted, update the
4612       // corresponding section index but otherwise leave it unchanged.
4613       if (Function->isEmitted()) {
4614         NewSymbol.st_value = Function->getOutputAddress();
4615         NewSymbol.st_size = Function->getOutputSize();
4616         NewSymbol.st_shndx = Function->getCodeSection()->getIndex();
4617       } else if (Symbol.st_shndx < ELF::SHN_LORESERVE) {
4618         NewSymbol.st_shndx = getNewSectionIndex(Symbol.st_shndx);
4619       }
4620 
4621       // Add new symbols to the symbol table if necessary.
4622       if (!IsDynSym)
4623         addExtraSymbols(*Function, NewSymbol);
4624     } else {
4625       // Check if the function symbol matches address inside a function, i.e.
4626       // it marks a secondary entry point.
4627       Function =
4628           (Symbol.getType() == ELF::STT_FUNC)
4629               ? BC->getBinaryFunctionContainingAddress(Symbol.st_value,
4630                                                        /*CheckPastEnd=*/false,
4631                                                        /*UseMaxSize=*/true)
4632               : nullptr;
4633 
4634       if (Function && Function->isEmitted()) {
4635         const uint64_t OutputAddress =
4636             Function->translateInputToOutputAddress(Symbol.st_value);
4637 
4638         NewSymbol.st_value = OutputAddress;
4639         // Force secondary entry points to have zero size.
4640         NewSymbol.st_size = 0;
4641         NewSymbol.st_shndx =
4642             OutputAddress >= Function->cold().getAddress() &&
4643                     OutputAddress < Function->cold().getImageSize()
4644                 ? Function->getColdCodeSection()->getIndex()
4645                 : Function->getCodeSection()->getIndex();
4646       } else {
4647         // Check if the symbol belongs to moved data object and update it.
4648         BinaryData *BD = opts::ReorderData.empty()
4649                              ? nullptr
4650                              : BC->getBinaryDataAtAddress(Symbol.st_value);
4651         if (BD && BD->isMoved() && !BD->isJumpTable()) {
4652           assert((!BD->getSize() || !Symbol.st_size ||
4653                   Symbol.st_size == BD->getSize()) &&
4654                  "sizes must match");
4655 
4656           BinarySection &OutputSection = BD->getOutputSection();
4657           assert(OutputSection.getIndex());
4658           LLVM_DEBUG(dbgs()
4659                      << "BOLT-DEBUG: moving " << BD->getName() << " from "
4660                      << *BC->getSectionNameForAddress(Symbol.st_value) << " ("
4661                      << Symbol.st_shndx << ") to " << OutputSection.getName()
4662                      << " (" << OutputSection.getIndex() << ")\n");
4663           NewSymbol.st_shndx = OutputSection.getIndex();
4664           NewSymbol.st_value = BD->getOutputAddress();
4665         } else {
4666           // Otherwise just update the section for the symbol.
4667           if (Symbol.st_shndx < ELF::SHN_LORESERVE)
4668             NewSymbol.st_shndx = getNewSectionIndex(Symbol.st_shndx);
4669         }
4670 
4671         // Detect local syms in the text section that we didn't update
4672         // and that were preserved by the linker to support relocations against
4673         // .text. Remove them from the symtab.
4674         if (Symbol.getType() == ELF::STT_NOTYPE &&
4675             Symbol.getBinding() == ELF::STB_LOCAL && Symbol.st_size == 0) {
4676           if (BC->getBinaryFunctionContainingAddress(Symbol.st_value,
4677                                                      /*CheckPastEnd=*/false,
4678                                                      /*UseMaxSize=*/true)) {
4679             // Can only delete the symbol if not patching. Such symbols should
4680             // not exist in the dynamic symbol table.
4681             assert(!IsDynSym && "cannot delete symbol");
4682             continue;
4683           }
4684         }
4685       }
4686     }
4687 
4688     // Handle special symbols based on their name.
4689     Expected<StringRef> SymbolName = Symbol.getName(StringSection);
4690     assert(SymbolName && "cannot get symbol name");
4691 
4692     auto updateSymbolValue = [&](const StringRef Name, unsigned &IsUpdated) {
4693       NewSymbol.st_value = getNewValueForSymbol(Name);
4694       NewSymbol.st_shndx = ELF::SHN_ABS;
4695       outs() << "BOLT-INFO: setting " << Name << " to 0x"
4696              << Twine::utohexstr(NewSymbol.st_value) << '\n';
4697       ++IsUpdated;
4698     };
4699 
4700     if (opts::HotText &&
4701         (*SymbolName == "__hot_start" || *SymbolName == "__hot_end"))
4702       updateSymbolValue(*SymbolName, NumHotTextSymsUpdated);
4703 
4704     if (opts::HotData &&
4705         (*SymbolName == "__hot_data_start" || *SymbolName == "__hot_data_end"))
4706       updateSymbolValue(*SymbolName, NumHotDataSymsUpdated);
4707 
4708     if (*SymbolName == "_end") {
4709       unsigned Ignored;
4710       updateSymbolValue(*SymbolName, Ignored);
4711     }
4712 
4713     if (IsDynSym)
4714       Write((&Symbol - cantFail(Obj.symbols(&SymTabSection)).begin()) *
4715                 sizeof(ELFSymTy),
4716             NewSymbol);
4717     else
4718       Symbols.emplace_back(NewSymbol);
4719   }
4720 
4721   if (IsDynSym) {
4722     assert(Symbols.empty());
4723     return;
4724   }
4725 
4726   // Add symbols of injected functions
4727   for (BinaryFunction *Function : BC->getInjectedBinaryFunctions()) {
4728     ELFSymTy NewSymbol;
4729     BinarySection *OriginSection = Function->getOriginSection();
4730     NewSymbol.st_shndx =
4731         OriginSection
4732             ? getNewSectionIndex(OriginSection->getSectionRef().getIndex())
4733             : Function->getCodeSection()->getIndex();
4734     NewSymbol.st_value = Function->getOutputAddress();
4735     NewSymbol.st_name = AddToStrTab(Function->getOneName());
4736     NewSymbol.st_size = Function->getOutputSize();
4737     NewSymbol.st_other = 0;
4738     NewSymbol.setBindingAndType(ELF::STB_LOCAL, ELF::STT_FUNC);
4739     Symbols.emplace_back(NewSymbol);
4740 
4741     if (Function->isSplit()) {
4742       ELFSymTy NewColdSym = NewSymbol;
4743       NewColdSym.setType(ELF::STT_NOTYPE);
4744       SmallVector<char, 256> Buf;
4745       NewColdSym.st_name = AddToStrTab(
4746           Twine(Function->getPrintName()).concat(".cold.0").toStringRef(Buf));
4747       NewColdSym.st_value = Function->cold().getAddress();
4748       NewColdSym.st_size = Function->cold().getImageSize();
4749       Symbols.emplace_back(NewColdSym);
4750     }
4751   }
4752 
4753   assert((!NumHotTextSymsUpdated || NumHotTextSymsUpdated == 2) &&
4754          "either none or both __hot_start/__hot_end symbols were expected");
4755   assert((!NumHotDataSymsUpdated || NumHotDataSymsUpdated == 2) &&
4756          "either none or both __hot_data_start/__hot_data_end symbols were "
4757          "expected");
4758 
4759   auto addSymbol = [&](const std::string &Name) {
4760     ELFSymTy Symbol;
4761     Symbol.st_value = getNewValueForSymbol(Name);
4762     Symbol.st_shndx = ELF::SHN_ABS;
4763     Symbol.st_name = AddToStrTab(Name);
4764     Symbol.st_size = 0;
4765     Symbol.st_other = 0;
4766     Symbol.setBindingAndType(ELF::STB_WEAK, ELF::STT_NOTYPE);
4767 
4768     outs() << "BOLT-INFO: setting " << Name << " to 0x"
4769            << Twine::utohexstr(Symbol.st_value) << '\n';
4770 
4771     Symbols.emplace_back(Symbol);
4772   };
4773 
4774   if (opts::HotText && !NumHotTextSymsUpdated) {
4775     addSymbol("__hot_start");
4776     addSymbol("__hot_end");
4777   }
4778 
4779   if (opts::HotData && !NumHotDataSymsUpdated) {
4780     addSymbol("__hot_data_start");
4781     addSymbol("__hot_data_end");
4782   }
4783 
4784   // Put local symbols at the beginning.
4785   std::stable_sort(Symbols.begin(), Symbols.end(),
4786                    [](const ELFSymTy &A, const ELFSymTy &B) {
4787                      if (A.getBinding() == ELF::STB_LOCAL &&
4788                          B.getBinding() != ELF::STB_LOCAL)
4789                        return true;
4790                      return false;
4791                    });
4792 
4793   for (const ELFSymTy &Symbol : Symbols)
4794     Write(0, Symbol);
4795 }
4796 
4797 template <typename ELFT>
4798 void RewriteInstance::patchELFSymTabs(ELFObjectFile<ELFT> *File) {
4799   const ELFFile<ELFT> &Obj = File->getELFFile();
4800   using ELFShdrTy = typename ELFObjectFile<ELFT>::Elf_Shdr;
4801   using ELFSymTy = typename ELFObjectFile<ELFT>::Elf_Sym;
4802 
4803   // Compute a preview of how section indices will change after rewriting, so
4804   // we can properly update the symbol table based on new section indices.
4805   std::vector<uint32_t> NewSectionIndex;
4806   getOutputSections(File, NewSectionIndex);
4807 
4808   // Set pointer at the end of the output file, so we can pwrite old symbol
4809   // tables if we need to.
4810   uint64_t NextAvailableOffset = getFileOffsetForAddress(NextAvailableAddress);
4811   assert(NextAvailableOffset >= FirstNonAllocatableOffset &&
4812          "next available offset calculation failure");
4813   Out->os().seek(NextAvailableOffset);
4814 
4815   // Update dynamic symbol table.
4816   const ELFShdrTy *DynSymSection = nullptr;
4817   for (const ELFShdrTy &Section : cantFail(Obj.sections())) {
4818     if (Section.sh_type == ELF::SHT_DYNSYM) {
4819       DynSymSection = &Section;
4820       break;
4821     }
4822   }
4823   assert((DynSymSection || BC->IsStaticExecutable) &&
4824          "dynamic symbol table expected");
4825   if (DynSymSection) {
4826     updateELFSymbolTable(
4827         File,
4828         /*IsDynSym=*/true,
4829         *DynSymSection,
4830         NewSectionIndex,
4831         [&](size_t Offset, const ELFSymTy &Sym) {
4832           Out->os().pwrite(reinterpret_cast<const char *>(&Sym),
4833                            sizeof(ELFSymTy),
4834                            DynSymSection->sh_offset + Offset);
4835         },
4836         [](StringRef) -> size_t { return 0; });
4837   }
4838 
4839   if (opts::RemoveSymtab)
4840     return;
4841 
4842   // (re)create regular symbol table.
4843   const ELFShdrTy *SymTabSection = nullptr;
4844   for (const ELFShdrTy &Section : cantFail(Obj.sections())) {
4845     if (Section.sh_type == ELF::SHT_SYMTAB) {
4846       SymTabSection = &Section;
4847       break;
4848     }
4849   }
4850   if (!SymTabSection) {
4851     errs() << "BOLT-WARNING: no symbol table found\n";
4852     return;
4853   }
4854 
4855   const ELFShdrTy *StrTabSection =
4856       cantFail(Obj.getSection(SymTabSection->sh_link));
4857   std::string NewContents;
4858   std::string NewStrTab = std::string(
4859       File->getData().substr(StrTabSection->sh_offset, StrTabSection->sh_size));
4860   StringRef SecName = cantFail(Obj.getSectionName(*SymTabSection));
4861   StringRef StrSecName = cantFail(Obj.getSectionName(*StrTabSection));
4862 
4863   NumLocalSymbols = 0;
4864   updateELFSymbolTable(
4865       File,
4866       /*IsDynSym=*/false,
4867       *SymTabSection,
4868       NewSectionIndex,
4869       [&](size_t Offset, const ELFSymTy &Sym) {
4870         if (Sym.getBinding() == ELF::STB_LOCAL)
4871           ++NumLocalSymbols;
4872         NewContents.append(reinterpret_cast<const char *>(&Sym),
4873                            sizeof(ELFSymTy));
4874       },
4875       [&](StringRef Str) {
4876         size_t Idx = NewStrTab.size();
4877         NewStrTab.append(NameResolver::restore(Str).str());
4878         NewStrTab.append(1, '\0');
4879         return Idx;
4880       });
4881 
4882   BC->registerOrUpdateNoteSection(SecName,
4883                                   copyByteArray(NewContents),
4884                                   NewContents.size(),
4885                                   /*Alignment=*/1,
4886                                   /*IsReadOnly=*/true,
4887                                   ELF::SHT_SYMTAB);
4888 
4889   BC->registerOrUpdateNoteSection(StrSecName,
4890                                   copyByteArray(NewStrTab),
4891                                   NewStrTab.size(),
4892                                   /*Alignment=*/1,
4893                                   /*IsReadOnly=*/true,
4894                                   ELF::SHT_STRTAB);
4895 }
4896 
4897 template <typename ELFT>
4898 void
4899 RewriteInstance::patchELFAllocatableRelaSections(ELFObjectFile<ELFT> *File) {
4900   using Elf_Rela = typename ELFT::Rela;
4901   raw_fd_ostream &OS = Out->os();
4902   const ELFFile<ELFT> &EF = File->getELFFile();
4903 
4904   uint64_t RelDynOffset = 0, RelDynEndOffset = 0;
4905   uint64_t RelPltOffset = 0, RelPltEndOffset = 0;
4906 
4907   auto setSectionFileOffsets = [&](uint64_t Address, uint64_t &Start,
4908                                    uint64_t &End) {
4909     ErrorOr<BinarySection &> Section = BC->getSectionForAddress(Address);
4910     Start = Section->getInputFileOffset();
4911     End = Start + Section->getSize();
4912   };
4913 
4914   if (!DynamicRelocationsAddress && !PLTRelocationsAddress)
4915     return;
4916 
4917   if (DynamicRelocationsAddress)
4918     setSectionFileOffsets(*DynamicRelocationsAddress, RelDynOffset,
4919                           RelDynEndOffset);
4920 
4921   if (PLTRelocationsAddress)
4922     setSectionFileOffsets(*PLTRelocationsAddress, RelPltOffset,
4923                           RelPltEndOffset);
4924 
4925   DynamicRelativeRelocationsCount = 0;
4926 
4927   auto writeRela = [&OS](const Elf_Rela *RelA, uint64_t &Offset) {
4928     OS.pwrite(reinterpret_cast<const char *>(RelA), sizeof(*RelA), Offset);
4929     Offset += sizeof(*RelA);
4930   };
4931 
4932   auto writeRelocations = [&](bool PatchRelative) {
4933     for (BinarySection &Section : BC->allocatableSections()) {
4934       for (const Relocation &Rel : Section.dynamicRelocations()) {
4935         const bool IsRelative = Rel.isRelative();
4936         if (PatchRelative != IsRelative)
4937           continue;
4938 
4939         if (IsRelative)
4940           ++DynamicRelativeRelocationsCount;
4941 
4942         Elf_Rela NewRelA;
4943         uint64_t SectionAddress = Section.getOutputAddress();
4944         SectionAddress =
4945             SectionAddress == 0 ? Section.getAddress() : SectionAddress;
4946         MCSymbol *Symbol = Rel.Symbol;
4947         uint32_t SymbolIdx = 0;
4948         uint64_t Addend = Rel.Addend;
4949 
4950         if (Rel.Symbol) {
4951           SymbolIdx = getOutputDynamicSymbolIndex(Symbol);
4952         } else {
4953           // Usually this case is used for R_*_(I)RELATIVE relocations
4954           const uint64_t Address = getNewFunctionOrDataAddress(Addend);
4955           if (Address)
4956             Addend = Address;
4957         }
4958 
4959         NewRelA.setSymbolAndType(SymbolIdx, Rel.Type, EF.isMips64EL());
4960         NewRelA.r_offset = SectionAddress + Rel.Offset;
4961         NewRelA.r_addend = Addend;
4962 
4963         const bool IsJmpRel =
4964             !!(IsJmpRelocation.find(Rel.Type) != IsJmpRelocation.end());
4965         uint64_t &Offset = IsJmpRel ? RelPltOffset : RelDynOffset;
4966         const uint64_t &EndOffset =
4967             IsJmpRel ? RelPltEndOffset : RelDynEndOffset;
4968         if (!Offset || !EndOffset) {
4969           errs() << "BOLT-ERROR: Invalid offsets for dynamic relocation\n";
4970           exit(1);
4971         }
4972 
4973         if (Offset + sizeof(NewRelA) > EndOffset) {
4974           errs() << "BOLT-ERROR: Offset overflow for dynamic relocation\n";
4975           exit(1);
4976         }
4977 
4978         writeRela(&NewRelA, Offset);
4979       }
4980     }
4981   };
4982 
4983   // The dynamic linker expects R_*_RELATIVE relocations to be emitted first
4984   writeRelocations(/* PatchRelative */ true);
4985   writeRelocations(/* PatchRelative */ false);
4986 
4987   auto fillNone = [&](uint64_t &Offset, uint64_t EndOffset) {
4988     if (!Offset)
4989       return;
4990 
4991     typename ELFObjectFile<ELFT>::Elf_Rela RelA;
4992     RelA.setSymbolAndType(0, Relocation::getNone(), EF.isMips64EL());
4993     RelA.r_offset = 0;
4994     RelA.r_addend = 0;
4995     while (Offset < EndOffset)
4996       writeRela(&RelA, Offset);
4997 
4998     assert(Offset == EndOffset && "Unexpected section overflow");
4999   };
5000 
5001   // Fill the rest of the sections with R_*_NONE relocations
5002   fillNone(RelDynOffset, RelDynEndOffset);
5003   fillNone(RelPltOffset, RelPltEndOffset);
5004 }
5005 
5006 template <typename ELFT>
5007 void RewriteInstance::patchELFGOT(ELFObjectFile<ELFT> *File) {
5008   raw_fd_ostream &OS = Out->os();
5009 
5010   SectionRef GOTSection;
5011   for (const SectionRef &Section : File->sections()) {
5012     StringRef SectionName = cantFail(Section.getName());
5013     if (SectionName == ".got") {
5014       GOTSection = Section;
5015       break;
5016     }
5017   }
5018   if (!GOTSection.getObject()) {
5019     if (!BC->IsStaticExecutable)
5020       errs() << "BOLT-INFO: no .got section found\n";
5021     return;
5022   }
5023 
5024   StringRef GOTContents = cantFail(GOTSection.getContents());
5025   for (const uint64_t *GOTEntry =
5026            reinterpret_cast<const uint64_t *>(GOTContents.data());
5027        GOTEntry < reinterpret_cast<const uint64_t *>(GOTContents.data() +
5028                                                      GOTContents.size());
5029        ++GOTEntry) {
5030     if (uint64_t NewAddress = getNewFunctionAddress(*GOTEntry)) {
5031       LLVM_DEBUG(dbgs() << "BOLT-DEBUG: patching GOT entry 0x"
5032                         << Twine::utohexstr(*GOTEntry) << " with 0x"
5033                         << Twine::utohexstr(NewAddress) << '\n');
5034       OS.pwrite(reinterpret_cast<const char *>(&NewAddress), sizeof(NewAddress),
5035                 reinterpret_cast<const char *>(GOTEntry) -
5036                     File->getData().data());
5037     }
5038   }
5039 }
5040 
5041 template <typename ELFT>
5042 void RewriteInstance::patchELFDynamic(ELFObjectFile<ELFT> *File) {
5043   if (BC->IsStaticExecutable)
5044     return;
5045 
5046   const ELFFile<ELFT> &Obj = File->getELFFile();
5047   raw_fd_ostream &OS = Out->os();
5048 
5049   using Elf_Phdr = typename ELFFile<ELFT>::Elf_Phdr;
5050   using Elf_Dyn = typename ELFFile<ELFT>::Elf_Dyn;
5051 
5052   // Locate DYNAMIC by looking through program headers.
5053   uint64_t DynamicOffset = 0;
5054   const Elf_Phdr *DynamicPhdr = 0;
5055   for (const Elf_Phdr &Phdr : cantFail(Obj.program_headers())) {
5056     if (Phdr.p_type == ELF::PT_DYNAMIC) {
5057       DynamicOffset = Phdr.p_offset;
5058       DynamicPhdr = &Phdr;
5059       assert(Phdr.p_memsz == Phdr.p_filesz && "dynamic sizes should match");
5060       break;
5061     }
5062   }
5063   assert(DynamicPhdr && "missing dynamic in ELF binary");
5064 
5065   bool ZNowSet = false;
5066 
5067   // Go through all dynamic entries and patch functions addresses with
5068   // new ones.
5069   typename ELFT::DynRange DynamicEntries =
5070       cantFail(Obj.dynamicEntries(), "error accessing dynamic table");
5071   auto DTB = DynamicEntries.begin();
5072   for (const Elf_Dyn &Dyn : DynamicEntries) {
5073     Elf_Dyn NewDE = Dyn;
5074     bool ShouldPatch = true;
5075     switch (Dyn.d_tag) {
5076     default:
5077       ShouldPatch = false;
5078       break;
5079     case ELF::DT_RELACOUNT:
5080       NewDE.d_un.d_val = DynamicRelativeRelocationsCount;
5081       break;
5082     case ELF::DT_INIT:
5083     case ELF::DT_FINI: {
5084       if (BC->HasRelocations) {
5085         if (uint64_t NewAddress = getNewFunctionAddress(Dyn.getPtr())) {
5086           LLVM_DEBUG(dbgs() << "BOLT-DEBUG: patching dynamic entry of type "
5087                             << Dyn.getTag() << '\n');
5088           NewDE.d_un.d_ptr = NewAddress;
5089         }
5090       }
5091       RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary();
5092       if (RtLibrary && Dyn.getTag() == ELF::DT_FINI) {
5093         if (uint64_t Addr = RtLibrary->getRuntimeFiniAddress())
5094           NewDE.d_un.d_ptr = Addr;
5095       }
5096       if (RtLibrary && Dyn.getTag() == ELF::DT_INIT && !BC->HasInterpHeader) {
5097         if (auto Addr = RtLibrary->getRuntimeStartAddress()) {
5098           LLVM_DEBUG(dbgs() << "BOLT-DEBUG: Set DT_INIT to 0x"
5099                             << Twine::utohexstr(Addr) << '\n');
5100           NewDE.d_un.d_ptr = Addr;
5101         }
5102       }
5103       break;
5104     }
5105     case ELF::DT_FLAGS:
5106       if (BC->RequiresZNow) {
5107         NewDE.d_un.d_val |= ELF::DF_BIND_NOW;
5108         ZNowSet = true;
5109       }
5110       break;
5111     case ELF::DT_FLAGS_1:
5112       if (BC->RequiresZNow) {
5113         NewDE.d_un.d_val |= ELF::DF_1_NOW;
5114         ZNowSet = true;
5115       }
5116       break;
5117     }
5118     if (ShouldPatch)
5119       OS.pwrite(reinterpret_cast<const char *>(&NewDE), sizeof(NewDE),
5120                 DynamicOffset + (&Dyn - DTB) * sizeof(Dyn));
5121   }
5122 
5123   if (BC->RequiresZNow && !ZNowSet) {
5124     errs() << "BOLT-ERROR: output binary requires immediate relocation "
5125               "processing which depends on DT_FLAGS or DT_FLAGS_1 presence in "
5126               ".dynamic. Please re-link the binary with -znow.\n";
5127     exit(1);
5128   }
5129 }
5130 
5131 template <typename ELFT>
5132 Error RewriteInstance::readELFDynamic(ELFObjectFile<ELFT> *File) {
5133   const ELFFile<ELFT> &Obj = File->getELFFile();
5134 
5135   using Elf_Phdr = typename ELFFile<ELFT>::Elf_Phdr;
5136   using Elf_Dyn = typename ELFFile<ELFT>::Elf_Dyn;
5137 
5138   // Locate DYNAMIC by looking through program headers.
5139   const Elf_Phdr *DynamicPhdr = 0;
5140   for (const Elf_Phdr &Phdr : cantFail(Obj.program_headers())) {
5141     if (Phdr.p_type == ELF::PT_DYNAMIC) {
5142       DynamicPhdr = &Phdr;
5143       break;
5144     }
5145   }
5146 
5147   if (!DynamicPhdr) {
5148     outs() << "BOLT-INFO: static input executable detected\n";
5149     // TODO: static PIE executable might have dynamic header
5150     BC->IsStaticExecutable = true;
5151     return Error::success();
5152   }
5153 
5154   if (DynamicPhdr->p_memsz != DynamicPhdr->p_filesz)
5155     return createStringError(errc::executable_format_error,
5156                              "dynamic section sizes should match");
5157 
5158   // Go through all dynamic entries to locate entries of interest.
5159   auto DynamicEntriesOrErr = Obj.dynamicEntries();
5160   if (!DynamicEntriesOrErr)
5161     return DynamicEntriesOrErr.takeError();
5162   typename ELFT::DynRange DynamicEntries = DynamicEntriesOrErr.get();
5163 
5164   for (const Elf_Dyn &Dyn : DynamicEntries) {
5165     switch (Dyn.d_tag) {
5166     case ELF::DT_INIT:
5167       if (!BC->HasInterpHeader) {
5168         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: Set start function address\n");
5169         BC->StartFunctionAddress = Dyn.getPtr();
5170       }
5171       break;
5172     case ELF::DT_FINI:
5173       BC->FiniFunctionAddress = Dyn.getPtr();
5174       break;
5175     case ELF::DT_RELA:
5176       DynamicRelocationsAddress = Dyn.getPtr();
5177       break;
5178     case ELF::DT_RELASZ:
5179       DynamicRelocationsSize = Dyn.getVal();
5180       break;
5181     case ELF::DT_JMPREL:
5182       PLTRelocationsAddress = Dyn.getPtr();
5183       break;
5184     case ELF::DT_PLTRELSZ:
5185       PLTRelocationsSize = Dyn.getVal();
5186       break;
5187     case ELF::DT_RELACOUNT:
5188       DynamicRelativeRelocationsCount = Dyn.getVal();
5189       break;
5190     }
5191   }
5192 
5193   if (!DynamicRelocationsAddress || !DynamicRelocationsSize) {
5194     DynamicRelocationsAddress.reset();
5195     DynamicRelocationsSize = 0;
5196   }
5197 
5198   if (!PLTRelocationsAddress || !PLTRelocationsSize) {
5199     PLTRelocationsAddress.reset();
5200     PLTRelocationsSize = 0;
5201   }
5202   return Error::success();
5203 }
5204 
5205 uint64_t RewriteInstance::getNewFunctionAddress(uint64_t OldAddress) {
5206   const BinaryFunction *Function = BC->getBinaryFunctionAtAddress(OldAddress);
5207   if (!Function)
5208     return 0;
5209 
5210   return Function->getOutputAddress();
5211 }
5212 
5213 uint64_t RewriteInstance::getNewFunctionOrDataAddress(uint64_t OldAddress) {
5214   if (uint64_t Function = getNewFunctionAddress(OldAddress))
5215     return Function;
5216 
5217   const BinaryData *BD = BC->getBinaryDataAtAddress(OldAddress);
5218   if (BD && BD->isMoved())
5219     return BD->getOutputAddress();
5220 
5221   return 0;
5222 }
5223 
5224 void RewriteInstance::rewriteFile() {
5225   std::error_code EC;
5226   Out = std::make_unique<ToolOutputFile>(opts::OutputFilename, EC,
5227                                          sys::fs::OF_None);
5228   check_error(EC, "cannot create output executable file");
5229 
5230   raw_fd_ostream &OS = Out->os();
5231 
5232   // Copy allocatable part of the input.
5233   OS << InputFile->getData().substr(0, FirstNonAllocatableOffset);
5234 
5235   // We obtain an asm-specific writer so that we can emit nops in an
5236   // architecture-specific way at the end of the function.
5237   std::unique_ptr<MCAsmBackend> MAB(
5238       BC->TheTarget->createMCAsmBackend(*BC->STI, *BC->MRI, MCTargetOptions()));
5239   auto Streamer = BC->createStreamer(OS);
5240   // Make sure output stream has enough reserved space, otherwise
5241   // pwrite() will fail.
5242   uint64_t Offset = OS.seek(getFileOffsetForAddress(NextAvailableAddress));
5243   (void)Offset;
5244   assert(Offset == getFileOffsetForAddress(NextAvailableAddress) &&
5245          "error resizing output file");
5246 
5247   // Overwrite functions with fixed output address. This is mostly used by
5248   // non-relocation mode, with one exception: injected functions are covered
5249   // here in both modes.
5250   uint64_t CountOverwrittenFunctions = 0;
5251   uint64_t OverwrittenScore = 0;
5252   for (BinaryFunction *Function : BC->getAllBinaryFunctions()) {
5253     if (Function->getImageAddress() == 0 || Function->getImageSize() == 0)
5254       continue;
5255 
5256     if (Function->getImageSize() > Function->getMaxSize()) {
5257       if (opts::Verbosity >= 1)
5258         errs() << "BOLT-WARNING: new function size (0x"
5259                << Twine::utohexstr(Function->getImageSize())
5260                << ") is larger than maximum allowed size (0x"
5261                << Twine::utohexstr(Function->getMaxSize()) << ") for function "
5262                << *Function << '\n';
5263 
5264       // Remove jump table sections that this function owns in non-reloc mode
5265       // because we don't want to write them anymore.
5266       if (!BC->HasRelocations && opts::JumpTables == JTS_BASIC) {
5267         for (auto &JTI : Function->JumpTables) {
5268           JumpTable *JT = JTI.second;
5269           BinarySection &Section = JT->getOutputSection();
5270           BC->deregisterSection(Section);
5271         }
5272       }
5273       continue;
5274     }
5275 
5276     if (Function->isSplit() && (Function->cold().getImageAddress() == 0 ||
5277                                 Function->cold().getImageSize() == 0))
5278       continue;
5279 
5280     OverwrittenScore += Function->getFunctionScore();
5281     // Overwrite function in the output file.
5282     if (opts::Verbosity >= 2)
5283       outs() << "BOLT: rewriting function \"" << *Function << "\"\n";
5284 
5285     OS.pwrite(reinterpret_cast<char *>(Function->getImageAddress()),
5286               Function->getImageSize(), Function->getFileOffset());
5287 
5288     // Write nops at the end of the function.
5289     if (Function->getMaxSize() != std::numeric_limits<uint64_t>::max()) {
5290       uint64_t Pos = OS.tell();
5291       OS.seek(Function->getFileOffset() + Function->getImageSize());
5292       MAB->writeNopData(OS, Function->getMaxSize() - Function->getImageSize(),
5293                         &*BC->STI);
5294 
5295       OS.seek(Pos);
5296     }
5297 
5298     if (!Function->isSplit()) {
5299       ++CountOverwrittenFunctions;
5300       if (opts::MaxFunctions &&
5301           CountOverwrittenFunctions == opts::MaxFunctions) {
5302         outs() << "BOLT: maximum number of functions reached\n";
5303         break;
5304       }
5305       continue;
5306     }
5307 
5308     // Write cold part
5309     if (opts::Verbosity >= 2)
5310       outs() << "BOLT: rewriting function \"" << *Function
5311              << "\" (cold part)\n";
5312 
5313     OS.pwrite(reinterpret_cast<char *>(Function->cold().getImageAddress()),
5314               Function->cold().getImageSize(),
5315               Function->cold().getFileOffset());
5316 
5317     ++CountOverwrittenFunctions;
5318     if (opts::MaxFunctions && CountOverwrittenFunctions == opts::MaxFunctions) {
5319       outs() << "BOLT: maximum number of functions reached\n";
5320       break;
5321     }
5322   }
5323 
5324   // Print function statistics for non-relocation mode.
5325   if (!BC->HasRelocations) {
5326     outs() << "BOLT: " << CountOverwrittenFunctions << " out of "
5327            << BC->getBinaryFunctions().size()
5328            << " functions were overwritten.\n";
5329     if (BC->TotalScore != 0) {
5330       double Coverage = OverwrittenScore / (double)BC->TotalScore * 100.0;
5331       outs() << format("BOLT-INFO: rewritten functions cover %.2lf", Coverage)
5332              << "% of the execution count of simple functions of "
5333                 "this binary\n";
5334     }
5335   }
5336 
5337   if (BC->HasRelocations && opts::TrapOldCode) {
5338     uint64_t SavedPos = OS.tell();
5339     // Overwrite function body to make sure we never execute these instructions.
5340     for (auto &BFI : BC->getBinaryFunctions()) {
5341       BinaryFunction &BF = BFI.second;
5342       if (!BF.getFileOffset() || !BF.isEmitted())
5343         continue;
5344       OS.seek(BF.getFileOffset());
5345       for (unsigned I = 0; I < BF.getMaxSize(); ++I)
5346         OS.write((unsigned char)BC->MIB->getTrapFillValue());
5347     }
5348     OS.seek(SavedPos);
5349   }
5350 
5351   // Write all allocatable sections - reloc-mode text is written here as well
5352   for (BinarySection &Section : BC->allocatableSections()) {
5353     if (!Section.isFinalized() || !Section.getOutputData())
5354       continue;
5355 
5356     if (opts::Verbosity >= 1)
5357       outs() << "BOLT: writing new section " << Section.getName()
5358              << "\n data at 0x" << Twine::utohexstr(Section.getAllocAddress())
5359              << "\n of size " << Section.getOutputSize() << "\n at offset "
5360              << Section.getOutputFileOffset() << '\n';
5361     OS.pwrite(reinterpret_cast<const char *>(Section.getOutputData()),
5362               Section.getOutputSize(), Section.getOutputFileOffset());
5363   }
5364 
5365   for (BinarySection &Section : BC->allocatableSections())
5366     Section.flushPendingRelocations(OS, [this](const MCSymbol *S) {
5367       return getNewValueForSymbol(S->getName());
5368     });
5369 
5370   // If .eh_frame is present create .eh_frame_hdr.
5371   if (EHFrameSection && EHFrameSection->isFinalized())
5372     writeEHFrameHeader();
5373 
5374   // Add BOLT Addresses Translation maps to allow profile collection to
5375   // happen in the output binary
5376   if (opts::EnableBAT)
5377     addBATSection();
5378 
5379   // Patch program header table.
5380   patchELFPHDRTable();
5381 
5382   // Finalize memory image of section string table.
5383   finalizeSectionStringTable();
5384 
5385   // Update symbol tables.
5386   patchELFSymTabs();
5387 
5388   patchBuildID();
5389 
5390   if (opts::EnableBAT)
5391     encodeBATSection();
5392 
5393   // Copy non-allocatable sections once allocatable part is finished.
5394   rewriteNoteSections();
5395 
5396   if (BC->HasRelocations) {
5397     patchELFAllocatableRelaSections();
5398     patchELFGOT();
5399   }
5400 
5401   // Patch dynamic section/segment.
5402   patchELFDynamic();
5403 
5404   // Update ELF book-keeping info.
5405   patchELFSectionHeaderTable();
5406 
5407   if (opts::PrintSections) {
5408     outs() << "BOLT-INFO: Sections after processing:\n";
5409     BC->printSections(outs());
5410   }
5411 
5412   Out->keep();
5413   EC = sys::fs::setPermissions(opts::OutputFilename, sys::fs::perms::all_all);
5414   check_error(EC, "cannot set permissions of output file");
5415 }
5416 
5417 void RewriteInstance::writeEHFrameHeader() {
5418   DWARFDebugFrame NewEHFrame(BC->TheTriple->getArch(), true,
5419                              EHFrameSection->getOutputAddress());
5420   Error E = NewEHFrame.parse(DWARFDataExtractor(
5421       EHFrameSection->getOutputContents(), BC->AsmInfo->isLittleEndian(),
5422       BC->AsmInfo->getCodePointerSize()));
5423   check_error(std::move(E), "failed to parse EH frame");
5424 
5425   uint64_t OldEHFrameAddress = 0;
5426   StringRef OldEHFrameContents;
5427   ErrorOr<BinarySection &> OldEHFrameSection =
5428       BC->getUniqueSectionByName(Twine(getOrgSecPrefix(), ".eh_frame").str());
5429   if (OldEHFrameSection) {
5430     OldEHFrameAddress = OldEHFrameSection->getOutputAddress();
5431     OldEHFrameContents = OldEHFrameSection->getOutputContents();
5432   }
5433   DWARFDebugFrame OldEHFrame(BC->TheTriple->getArch(), true, OldEHFrameAddress);
5434   Error Er = OldEHFrame.parse(
5435       DWARFDataExtractor(OldEHFrameContents, BC->AsmInfo->isLittleEndian(),
5436                          BC->AsmInfo->getCodePointerSize()));
5437   check_error(std::move(Er), "failed to parse EH frame");
5438 
5439   LLVM_DEBUG(dbgs() << "BOLT: writing a new .eh_frame_hdr\n");
5440 
5441   NextAvailableAddress =
5442       appendPadding(Out->os(), NextAvailableAddress, EHFrameHdrAlign);
5443 
5444   const uint64_t EHFrameHdrOutputAddress = NextAvailableAddress;
5445   const uint64_t EHFrameHdrFileOffset =
5446       getFileOffsetForAddress(NextAvailableAddress);
5447 
5448   std::vector<char> NewEHFrameHdr = CFIRdWrt->generateEHFrameHeader(
5449       OldEHFrame, NewEHFrame, EHFrameHdrOutputAddress, FailedAddresses);
5450 
5451   assert(Out->os().tell() == EHFrameHdrFileOffset && "offset mismatch");
5452   Out->os().write(NewEHFrameHdr.data(), NewEHFrameHdr.size());
5453 
5454   const unsigned Flags = BinarySection::getFlags(/*IsReadOnly=*/true,
5455                                                  /*IsText=*/false,
5456                                                  /*IsAllocatable=*/true);
5457   BinarySection &EHFrameHdrSec = BC->registerOrUpdateSection(
5458       ".eh_frame_hdr", ELF::SHT_PROGBITS, Flags, nullptr, NewEHFrameHdr.size(),
5459       /*Alignment=*/1);
5460   EHFrameHdrSec.setOutputFileOffset(EHFrameHdrFileOffset);
5461   EHFrameHdrSec.setOutputAddress(EHFrameHdrOutputAddress);
5462 
5463   NextAvailableAddress += EHFrameHdrSec.getOutputSize();
5464 
5465   // Merge new .eh_frame with original so that gdb can locate all FDEs.
5466   if (OldEHFrameSection) {
5467     const uint64_t EHFrameSectionSize = (OldEHFrameSection->getOutputAddress() +
5468                                          OldEHFrameSection->getOutputSize() -
5469                                          EHFrameSection->getOutputAddress());
5470     EHFrameSection =
5471       BC->registerOrUpdateSection(".eh_frame",
5472                                   EHFrameSection->getELFType(),
5473                                   EHFrameSection->getELFFlags(),
5474                                   EHFrameSection->getOutputData(),
5475                                   EHFrameSectionSize,
5476                                   EHFrameSection->getAlignment());
5477     BC->deregisterSection(*OldEHFrameSection);
5478   }
5479 
5480   LLVM_DEBUG(dbgs() << "BOLT-DEBUG: size of .eh_frame after merge is "
5481                     << EHFrameSection->getOutputSize() << '\n');
5482 }
5483 
5484 uint64_t RewriteInstance::getNewValueForSymbol(const StringRef Name) {
5485   uint64_t Value = RTDyld->getSymbol(Name).getAddress();
5486   if (Value != 0)
5487     return Value;
5488 
5489   // Return the original value if we haven't emitted the symbol.
5490   BinaryData *BD = BC->getBinaryDataByName(Name);
5491   if (!BD)
5492     return 0;
5493 
5494   return BD->getAddress();
5495 }
5496 
5497 uint64_t RewriteInstance::getFileOffsetForAddress(uint64_t Address) const {
5498   // Check if it's possibly part of the new segment.
5499   if (Address >= NewTextSegmentAddress)
5500     return Address - NewTextSegmentAddress + NewTextSegmentOffset;
5501 
5502   // Find an existing segment that matches the address.
5503   const auto SegmentInfoI = BC->SegmentMapInfo.upper_bound(Address);
5504   if (SegmentInfoI == BC->SegmentMapInfo.begin())
5505     return 0;
5506 
5507   const SegmentInfo &SegmentInfo = std::prev(SegmentInfoI)->second;
5508   if (Address < SegmentInfo.Address ||
5509       Address >= SegmentInfo.Address + SegmentInfo.FileSize)
5510     return 0;
5511 
5512   return SegmentInfo.FileOffset + Address - SegmentInfo.Address;
5513 }
5514 
5515 bool RewriteInstance::willOverwriteSection(StringRef SectionName) {
5516   for (const char *const &OverwriteName : SectionsToOverwrite)
5517     if (SectionName == OverwriteName)
5518       return true;
5519   for (std::string &OverwriteName : DebugSectionsToOverwrite)
5520     if (SectionName == OverwriteName)
5521       return true;
5522 
5523   ErrorOr<BinarySection &> Section = BC->getUniqueSectionByName(SectionName);
5524   return Section && Section->isAllocatable() && Section->isFinalized();
5525 }
5526 
5527 bool RewriteInstance::isDebugSection(StringRef SectionName) {
5528   if (SectionName.startswith(".debug_") || SectionName.startswith(".zdebug_") ||
5529       SectionName == ".gdb_index" || SectionName == ".stab" ||
5530       SectionName == ".stabstr")
5531     return true;
5532 
5533   return false;
5534 }
5535 
5536 bool RewriteInstance::isKSymtabSection(StringRef SectionName) {
5537   if (SectionName.startswith("__ksymtab"))
5538     return true;
5539 
5540   return false;
5541 }
5542