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/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"
28 #include <algorithm>
29 #include <cstddef>
30 #include <cstdint>
31 #include <system_error>
32 #include <vector>
33 
34 using namespace llvm;
35 using namespace xray;
36 
37 Optional<int32_t> InstrumentationMap::getFunctionId(uint64_t Addr) const {
38   auto I = FunctionIds.find(Addr);
39   if (I != FunctionIds.end())
40     return I->second;
41   return None;
42 }
43 
44 Optional<uint64_t> InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
45   auto I = FunctionAddresses.find(FuncId);
46   if (I != FunctionAddresses.end())
47     return I->second;
48   return None;
49 }
50 
51 using RelocMap = DenseMap<uint64_t, uint64_t>;
52 
53 static Error
54 loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
55           InstrumentationMap::SledContainer &Sleds,
56           InstrumentationMap::FunctionAddressMap &FunctionAddresses,
57           InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
58   InstrumentationMap Map;
59 
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));
68 
69   StringRef Contents = "";
70   const auto &Sections = ObjFile.getBinary()->sections();
71   uint64_t Address = 0;
72   auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
73     Expected<StringRef> NameOrErr = Section.getName();
74     if (NameOrErr) {
75       Address = Section.getAddress();
76       return *NameOrErr == "xray_instr_map";
77     }
78     consumeError(NameOrErr.takeError());
79     return false;
80   });
81 
82   if (I == Sections.end())
83     return make_error<StringError>(
84         "Failed to find XRay instrumentation map.",
85         std::make_error_code(std::errc::executable_format_error));
86 
87   if (Expected<StringRef> E = I->getContents())
88     Contents = *E;
89   else
90     return E.takeError();
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>(ObjFile))
96         return ELFObj->getELFFile()->getRelativeRelocationType();
97       else if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(ObjFile))
98         return ELFObj->getELFFile()->getRelativeRelocationType();
99       else if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(ObjFile))
100         return ELFObj->getELFFile()->getRelativeRelocationType();
101       else if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(ObjFile))
102         return ELFObj->getELFFile()->getRelativeRelocationType();
103       else
104         return static_cast<uint32_t>(0);
105     }(ObjFile.getBinary());
106 
107     bool (*SupportsRelocation)(uint64_t);
108     object::RelocationResolver Resolver;
109     std::tie(SupportsRelocation, Resolver) =
110         object::getRelocationResolver(*ObjFile.getBinary());
111 
112     for (const object::SectionRef &Section : Sections) {
113       for (const object::RelocationRef &Reloc : Section.relocations()) {
114         if (SupportsRelocation && SupportsRelocation(Reloc.getType())) {
115           auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend();
116           auto A = AddendOrErr ? *AddendOrErr : 0;
117           Expected<uint64_t> ValueOrErr = Reloc.getSymbol()->getValue();
118           if (!ValueOrErr)
119             // TODO: Test this error.
120             return ValueOrErr.takeError();
121           Relocs.insert({Reloc.getOffset(), Resolver(Reloc, *ValueOrErr, A)});
122         } else if (Reloc.getType() == RelativeRelocation) {
123           if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
124             Relocs.insert({Reloc.getOffset(), *AddendOrErr});
125         }
126       }
127     }
128   }
129 
130   // Copy the instrumentation map data into the Sleds data structure.
131   auto C = Contents.bytes_begin();
132   static constexpr size_t ELF64SledEntrySize = 32;
133 
134   if ((C - Contents.bytes_end()) % ELF64SledEntrySize != 0)
135     return make_error<StringError>(
136         Twine("Instrumentation map entries not evenly divisible by size of "
137               "an XRay sled entry in ELF64."),
138         std::make_error_code(std::errc::executable_format_error));
139 
140   auto RelocateOrElse = [&](uint64_t Offset, uint64_t Address) {
141     if (!Address) {
142       uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
143       RelocMap::const_iterator R = Relocs.find(A);
144       if (R != Relocs.end())
145         return R->second;
146     }
147     return Address;
148   };
149 
150   const int WordSize = 8;
151   int32_t FuncId = 1;
152   uint64_t CurFn = 0;
153   for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) {
154     DataExtractor Extractor(
155         StringRef(reinterpret_cast<const char *>(C), ELF64SledEntrySize), true,
156         8);
157     Sleds.push_back({});
158     auto &Entry = Sleds.back();
159     uint64_t OffsetPtr = 0;
160     uint64_t AddrOff = OffsetPtr;
161     Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(&OffsetPtr));
162     uint64_t FuncOff = OffsetPtr;
163     Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(&OffsetPtr));
164     auto Kind = Extractor.getU8(&OffsetPtr);
165     static constexpr SledEntry::FunctionKinds Kinds[] = {
166         SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
167         SledEntry::FunctionKinds::TAIL,
168         SledEntry::FunctionKinds::LOG_ARGS_ENTER,
169         SledEntry::FunctionKinds::CUSTOM_EVENT};
170     if (Kind >= sizeof(Kinds))
171       return errorCodeToError(
172           std::make_error_code(std::errc::executable_format_error));
173     Entry.Kind = Kinds[Kind];
174     Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
175     Entry.Version = Extractor.getU8(&OffsetPtr);
176     if (Entry.Version >= 2) {
177       Entry.Address += C - Contents.bytes_begin() + Address;
178       Entry.Function += C - Contents.bytes_begin() + WordSize + Address;
179     }
180 
181     // We do replicate the function id generation scheme implemented in the
182     // XRay runtime.
183     // FIXME: Figure out how to keep this consistent with the XRay runtime.
184     if (CurFn == 0) {
185       CurFn = Entry.Function;
186       FunctionAddresses[FuncId] = Entry.Function;
187       FunctionIds[Entry.Function] = FuncId;
188     }
189     if (Entry.Function != CurFn) {
190       ++FuncId;
191       CurFn = Entry.Function;
192       FunctionAddresses[FuncId] = Entry.Function;
193       FunctionIds[Entry.Function] = FuncId;
194     }
195   }
196   return Error::success();
197 }
198 
199 static Error
200 loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename,
201          InstrumentationMap::SledContainer &Sleds,
202          InstrumentationMap::FunctionAddressMap &FunctionAddresses,
203          InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
204   std::error_code EC;
205   sys::fs::mapped_file_region MappedFile(
206       Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
207   sys::fs::closeFile(Fd);
208   if (EC)
209     return make_error<StringError>(
210         Twine("Failed memory-mapping file '") + Filename + "'.", EC);
211 
212   std::vector<YAMLXRaySledEntry> YAMLSleds;
213   yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
214   In >> YAMLSleds;
215   if (In.error())
216     return make_error<StringError>(
217         Twine("Failed loading YAML document from '") + Filename + "'.",
218         In.error());
219 
220   Sleds.reserve(YAMLSleds.size());
221   for (const auto &Y : YAMLSleds) {
222     FunctionAddresses[Y.FuncId] = Y.Function;
223     FunctionIds[Y.Function] = Y.FuncId;
224     Sleds.push_back(SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument,
225                               Y.Version});
226   }
227   return Error::success();
228 }
229 
230 // FIXME: Create error types that encapsulate a bit more information than what
231 // StringError instances contain.
232 Expected<InstrumentationMap>
233 llvm::xray::loadInstrumentationMap(StringRef Filename) {
234   // At this point we assume the file is an object file -- and if that doesn't
235   // work, we treat it as YAML.
236   // FIXME: Extend to support non-ELF and non-x86_64 binaries.
237 
238   InstrumentationMap Map;
239   auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
240   if (!ObjectFileOrError) {
241     auto E = ObjectFileOrError.takeError();
242     // We try to load it as YAML if the ELF load didn't work.
243     Expected<sys::fs::file_t> FdOrErr = sys::fs::openNativeFileForRead(Filename);
244     if (!FdOrErr) {
245       // Report the ELF load error if YAML failed.
246       consumeError(FdOrErr.takeError());
247       return std::move(E);
248     }
249 
250     uint64_t FileSize;
251     if (sys::fs::file_size(Filename, FileSize))
252       return std::move(E);
253 
254     // If the file is empty, we return the original error.
255     if (FileSize == 0)
256       return std::move(E);
257 
258     // From this point on the errors will be only for the YAML parts, so we
259     // consume the errors at this point.
260     consumeError(std::move(E));
261     if (auto E = loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,
262                           Map.FunctionAddresses, Map.FunctionIds))
263       return std::move(E);
264   } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
265                                 Map.FunctionAddresses, Map.FunctionIds)) {
266     return std::move(E);
267   }
268   return Map;
269 }
270