1 //===- InstrumentationMap.cpp - XRay Instrumentation Map ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implementation of the InstrumentationMap type for XRay sleds.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/XRay/InstrumentationMap.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Object/ELFObjectFile.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Object/RelocationResolver.h"
24 #include "llvm/Support/DataExtractor.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/YAMLTraits.h"
31 #include <system_error>
37 Optional<int32_t> InstrumentationMap::getFunctionId(uint64_t Addr) const {
38 auto I = FunctionIds.find(Addr);
39 if (I != FunctionIds.end())
44 Optional<uint64_t> InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
45 auto I = FunctionAddresses.find(FuncId);
46 if (I != FunctionAddresses.end())
51 using RelocMap = DenseMap<uint64_t, uint64_t>;
54 loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
55 InstrumentationMap::SledContainer &Sleds,
56 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
57 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
58 InstrumentationMap Map;
60 // Find the section named "xray_instr_map".
61 if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) ||
62 !(ObjFile.getBinary()->getArch() == Triple::x86_64 ||
63 ObjFile.getBinary()->getArch() == Triple::ppc64le ||
64 ObjFile.getBinary()->getArch() == Triple::aarch64))
65 return make_error<StringError>(
66 "File format not supported (only does ELF and Mach-O little endian 64-bit).",
67 std::make_error_code(std::errc::not_supported));
69 StringRef Contents = "";
70 const auto &Sections = ObjFile.getBinary()->sections();
71 auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
72 Expected<StringRef> NameOrErr = Section.getName();
74 return *NameOrErr == "xray_instr_map";
75 consumeError(NameOrErr.takeError());
79 if (I == Sections.end())
80 return make_error<StringError>(
81 "Failed to find XRay instrumentation map.",
82 std::make_error_code(std::errc::executable_format_error));
84 if (Expected<StringRef> E = I->getContents())
90 if (ObjFile.getBinary()->isELF()) {
91 uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) {
92 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
93 return ELFObj->getELFFile()->getRelativeRelocationType();
94 else if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(ObjFile))
95 return ELFObj->getELFFile()->getRelativeRelocationType();
96 else if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(ObjFile))
97 return ELFObj->getELFFile()->getRelativeRelocationType();
98 else if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(ObjFile))
99 return ELFObj->getELFFile()->getRelativeRelocationType();
101 return static_cast<uint32_t>(0);
102 }(ObjFile.getBinary());
104 bool (*SupportsRelocation)(uint64_t);
105 object::RelocationResolver Resolver;
106 std::tie(SupportsRelocation, Resolver) =
107 object::getRelocationResolver(*ObjFile.getBinary());
109 for (const object::SectionRef &Section : Sections) {
110 for (const object::RelocationRef &Reloc : Section.relocations()) {
111 if (SupportsRelocation && SupportsRelocation(Reloc.getType())) {
112 auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend();
113 auto A = AddendOrErr ? *AddendOrErr : 0;
114 uint64_t resolved = Resolver(Reloc, Reloc.getSymbol()->getValue(), A);
115 Relocs.insert({Reloc.getOffset(), resolved});
116 } else if (Reloc.getType() == RelativeRelocation) {
117 if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
118 Relocs.insert({Reloc.getOffset(), *AddendOrErr});
124 // Copy the instrumentation map data into the Sleds data structure.
125 auto C = Contents.bytes_begin();
126 static constexpr size_t ELF64SledEntrySize = 32;
128 if ((C - Contents.bytes_end()) % ELF64SledEntrySize != 0)
129 return make_error<StringError>(
130 Twine("Instrumentation map entries not evenly divisible by size of "
131 "an XRay sled entry in ELF64."),
132 std::make_error_code(std::errc::executable_format_error));
134 auto RelocateOrElse = [&](uint64_t Offset, uint64_t Address) {
136 uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
137 RelocMap::const_iterator R = Relocs.find(A);
138 if (R != Relocs.end())
146 for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) {
147 DataExtractor Extractor(
148 StringRef(reinterpret_cast<const char *>(C), ELF64SledEntrySize), true,
151 auto &Entry = Sleds.back();
152 uint64_t OffsetPtr = 0;
153 uint64_t AddrOff = OffsetPtr;
154 Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(&OffsetPtr));
155 uint64_t FuncOff = OffsetPtr;
156 Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(&OffsetPtr));
157 auto Kind = Extractor.getU8(&OffsetPtr);
158 static constexpr SledEntry::FunctionKinds Kinds[] = {
159 SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
160 SledEntry::FunctionKinds::TAIL,
161 SledEntry::FunctionKinds::LOG_ARGS_ENTER,
162 SledEntry::FunctionKinds::CUSTOM_EVENT};
163 if (Kind >= sizeof(Kinds))
164 return errorCodeToError(
165 std::make_error_code(std::errc::executable_format_error));
166 Entry.Kind = Kinds[Kind];
167 Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
169 // We do replicate the function id generation scheme implemented in the
171 // FIXME: Figure out how to keep this consistent with the XRay runtime.
173 CurFn = Entry.Function;
174 FunctionAddresses[FuncId] = Entry.Function;
175 FunctionIds[Entry.Function] = FuncId;
177 if (Entry.Function != CurFn) {
179 CurFn = Entry.Function;
180 FunctionAddresses[FuncId] = Entry.Function;
181 FunctionIds[Entry.Function] = FuncId;
184 return Error::success();
188 loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename,
189 InstrumentationMap::SledContainer &Sleds,
190 InstrumentationMap::FunctionAddressMap &FunctionAddresses,
191 InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
193 sys::fs::mapped_file_region MappedFile(
194 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
195 sys::fs::closeFile(Fd);
197 return make_error<StringError>(
198 Twine("Failed memory-mapping file '") + Filename + "'.", EC);
200 std::vector<YAMLXRaySledEntry> YAMLSleds;
201 yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
204 return make_error<StringError>(
205 Twine("Failed loading YAML document from '") + Filename + "'.",
208 Sleds.reserve(YAMLSleds.size());
209 for (const auto &Y : YAMLSleds) {
210 FunctionAddresses[Y.FuncId] = Y.Function;
211 FunctionIds[Y.Function] = Y.FuncId;
213 SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument});
215 return Error::success();
218 // FIXME: Create error types that encapsulate a bit more information than what
219 // StringError instances contain.
220 Expected<InstrumentationMap>
221 llvm::xray::loadInstrumentationMap(StringRef Filename) {
222 // At this point we assume the file is an object file -- and if that doesn't
223 // work, we treat it as YAML.
224 // FIXME: Extend to support non-ELF and non-x86_64 binaries.
226 InstrumentationMap Map;
227 auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
228 if (!ObjectFileOrError) {
229 auto E = ObjectFileOrError.takeError();
230 // We try to load it as YAML if the ELF load didn't work.
231 Expected<sys::fs::file_t> FdOrErr = sys::fs::openNativeFileForRead(Filename);
233 // Report the ELF load error if YAML failed.
234 consumeError(FdOrErr.takeError());
239 if (sys::fs::file_size(Filename, FileSize))
242 // If the file is empty, we return the original error.
246 // From this point on the errors will be only for the YAML parts, so we
247 // consume the errors at this point.
248 consumeError(std::move(E));
249 if (auto E = loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,
250 Map.FunctionAddresses, Map.FunctionIds))
252 } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
253 Map.FunctionAddresses, Map.FunctionIds)) {