xref: /llvm-project/llvm/lib/XRay/InstrumentationMap.cpp (revision 405fc404bf84fcc13e10bfac754d398199f69b7d)
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/Triple.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Object/RelocationResolver.h"
23 #include "llvm/Support/DataExtractor.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/YAMLTraits.h"
27 #include <algorithm>
28 #include <cstddef>
29 #include <cstdint>
30 #include <system_error>
31 #include <vector>
32 
33 using namespace llvm;
34 using namespace xray;
35 
36 Optional<int32_t> InstrumentationMap::getFunctionId(uint64_t Addr) const {
37   auto I = FunctionIds.find(Addr);
38   if (I != FunctionIds.end())
39     return I->second;
40   return std::nullopt;
41 }
42 
43 Optional<uint64_t> InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
44   auto I = FunctionAddresses.find(FuncId);
45   if (I != FunctionAddresses.end())
46     return I->second;
47   return std::nullopt;
48 }
49 
50 using RelocMap = DenseMap<uint64_t, uint64_t>;
51 
52 static Error
53 loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
54         InstrumentationMap::SledContainer &Sleds,
55         InstrumentationMap::FunctionAddressMap &FunctionAddresses,
56         InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
57   InstrumentationMap Map;
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::ppc64le ||
63         ObjFile.getBinary()->getArch() == Triple::arm ||
64         ObjFile.getBinary()->getArch() == Triple::aarch64))
65     return make_error<StringError>(
66         "File format not supported (only does ELF and Mach-O little endian "
67         "64-bit).",
68         std::make_error_code(std::errc::not_supported));
69 
70   StringRef Contents = "";
71   const auto &Sections = ObjFile.getBinary()->sections();
72   uint64_t Address = 0;
73   auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
74     Expected<StringRef> NameOrErr = Section.getName();
75     if (NameOrErr) {
76       Address = Section.getAddress();
77       return *NameOrErr == "xray_instr_map";
78     }
79     consumeError(NameOrErr.takeError());
80     return false;
81   });
82 
83   if (I == Sections.end())
84     return make_error<StringError>(
85         "Failed to find XRay instrumentation map.",
86         std::make_error_code(std::errc::executable_format_error));
87 
88   if (Error E = I->getContents().moveInto(Contents))
89     return E;
90 
91   RelocMap Relocs;
92   if (ObjFile.getBinary()->isELF()) {
93     uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) {
94       if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
95         return ELFObj->getELFFile().getRelativeRelocationType();
96       else if (const auto *ELFObj =
97                    dyn_cast<object::ELF32BEObjectFile>(ObjFile))
98         return ELFObj->getELFFile().getRelativeRelocationType();
99       else if (const auto *ELFObj =
100                    dyn_cast<object::ELF64LEObjectFile>(ObjFile))
101         return ELFObj->getELFFile().getRelativeRelocationType();
102       else if (const auto *ELFObj =
103                    dyn_cast<object::ELF64BEObjectFile>(ObjFile))
104         return ELFObj->getELFFile().getRelativeRelocationType();
105       else
106         return static_cast<uint32_t>(0);
107     }(ObjFile.getBinary());
108 
109     object::SupportsRelocation Supports;
110     object::RelocationResolver Resolver;
111     std::tie(Supports, Resolver) =
112         object::getRelocationResolver(*ObjFile.getBinary());
113 
114     for (const object::SectionRef &Section : Sections) {
115       for (const object::RelocationRef &Reloc : Section.relocations()) {
116         if (ObjFile.getBinary()->getArch() == Triple::arm) {
117           if (Supports && Supports(Reloc.getType())) {
118             Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
119             if (!ValueOrErr)
120               return ValueOrErr.takeError();
121             Relocs.insert(
122                 {Reloc.getOffset(),
123                  object::resolveRelocation(Resolver, Reloc, *ValueOrErr, 0)});
124           }
125         } else if (Supports && Supports(Reloc.getType())) {
126           auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend();
127           auto A = AddendOrErr ? *AddendOrErr : 0;
128           Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
129           if (!ValueOrErr)
130             // TODO: Test this error.
131             return ValueOrErr.takeError();
132           Relocs.insert(
133               {Reloc.getOffset(),
134                object::resolveRelocation(Resolver, Reloc, *ValueOrErr, A)});
135         } else if (Reloc.getType() == RelativeRelocation) {
136           if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
137             Relocs.insert({Reloc.getOffset(), *AddendOrErr});
138         }
139       }
140     }
141   }
142 
143   // Copy the instrumentation map data into the Sleds data structure.
144   auto C = Contents.bytes_begin();
145   bool Is32Bit = ObjFile.getBinary()->makeTriple().isArch32Bit();
146   size_t ELFSledEntrySize = Is32Bit ? 16 : 32;
147 
148   if ((C - Contents.bytes_end()) % ELFSledEntrySize != 0)
149     return make_error<StringError>(
150         Twine("Instrumentation map entries not evenly divisible by size of "
151               "an XRay sled entry."),
152         std::make_error_code(std::errc::executable_format_error));
153 
154   auto RelocateOrElse = [&](uint64_t Offset, uint64_t Address) {
155     if (!Address) {
156       uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
157       RelocMap::const_iterator R = Relocs.find(A);
158       if (R != Relocs.end())
159         return R->second;
160     }
161     return Address;
162   };
163 
164   const int WordSize = Is32Bit ? 4 : 8;
165   int32_t FuncId = 1;
166   uint64_t CurFn = 0;
167   for (; C != Contents.bytes_end(); C += ELFSledEntrySize) {
168     DataExtractor Extractor(
169         StringRef(reinterpret_cast<const char *>(C), ELFSledEntrySize), true,
170         8);
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[] = {
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           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 
219 static Error
220 loadYAML(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(Fd);
228   if (EC)
229     return make_error<StringError>(
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())
236     return make_error<StringError>(
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.
252 Expected<InstrumentationMap>
253 llvm::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(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(Filename);
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 }
291