xref: /freebsd-src/contrib/llvm-project/llvm/lib/XRay/InstrumentationMap.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- InstrumentationMap.cpp - XRay Instrumentation Map ------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implementation of the InstrumentationMap type for XRay sleds.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/XRay/InstrumentationMap.h"
140b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
150b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
170b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
180b57cec5SDimitry Andric #include "llvm/Object/Binary.h"
190b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h"
200b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
21480093f4SDimitry Andric #include "llvm/Object/RelocationResolver.h"
220b57cec5SDimitry Andric #include "llvm/Support/DataExtractor.h"
230b57cec5SDimitry Andric #include "llvm/Support/Error.h"
240b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
250b57cec5SDimitry Andric #include "llvm/Support/YAMLTraits.h"
26*06c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
270b57cec5SDimitry Andric #include <algorithm>
280b57cec5SDimitry Andric #include <cstddef>
290b57cec5SDimitry Andric #include <cstdint>
300b57cec5SDimitry Andric #include <system_error>
310b57cec5SDimitry Andric #include <vector>
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric using namespace llvm;
340b57cec5SDimitry Andric using namespace xray;
350b57cec5SDimitry Andric 
getFunctionId(uint64_t Addr) const36bdd1243dSDimitry Andric std::optional<int32_t> InstrumentationMap::getFunctionId(uint64_t Addr) const {
370b57cec5SDimitry Andric   auto I = FunctionIds.find(Addr);
380b57cec5SDimitry Andric   if (I != FunctionIds.end())
390b57cec5SDimitry Andric     return I->second;
40bdd1243dSDimitry Andric   return std::nullopt;
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
43bdd1243dSDimitry Andric std::optional<uint64_t>
getFunctionAddr(int32_t FuncId) const44bdd1243dSDimitry Andric InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
450b57cec5SDimitry Andric   auto I = FunctionAddresses.find(FuncId);
460b57cec5SDimitry Andric   if (I != FunctionAddresses.end())
470b57cec5SDimitry Andric     return I->second;
48bdd1243dSDimitry Andric   return std::nullopt;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric using RelocMap = DenseMap<uint64_t, uint64_t>;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric static Error
loadObj(StringRef Filename,object::OwningBinary<object::ObjectFile> & ObjFile,InstrumentationMap::SledContainer & Sleds,InstrumentationMap::FunctionAddressMap & FunctionAddresses,InstrumentationMap::FunctionAddressReverseMap & FunctionIds)540b57cec5SDimitry Andric loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
550b57cec5SDimitry Andric         InstrumentationMap::SledContainer &Sleds,
560b57cec5SDimitry Andric         InstrumentationMap::FunctionAddressMap &FunctionAddresses,
570b57cec5SDimitry Andric         InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
580b57cec5SDimitry Andric   InstrumentationMap Map;
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   // Find the section named "xray_instr_map".
610b57cec5SDimitry Andric   if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) ||
620b57cec5SDimitry Andric       !(ObjFile.getBinary()->getArch() == Triple::x86_64 ||
63*06c3fb27SDimitry Andric         ObjFile.getBinary()->getArch() == Triple::loongarch64 ||
64480093f4SDimitry Andric         ObjFile.getBinary()->getArch() == Triple::ppc64le ||
655ffd83dbSDimitry Andric         ObjFile.getBinary()->getArch() == Triple::arm ||
66480093f4SDimitry Andric         ObjFile.getBinary()->getArch() == Triple::aarch64))
670b57cec5SDimitry Andric     return make_error<StringError>(
685ffd83dbSDimitry Andric         "File format not supported (only does ELF and Mach-O little endian "
695ffd83dbSDimitry Andric         "64-bit).",
700b57cec5SDimitry Andric         std::make_error_code(std::errc::not_supported));
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   StringRef Contents = "";
730b57cec5SDimitry Andric   const auto &Sections = ObjFile.getBinary()->sections();
745ffd83dbSDimitry Andric   uint64_t Address = 0;
750b57cec5SDimitry Andric   auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
768bcb0991SDimitry Andric     Expected<StringRef> NameOrErr = Section.getName();
775ffd83dbSDimitry Andric     if (NameOrErr) {
785ffd83dbSDimitry Andric       Address = Section.getAddress();
798bcb0991SDimitry Andric       return *NameOrErr == "xray_instr_map";
805ffd83dbSDimitry Andric     }
818bcb0991SDimitry Andric     consumeError(NameOrErr.takeError());
820b57cec5SDimitry Andric     return false;
830b57cec5SDimitry Andric   });
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   if (I == Sections.end())
860b57cec5SDimitry Andric     return make_error<StringError>(
870b57cec5SDimitry Andric         "Failed to find XRay instrumentation map.",
880b57cec5SDimitry Andric         std::make_error_code(std::errc::executable_format_error));
890b57cec5SDimitry Andric 
90349cc55cSDimitry Andric   if (Error E = I->getContents().moveInto(Contents))
91349cc55cSDimitry Andric     return E;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   RelocMap Relocs;
940b57cec5SDimitry Andric   if (ObjFile.getBinary()->isELF()) {
950b57cec5SDimitry Andric     uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) {
960b57cec5SDimitry Andric       if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
97e8d8bef9SDimitry Andric         return ELFObj->getELFFile().getRelativeRelocationType();
985ffd83dbSDimitry Andric       else if (const auto *ELFObj =
995ffd83dbSDimitry Andric                    dyn_cast<object::ELF32BEObjectFile>(ObjFile))
100e8d8bef9SDimitry Andric         return ELFObj->getELFFile().getRelativeRelocationType();
1015ffd83dbSDimitry Andric       else if (const auto *ELFObj =
1025ffd83dbSDimitry Andric                    dyn_cast<object::ELF64LEObjectFile>(ObjFile))
103e8d8bef9SDimitry Andric         return ELFObj->getELFFile().getRelativeRelocationType();
1045ffd83dbSDimitry Andric       else if (const auto *ELFObj =
1055ffd83dbSDimitry Andric                    dyn_cast<object::ELF64BEObjectFile>(ObjFile))
106e8d8bef9SDimitry Andric         return ELFObj->getELFFile().getRelativeRelocationType();
1070b57cec5SDimitry Andric       else
1080b57cec5SDimitry Andric         return static_cast<uint32_t>(0);
1090b57cec5SDimitry Andric     }(ObjFile.getBinary());
1100b57cec5SDimitry Andric 
111e8d8bef9SDimitry Andric     object::SupportsRelocation Supports;
112480093f4SDimitry Andric     object::RelocationResolver Resolver;
113e8d8bef9SDimitry Andric     std::tie(Supports, Resolver) =
114480093f4SDimitry Andric         object::getRelocationResolver(*ObjFile.getBinary());
115480093f4SDimitry Andric 
1160b57cec5SDimitry Andric     for (const object::SectionRef &Section : Sections) {
1170b57cec5SDimitry Andric       for (const object::RelocationRef &Reloc : Section.relocations()) {
1185ffd83dbSDimitry Andric         if (ObjFile.getBinary()->getArch() == Triple::arm) {
119e8d8bef9SDimitry Andric           if (Supports && Supports(Reloc.getType())) {
1205ffd83dbSDimitry Andric             Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
1215ffd83dbSDimitry Andric             if (!ValueOrErr)
1225ffd83dbSDimitry Andric               return ValueOrErr.takeError();
123e8d8bef9SDimitry Andric             Relocs.insert(
124e8d8bef9SDimitry Andric                 {Reloc.getOffset(),
125e8d8bef9SDimitry Andric                  object::resolveRelocation(Resolver, Reloc, *ValueOrErr, 0)});
1265ffd83dbSDimitry Andric           }
127e8d8bef9SDimitry Andric         } else if (Supports && Supports(Reloc.getType())) {
128480093f4SDimitry Andric           auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend();
129480093f4SDimitry Andric           auto A = AddendOrErr ? *AddendOrErr : 0;
1305ffd83dbSDimitry Andric           Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
1315ffd83dbSDimitry Andric           if (!ValueOrErr)
1325ffd83dbSDimitry Andric             // TODO: Test this error.
1335ffd83dbSDimitry Andric             return ValueOrErr.takeError();
134e8d8bef9SDimitry Andric           Relocs.insert(
135e8d8bef9SDimitry Andric               {Reloc.getOffset(),
136e8d8bef9SDimitry Andric                object::resolveRelocation(Resolver, Reloc, *ValueOrErr, A)});
137480093f4SDimitry Andric         } else if (Reloc.getType() == RelativeRelocation) {
1380b57cec5SDimitry Andric           if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
1390b57cec5SDimitry Andric             Relocs.insert({Reloc.getOffset(), *AddendOrErr});
1400b57cec5SDimitry Andric         }
1410b57cec5SDimitry Andric       }
1420b57cec5SDimitry Andric     }
143480093f4SDimitry Andric   }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   // Copy the instrumentation map data into the Sleds data structure.
1460b57cec5SDimitry Andric   auto C = Contents.bytes_begin();
1475ffd83dbSDimitry Andric   bool Is32Bit = ObjFile.getBinary()->makeTriple().isArch32Bit();
1485ffd83dbSDimitry Andric   size_t ELFSledEntrySize = Is32Bit ? 16 : 32;
1490b57cec5SDimitry Andric 
1505ffd83dbSDimitry Andric   if ((C - Contents.bytes_end()) % ELFSledEntrySize != 0)
1510b57cec5SDimitry Andric     return make_error<StringError>(
1520b57cec5SDimitry Andric         Twine("Instrumentation map entries not evenly divisible by size of "
1535ffd83dbSDimitry Andric               "an XRay sled entry."),
1540b57cec5SDimitry Andric         std::make_error_code(std::errc::executable_format_error));
1550b57cec5SDimitry Andric 
1568bcb0991SDimitry Andric   auto RelocateOrElse = [&](uint64_t Offset, uint64_t Address) {
1570b57cec5SDimitry Andric     if (!Address) {
1580b57cec5SDimitry Andric       uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
1590b57cec5SDimitry Andric       RelocMap::const_iterator R = Relocs.find(A);
1600b57cec5SDimitry Andric       if (R != Relocs.end())
1610b57cec5SDimitry Andric         return R->second;
1620b57cec5SDimitry Andric     }
1630b57cec5SDimitry Andric     return Address;
1640b57cec5SDimitry Andric   };
1650b57cec5SDimitry Andric 
1665ffd83dbSDimitry Andric   const int WordSize = Is32Bit ? 4 : 8;
1670b57cec5SDimitry Andric   int32_t FuncId = 1;
1680b57cec5SDimitry Andric   uint64_t CurFn = 0;
1695ffd83dbSDimitry Andric   for (; C != Contents.bytes_end(); C += ELFSledEntrySize) {
1700b57cec5SDimitry Andric     DataExtractor Extractor(
1715ffd83dbSDimitry Andric         StringRef(reinterpret_cast<const char *>(C), ELFSledEntrySize), true,
1720b57cec5SDimitry Andric         8);
1730b57cec5SDimitry Andric     Sleds.push_back({});
1740b57cec5SDimitry Andric     auto &Entry = Sleds.back();
1758bcb0991SDimitry Andric     uint64_t OffsetPtr = 0;
1768bcb0991SDimitry Andric     uint64_t AddrOff = OffsetPtr;
1775ffd83dbSDimitry Andric     if (Is32Bit)
1785ffd83dbSDimitry Andric       Entry.Address = RelocateOrElse(AddrOff, Extractor.getU32(&OffsetPtr));
1795ffd83dbSDimitry Andric     else
1800b57cec5SDimitry Andric       Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(&OffsetPtr));
1818bcb0991SDimitry Andric     uint64_t FuncOff = OffsetPtr;
1825ffd83dbSDimitry Andric     if (Is32Bit)
1835ffd83dbSDimitry Andric       Entry.Function = RelocateOrElse(FuncOff, Extractor.getU32(&OffsetPtr));
1845ffd83dbSDimitry Andric     else
1850b57cec5SDimitry Andric       Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(&OffsetPtr));
1860b57cec5SDimitry Andric     auto Kind = Extractor.getU8(&OffsetPtr);
1870b57cec5SDimitry Andric     static constexpr SledEntry::FunctionKinds Kinds[] = {
1880b57cec5SDimitry Andric         SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
1890b57cec5SDimitry Andric         SledEntry::FunctionKinds::TAIL,
1900b57cec5SDimitry Andric         SledEntry::FunctionKinds::LOG_ARGS_ENTER,
1910b57cec5SDimitry Andric         SledEntry::FunctionKinds::CUSTOM_EVENT};
192bdd1243dSDimitry Andric     if (Kind >= std::size(Kinds))
1930b57cec5SDimitry Andric       return errorCodeToError(
1940b57cec5SDimitry Andric           std::make_error_code(std::errc::executable_format_error));
1950b57cec5SDimitry Andric     Entry.Kind = Kinds[Kind];
1960b57cec5SDimitry Andric     Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
1975ffd83dbSDimitry Andric     Entry.Version = Extractor.getU8(&OffsetPtr);
1985ffd83dbSDimitry Andric     if (Entry.Version >= 2) {
1995ffd83dbSDimitry Andric       Entry.Address += C - Contents.bytes_begin() + Address;
2005ffd83dbSDimitry Andric       Entry.Function += C - Contents.bytes_begin() + WordSize + Address;
2015ffd83dbSDimitry Andric     }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric     // We do replicate the function id generation scheme implemented in the
2040b57cec5SDimitry Andric     // XRay runtime.
2050b57cec5SDimitry Andric     // FIXME: Figure out how to keep this consistent with the XRay runtime.
2060b57cec5SDimitry Andric     if (CurFn == 0) {
2070b57cec5SDimitry Andric       CurFn = Entry.Function;
2080b57cec5SDimitry Andric       FunctionAddresses[FuncId] = Entry.Function;
2090b57cec5SDimitry Andric       FunctionIds[Entry.Function] = FuncId;
2100b57cec5SDimitry Andric     }
2110b57cec5SDimitry Andric     if (Entry.Function != CurFn) {
2120b57cec5SDimitry Andric       ++FuncId;
2130b57cec5SDimitry Andric       CurFn = Entry.Function;
2140b57cec5SDimitry Andric       FunctionAddresses[FuncId] = Entry.Function;
2150b57cec5SDimitry Andric       FunctionIds[Entry.Function] = FuncId;
2160b57cec5SDimitry Andric     }
2170b57cec5SDimitry Andric   }
2180b57cec5SDimitry Andric   return Error::success();
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric static Error
loadYAML(sys::fs::file_t Fd,size_t FileSize,StringRef Filename,InstrumentationMap::SledContainer & Sleds,InstrumentationMap::FunctionAddressMap & FunctionAddresses,InstrumentationMap::FunctionAddressReverseMap & FunctionIds)2220b57cec5SDimitry Andric loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename,
2230b57cec5SDimitry Andric          InstrumentationMap::SledContainer &Sleds,
2240b57cec5SDimitry Andric          InstrumentationMap::FunctionAddressMap &FunctionAddresses,
2250b57cec5SDimitry Andric          InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
2260b57cec5SDimitry Andric   std::error_code EC;
2270b57cec5SDimitry Andric   sys::fs::mapped_file_region MappedFile(
2280b57cec5SDimitry Andric       Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
2290b57cec5SDimitry Andric   sys::fs::closeFile(Fd);
2300b57cec5SDimitry Andric   if (EC)
2310b57cec5SDimitry Andric     return make_error<StringError>(
2320b57cec5SDimitry Andric         Twine("Failed memory-mapping file '") + Filename + "'.", EC);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   std::vector<YAMLXRaySledEntry> YAMLSleds;
2350b57cec5SDimitry Andric   yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
2360b57cec5SDimitry Andric   In >> YAMLSleds;
2370b57cec5SDimitry Andric   if (In.error())
2380b57cec5SDimitry Andric     return make_error<StringError>(
2390b57cec5SDimitry Andric         Twine("Failed loading YAML document from '") + Filename + "'.",
2400b57cec5SDimitry Andric         In.error());
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   Sleds.reserve(YAMLSleds.size());
2430b57cec5SDimitry Andric   for (const auto &Y : YAMLSleds) {
2440b57cec5SDimitry Andric     FunctionAddresses[Y.FuncId] = Y.Function;
2450b57cec5SDimitry Andric     FunctionIds[Y.Function] = Y.FuncId;
2465ffd83dbSDimitry Andric     Sleds.push_back(SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument,
2475ffd83dbSDimitry Andric                               Y.Version});
2480b57cec5SDimitry Andric   }
2490b57cec5SDimitry Andric   return Error::success();
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric // FIXME: Create error types that encapsulate a bit more information than what
2530b57cec5SDimitry Andric // StringError instances contain.
2540b57cec5SDimitry Andric Expected<InstrumentationMap>
loadInstrumentationMap(StringRef Filename)2550b57cec5SDimitry Andric llvm::xray::loadInstrumentationMap(StringRef Filename) {
2560b57cec5SDimitry Andric   // At this point we assume the file is an object file -- and if that doesn't
2570b57cec5SDimitry Andric   // work, we treat it as YAML.
2580b57cec5SDimitry Andric   // FIXME: Extend to support non-ELF and non-x86_64 binaries.
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   InstrumentationMap Map;
2610b57cec5SDimitry Andric   auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
2620b57cec5SDimitry Andric   if (!ObjectFileOrError) {
2630b57cec5SDimitry Andric     auto E = ObjectFileOrError.takeError();
2640b57cec5SDimitry Andric     // We try to load it as YAML if the ELF load didn't work.
2655ffd83dbSDimitry Andric     Expected<sys::fs::file_t> FdOrErr =
2665ffd83dbSDimitry Andric         sys::fs::openNativeFileForRead(Filename);
2670b57cec5SDimitry Andric     if (!FdOrErr) {
2680b57cec5SDimitry Andric       // Report the ELF load error if YAML failed.
2690b57cec5SDimitry Andric       consumeError(FdOrErr.takeError());
2700b57cec5SDimitry Andric       return std::move(E);
2710b57cec5SDimitry Andric     }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric     uint64_t FileSize;
2740b57cec5SDimitry Andric     if (sys::fs::file_size(Filename, FileSize))
2750b57cec5SDimitry Andric       return std::move(E);
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric     // If the file is empty, we return the original error.
2780b57cec5SDimitry Andric     if (FileSize == 0)
2790b57cec5SDimitry Andric       return std::move(E);
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric     // From this point on the errors will be only for the YAML parts, so we
2820b57cec5SDimitry Andric     // consume the errors at this point.
2830b57cec5SDimitry Andric     consumeError(std::move(E));
2840b57cec5SDimitry Andric     if (auto E = loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,
2850b57cec5SDimitry Andric                           Map.FunctionAddresses, Map.FunctionIds))
2860b57cec5SDimitry Andric       return std::move(E);
2870b57cec5SDimitry Andric   } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
2880b57cec5SDimitry Andric                               Map.FunctionAddresses, Map.FunctionIds)) {
2890b57cec5SDimitry Andric     return std::move(E);
2900b57cec5SDimitry Andric   }
2910b57cec5SDimitry Andric   return Map;
2920b57cec5SDimitry Andric }
293