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
13#include "llvm/XRay/InstrumentationMap.h"
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"
19#include "llvm/Object/ELFObjectFile.h"
20#include "llvm/Object/ObjectFile.h"
21#include "llvm/Object/RelocationResolver.h"
22#include "llvm/Support/DataExtractor.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/YAMLTraits.h"
26#include "llvm/TargetParser/Triple.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(Val: Addr);
37 if (I != FunctionIds.end())
38 return I->second;
39 return std::nullopt;
40}
41
42std::optional<uint64_t>
43InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
44 auto I = FunctionAddresses.find(Val: FuncId);
45 if (I != FunctionAddresses.end())
46 return I->second;
47 return std::nullopt;
48}
49
50using RelocMap = DenseMap<uint64_t, uint64_t>;
51
52static Error
53loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
54 InstrumentationMap::SledContainer &Sleds,
55 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
56 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
57 // Find the section named "xray_instr_map".
58 if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) ||
59 !(ObjFile.getBinary()->getArch() == Triple::x86_64 ||
60 ObjFile.getBinary()->getArch() == Triple::loongarch64 ||
61 ObjFile.getBinary()->getArch() == Triple::ppc64le ||
62 ObjFile.getBinary()->getArch() == Triple::arm ||
63 ObjFile.getBinary()->getArch() == Triple::aarch64 ||
64 ObjFile.getBinary()->getArch() == Triple::riscv64 ||
65 ObjFile.getBinary()->getArch() == Triple::systemz))
66 return make_error<StringError>(
67 Args: "File format not supported (only does ELF and Mach-O little endian "
68 "64-bit).",
69 Args: std::make_error_code(e: std::errc::not_supported));
70
71 StringRef Contents = "";
72 const auto &Sections = ObjFile.getBinary()->sections();
73 uint64_t Address = 0;
74 auto I = llvm::find_if(Range: Sections, P: [&](object::SectionRef Section) {
75 Expected<StringRef> NameOrErr = Section.getName();
76 if (NameOrErr) {
77 Address = Section.getAddress();
78 return *NameOrErr == "xray_instr_map";
79 }
80 consumeError(Err: NameOrErr.takeError());
81 return false;
82 });
83
84 if (I == Sections.end())
85 return make_error<StringError>(
86 Args: "Failed to find XRay instrumentation map.",
87 Args: std::make_error_code(e: std::errc::executable_format_error));
88
89 if (Error E = I->getContents().moveInto(Value&: Contents))
90 return E;
91
92 RelocMap Relocs;
93 if (ObjFile.getBinary()->isELF()) {
94 uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) {
95 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(Val: ObjFile))
96 return ELFObj->getELFFile().getRelativeRelocationType();
97 else if (const auto *ELFObj =
98 dyn_cast<object::ELF32BEObjectFile>(Val: ObjFile))
99 return ELFObj->getELFFile().getRelativeRelocationType();
100 else if (const auto *ELFObj =
101 dyn_cast<object::ELF64LEObjectFile>(Val: ObjFile))
102 return ELFObj->getELFFile().getRelativeRelocationType();
103 else if (const auto *ELFObj =
104 dyn_cast<object::ELF64BEObjectFile>(Val: ObjFile))
105 return ELFObj->getELFFile().getRelativeRelocationType();
106 else
107 return static_cast<uint32_t>(0);
108 }(ObjFile.getBinary());
109
110 object::SupportsRelocation Supports;
111 object::RelocationResolver Resolver;
112 std::tie(args&: Supports, args&: Resolver) =
113 object::getRelocationResolver(Obj: *ObjFile.getBinary());
114
115 for (const object::SectionRef &Section : Sections) {
116 for (const object::RelocationRef &Reloc : Section.relocations()) {
117 if (ObjFile.getBinary()->getArch() == Triple::arm) {
118 if (Supports && Supports(Reloc.getType())) {
119 Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
120 if (!ValueOrErr)
121 return ValueOrErr.takeError();
122 Relocs.insert(
123 KV: {Reloc.getOffset(),
124 object::resolveRelocation(Resolver, R: Reloc, S: *ValueOrErr, LocData: 0)});
125 }
126 } else if (Supports && Supports(Reloc.getType())) {
127 auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend();
128 auto A = AddendOrErr ? *AddendOrErr : 0;
129 Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
130 if (!ValueOrErr)
131 // TODO: Test this error.
132 return ValueOrErr.takeError();
133 Relocs.insert(
134 KV: {Reloc.getOffset(),
135 object::resolveRelocation(Resolver, R: Reloc, S: *ValueOrErr, LocData: A)});
136 } else if (Reloc.getType() == RelativeRelocation) {
137 if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
138 Relocs.insert(KV: {Reloc.getOffset(), *AddendOrErr});
139 }
140 }
141 }
142 }
143
144 // Copy the instrumentation map data into the Sleds data structure.
145 auto C = Contents.bytes_begin();
146 bool Is32Bit = ObjFile.getBinary()->makeTriple().isArch32Bit();
147 size_t ELFSledEntrySize = Is32Bit ? 16 : 32;
148
149 if ((C - Contents.bytes_end()) % ELFSledEntrySize != 0)
150 return make_error<StringError>(
151 Args: Twine("Instrumentation map entries not evenly divisible by size of "
152 "an XRay sled entry."),
153 Args: std::make_error_code(e: std::errc::executable_format_error));
154
155 auto RelocateOrElse = [&](uint64_t Offset, uint64_t Address) {
156 if (!Address) {
157 uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
158 RelocMap::const_iterator R = Relocs.find(Val: A);
159 if (R != Relocs.end())
160 return R->second;
161 }
162 return Address;
163 };
164
165 const int WordSize = Is32Bit ? 4 : 8;
166 int32_t FuncId = 1;
167 uint64_t CurFn = 0;
168 for (; C != Contents.bytes_end(); C += ELFSledEntrySize) {
169 DataExtractor Extractor(ArrayRef<uint8_t>(C, ELFSledEntrySize),
170 ObjFile.getBinary()->isLittleEndian());
171 Sleds.push_back(x: {});
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(offset_ptr: &OffsetPtr));
177 else
178 Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(offset_ptr: &OffsetPtr));
179 uint64_t FuncOff = OffsetPtr;
180 if (Is32Bit)
181 Entry.Function = RelocateOrElse(FuncOff, Extractor.getU32(offset_ptr: &OffsetPtr));
182 else
183 Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(offset_ptr: &OffsetPtr));
184 auto Kind = Extractor.getU8(offset_ptr: &OffsetPtr);
185 static constexpr SledEntry::FunctionKinds Kinds[] = {
186 SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
187 SledEntry::FunctionKinds::TAIL,
188 SledEntry::FunctionKinds::LOG_ARGS_ENTER,
189 SledEntry::FunctionKinds::CUSTOM_EVENT};
190 if (Kind >= std::size(Kinds))
191 return errorCodeToError(
192 EC: std::make_error_code(e: std::errc::executable_format_error));
193 Entry.Kind = Kinds[Kind];
194 Entry.AlwaysInstrument = Extractor.getU8(offset_ptr: &OffsetPtr) != 0;
195 Entry.Version = Extractor.getU8(offset_ptr: &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
220loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename,
221 InstrumentationMap::SledContainer &Sleds,
222 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
223 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
224 std::error_code EC;
225 sys::fs::mapped_file_region MappedFile(
226 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
227 sys::fs::closeFile(F&: Fd);
228 if (EC)
229 return make_error<StringError>(
230 Args: Twine("Failed memory-mapping file '") + Filename + "'.", Args&: EC);
231
232 std::vector<YAMLXRaySledEntry> YAMLSleds;
233 yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
234 In >> YAMLSleds;
235 if (In.error())
236 return make_error<StringError>(
237 Args: Twine("Failed loading YAML document from '") + Filename + "'.",
238 Args: In.error());
239
240 Sleds.reserve(n: YAMLSleds.size());
241 for (const auto &Y : YAMLSleds) {
242 FunctionAddresses[Y.FuncId] = Y.Function;
243 FunctionIds[Y.Function] = Y.FuncId;
244 Sleds.push_back(x: SledEntry{.Address: Y.Address, .Function: Y.Function, .Kind: Y.Kind, .AlwaysInstrument: Y.AlwaysInstrument,
245 .Version: 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>
253llvm::xray::loadInstrumentationMap(StringRef Filename) {
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
258 InstrumentationMap Map;
259 auto ObjectFileOrError = object::ObjectFile::createObjectFile(ObjectPath: Filename);
260 if (!ObjectFileOrError) {
261 auto E = ObjectFileOrError.takeError();
262 // We try to load it as YAML if the ELF load didn't work.
263 Expected<sys::fs::file_t> FdOrErr =
264 sys::fs::openNativeFileForRead(Name: Filename);
265 if (!FdOrErr) {
266 // Report the ELF load error if YAML failed.
267 consumeError(Err: FdOrErr.takeError());
268 return std::move(E);
269 }
270
271 uint64_t FileSize;
272 if (sys::fs::file_size(Path: Filename, Result&: 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(Err: std::move(E));
282 if (auto E = loadYAML(Fd: *FdOrErr, FileSize, Filename, Sleds&: Map.Sleds,
283 FunctionAddresses&: Map.FunctionAddresses, FunctionIds&: Map.FunctionIds))
284 return std::move(E);
285 } else if (auto E = loadObj(Filename, ObjFile&: *ObjectFileOrError, Sleds&: Map.Sleds,
286 FunctionAddresses&: Map.FunctionAddresses, FunctionIds&: Map.FunctionIds)) {
287 return std::move(E);
288 }
289 return Map;
290}
291