LLVM 23.0.0git
InstrumentationMap.cpp
Go to the documentation of this file.
1//===- InstrumentationMap.cpp - XRay Instrumentation Map ------------------===//
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// Implementation of the InstrumentationMap type for XRay sleds.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/Object/Binary.h"
23#include "llvm/Support/Error.h"
27#include <cstddef>
28#include <cstdint>
29#include <system_error>
30#include <vector>
31
32using namespace llvm;
33using namespace xray;
34
35std::optional<int32_t> InstrumentationMap::getFunctionId(uint64_t Addr) const {
36 auto I = FunctionIds.find(Addr);
37 if (I != FunctionIds.end())
38 return I->second;
39 return std::nullopt;
40}
41
42std::optional<uint64_t>
44 auto I = FunctionAddresses.find(FuncId);
45 if (I != FunctionAddresses.end())
46 return I->second;
47 return std::nullopt;
48}
49
51
52static Error
58
59 // Find the section named "xray_instr_map".
60 if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) ||
61 !(ObjFile.getBinary()->getArch() == Triple::x86_64 ||
62 ObjFile.getBinary()->getArch() == Triple::loongarch64 ||
63 ObjFile.getBinary()->getArch() == Triple::ppc64le ||
64 ObjFile.getBinary()->getArch() == Triple::arm ||
65 ObjFile.getBinary()->getArch() == Triple::aarch64 ||
66 ObjFile.getBinary()->getArch() == Triple::riscv64))
68 "File format not supported (only does ELF and Mach-O little endian "
69 "64-bit).",
70 std::make_error_code(std::errc::not_supported));
71
72 StringRef Contents = "";
73 const auto &Sections = ObjFile.getBinary()->sections();
74 uint64_t Address = 0;
75 auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
76 Expected<StringRef> NameOrErr = Section.getName();
77 if (NameOrErr) {
78 Address = Section.getAddress();
79 return *NameOrErr == "xray_instr_map";
80 }
81 consumeError(NameOrErr.takeError());
82 return false;
83 });
84
85 if (I == Sections.end())
87 "Failed to find XRay instrumentation map.",
88 std::make_error_code(std::errc::executable_format_error));
89
90 if (Error E = I->getContents().moveInto(Contents))
91 return E;
92
93 RelocMap Relocs;
94 if (ObjFile.getBinary()->isELF()) {
95 uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) {
96 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
97 return ELFObj->getELFFile().getRelativeRelocationType();
98 else if (const auto *ELFObj =
100 return ELFObj->getELFFile().getRelativeRelocationType();
101 else if (const auto *ELFObj =
103 return ELFObj->getELFFile().getRelativeRelocationType();
104 else if (const auto *ELFObj =
106 return ELFObj->getELFFile().getRelativeRelocationType();
107 else
108 return static_cast<uint32_t>(0);
109 }(ObjFile.getBinary());
110
113 std::tie(Supports, Resolver) =
115
116 for (const object::SectionRef &Section : Sections) {
117 for (const object::RelocationRef &Reloc : Section.relocations()) {
118 if (ObjFile.getBinary()->getArch() == Triple::arm) {
119 if (Supports && Supports(Reloc.getType())) {
120 Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
121 if (!ValueOrErr)
122 return ValueOrErr.takeError();
123 Relocs.insert(
124 {Reloc.getOffset(),
125 object::resolveRelocation(Resolver, Reloc, *ValueOrErr, 0)});
126 }
127 } else if (Supports && Supports(Reloc.getType())) {
128 auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend();
129 auto A = AddendOrErr ? *AddendOrErr : 0;
130 Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
131 if (!ValueOrErr)
132 // TODO: Test this error.
133 return ValueOrErr.takeError();
134 Relocs.insert(
135 {Reloc.getOffset(),
136 object::resolveRelocation(Resolver, Reloc, *ValueOrErr, A)});
137 } else if (Reloc.getType() == RelativeRelocation) {
138 if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
139 Relocs.insert({Reloc.getOffset(), *AddendOrErr});
140 }
141 }
142 }
143 }
144
145 // Copy the instrumentation map data into the Sleds data structure.
146 auto C = Contents.bytes_begin();
147 bool Is32Bit = ObjFile.getBinary()->makeTriple().isArch32Bit();
148 size_t ELFSledEntrySize = Is32Bit ? 16 : 32;
149
150 if ((C - Contents.bytes_end()) % ELFSledEntrySize != 0)
152 Twine("Instrumentation map entries not evenly divisible by size of "
153 "an XRay sled entry."),
154 std::make_error_code(std::errc::executable_format_error));
155
156 auto RelocateOrElse = [&](uint64_t Offset, uint64_t Address) {
157 if (!Address) {
158 uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
159 RelocMap::const_iterator R = Relocs.find(A);
160 if (R != Relocs.end())
161 return R->second;
162 }
163 return Address;
164 };
165
166 const int WordSize = Is32Bit ? 4 : 8;
167 int32_t FuncId = 1;
168 uint64_t CurFn = 0;
169 for (; C != Contents.bytes_end(); C += ELFSledEntrySize) {
170 DataExtractor Extractor(ArrayRef<uint8_t>(C, ELFSledEntrySize), true);
171 Sleds.push_back({});
172 auto &Entry = Sleds.back();
173 uint64_t OffsetPtr = 0;
174 uint64_t AddrOff = OffsetPtr;
175 if (Is32Bit)
176 Entry.Address = RelocateOrElse(AddrOff, Extractor.getU32(&OffsetPtr));
177 else
178 Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(&OffsetPtr));
179 uint64_t FuncOff = OffsetPtr;
180 if (Is32Bit)
181 Entry.Function = RelocateOrElse(FuncOff, Extractor.getU32(&OffsetPtr));
182 else
183 Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(&OffsetPtr));
184 auto Kind = Extractor.getU8(&OffsetPtr);
185 static constexpr SledEntry::FunctionKinds Kinds[] = {
190 if (Kind >= std::size(Kinds))
191 return errorCodeToError(
192 std::make_error_code(std::errc::executable_format_error));
193 Entry.Kind = Kinds[Kind];
194 Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
195 Entry.Version = Extractor.getU8(&OffsetPtr);
196 if (Entry.Version >= 2) {
197 Entry.Address += C - Contents.bytes_begin() + Address;
198 Entry.Function += C - Contents.bytes_begin() + WordSize + Address;
199 }
200
201 // We do replicate the function id generation scheme implemented in the
202 // XRay runtime.
203 // FIXME: Figure out how to keep this consistent with the XRay runtime.
204 if (CurFn == 0) {
205 CurFn = Entry.Function;
206 FunctionAddresses[FuncId] = Entry.Function;
207 FunctionIds[Entry.Function] = FuncId;
208 }
209 if (Entry.Function != CurFn) {
210 ++FuncId;
211 CurFn = Entry.Function;
212 FunctionAddresses[FuncId] = Entry.Function;
213 FunctionIds[Entry.Function] = FuncId;
214 }
215 }
216 return Error::success();
217}
218
219static Error
224 std::error_code EC;
228 if (EC)
230 Twine("Failed memory-mapping file '") + Filename + "'.", EC);
231
232 std::vector<YAMLXRaySledEntry> YAMLSleds;
233 yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
234 In >> YAMLSleds;
235 if (In.error())
237 Twine("Failed loading YAML document from '") + Filename + "'.",
238 In.error());
239
240 Sleds.reserve(YAMLSleds.size());
241 for (const auto &Y : YAMLSleds) {
242 FunctionAddresses[Y.FuncId] = Y.Function;
243 FunctionIds[Y.Function] = Y.FuncId;
244 Sleds.push_back(SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument,
245 Y.Version});
246 }
247 return Error::success();
248}
249
250// FIXME: Create error types that encapsulate a bit more information than what
251// StringError instances contain.
252Expected<InstrumentationMap>
254 // At this point we assume the file is an object file -- and if that doesn't
255 // work, we treat it as YAML.
256 // FIXME: Extend to support non-ELF and non-x86_64 binaries.
257
259 auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
260 if (!ObjectFileOrError) {
261 auto E = ObjectFileOrError.takeError();
262 // We try to load it as YAML if the ELF load didn't work.
265 if (!FdOrErr) {
266 // Report the ELF load error if YAML failed.
267 consumeError(FdOrErr.takeError());
268 return std::move(E);
269 }
270
271 uint64_t FileSize;
272 if (sys::fs::file_size(Filename, FileSize))
273 return std::move(E);
274
275 // If the file is empty, we return the original error.
276 if (FileSize == 0)
277 return std::move(E);
278
279 // From this point on the errors will be only for the YAML parts, so we
280 // consume the errors at this point.
281 consumeError(std::move(E));
282 if (auto E = loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,
283 Map.FunctionAddresses, Map.FunctionIds))
284 return std::move(E);
285 } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
286 Map.FunctionAddresses, Map.FunctionIds)) {
287 return std::move(E);
288 }
289 return Map;
290}
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
static void getAddend(uint64_t &, const Elf_Rel_Impl< ELFT, false > &)
static Error loadObj(StringRef Filename, object::OwningBinary< object::ObjectFile > &ObjFile, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
static Error loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
DenseMap< uint64_t, uint64_t > RelocMap
#define I(x, y, z)
Definition MD5.cpp:57
static constexpr StringLiteral Filename
This file contains some templates that are useful if you are working with the STL at all.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition Record.h:2199
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
const unsigned char * bytes_end() const
Definition StringRef.h:124
const unsigned char * bytes_begin() const
Definition StringRef.h:121
@ loongarch64
Definition Triple.h:65
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Expected< int64_t > getAddend() const
This class is the base class for all object file types.
Definition ObjectFile.h:231
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition ObjectFile.h:54
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
This class represents a memory mapped file.
LLVM_ABI size_t size() const
Definition Path.cpp:1189
@ readonly
May only access map via const_data as read only.
LLVM_ABI char * data() const
Definition Path.cpp:1194
The InstrumentationMap represents the computed function id's and indicated function addresses from an...
std::unordered_map< int32_t, uint64_t > FunctionAddressMap
std::unordered_map< uint64_t, int32_t > FunctionAddressReverseMap
std::vector< SledEntry > SledContainer
LLVM_ABI std::optional< int32_t > getFunctionId(uint64_t Addr) const
Returns an XRay computed function id, provided a function address.
LLVM_ABI std::optional< uint64_t > getFunctionAddr(int32_t FuncId) const
Returns the function address for a function id.
The Input class is used to parse a yaml document into in-memory structs and vectors.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
uint64_t(*)(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend) RelocationResolver
LLVM_ABI std::pair< SupportsRelocation, RelocationResolver > getRelocationResolver(const ObjectFile &Obj)
bool(*)(uint64_t) SupportsRelocation
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
std::error_code file_size(const Twine &Path, uint64_t &Result)
Get file size.
Definition FileSystem.h:706
LLVM_ABI Expected< InstrumentationMap > loadInstrumentationMap(StringRef Filename)
Loads the instrumentation map from |Filename|.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
Represents an XRay instrumentation sled entry from an object file.
FunctionKinds
Each entry here represents the kinds of supported instrumentation map entries.