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           uint64_t resolved = Resolver(Reloc, Reloc.getSymbol()->getValue(), A);
118           Relocs.insert({Reloc.getOffset(), resolved});
119         } else if (Reloc.getType() == RelativeRelocation) {
120           if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
121             Relocs.insert({Reloc.getOffset(), *AddendOrErr});
122         }
123       }
124     }
125   }
126 
127   // Copy the instrumentation map data into the Sleds data structure.
128   auto C = Contents.bytes_begin();
129   static constexpr size_t ELF64SledEntrySize = 32;
130 
131   if ((C - Contents.bytes_end()) % ELF64SledEntrySize != 0)
132     return make_error<StringError>(
133         Twine("Instrumentation map entries not evenly divisible by size of "
134               "an XRay sled entry in ELF64."),
135         std::make_error_code(std::errc::executable_format_error));
136 
137   auto RelocateOrElse = [&](uint64_t Offset, uint64_t Address) {
138     if (!Address) {
139       uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
140       RelocMap::const_iterator R = Relocs.find(A);
141       if (R != Relocs.end())
142         return R->second;
143     }
144     return Address;
145   };
146 
147   const int WordSize = 8;
148   int32_t FuncId = 1;
149   uint64_t CurFn = 0;
150   for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) {
151     DataExtractor Extractor(
152         StringRef(reinterpret_cast<const char *>(C), ELF64SledEntrySize), true,
153         8);
154     Sleds.push_back({});
155     auto &Entry = Sleds.back();
156     uint64_t OffsetPtr = 0;
157     uint64_t AddrOff = OffsetPtr;
158     Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(&OffsetPtr));
159     uint64_t FuncOff = OffsetPtr;
160     Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(&OffsetPtr));
161     auto Kind = Extractor.getU8(&OffsetPtr);
162     static constexpr SledEntry::FunctionKinds Kinds[] = {
163         SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
164         SledEntry::FunctionKinds::TAIL,
165         SledEntry::FunctionKinds::LOG_ARGS_ENTER,
166         SledEntry::FunctionKinds::CUSTOM_EVENT};
167     if (Kind >= sizeof(Kinds))
168       return errorCodeToError(
169           std::make_error_code(std::errc::executable_format_error));
170     Entry.Kind = Kinds[Kind];
171     Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
172     Entry.Version = Extractor.getU8(&OffsetPtr);
173     if (Entry.Version >= 2) {
174       Entry.Address += C - Contents.bytes_begin() + Address;
175       Entry.Function += C - Contents.bytes_begin() + WordSize + Address;
176     }
177 
178     // We do replicate the function id generation scheme implemented in the
179     // XRay runtime.
180     // FIXME: Figure out how to keep this consistent with the XRay runtime.
181     if (CurFn == 0) {
182       CurFn = Entry.Function;
183       FunctionAddresses[FuncId] = Entry.Function;
184       FunctionIds[Entry.Function] = FuncId;
185     }
186     if (Entry.Function != CurFn) {
187       ++FuncId;
188       CurFn = Entry.Function;
189       FunctionAddresses[FuncId] = Entry.Function;
190       FunctionIds[Entry.Function] = FuncId;
191     }
192   }
193   return Error::success();
194 }
195 
196 static Error
197 loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename,
198          InstrumentationMap::SledContainer &Sleds,
199          InstrumentationMap::FunctionAddressMap &FunctionAddresses,
200          InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
201   std::error_code EC;
202   sys::fs::mapped_file_region MappedFile(
203       Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
204   sys::fs::closeFile(Fd);
205   if (EC)
206     return make_error<StringError>(
207         Twine("Failed memory-mapping file '") + Filename + "'.", EC);
208 
209   std::vector<YAMLXRaySledEntry> YAMLSleds;
210   yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
211   In >> YAMLSleds;
212   if (In.error())
213     return make_error<StringError>(
214         Twine("Failed loading YAML document from '") + Filename + "'.",
215         In.error());
216 
217   Sleds.reserve(YAMLSleds.size());
218   for (const auto &Y : YAMLSleds) {
219     FunctionAddresses[Y.FuncId] = Y.Function;
220     FunctionIds[Y.Function] = Y.FuncId;
221     Sleds.push_back(SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument,
222                               Y.Version});
223   }
224   return Error::success();
225 }
226 
227 // FIXME: Create error types that encapsulate a bit more information than what
228 // StringError instances contain.
229 Expected<InstrumentationMap>
230 llvm::xray::loadInstrumentationMap(StringRef Filename) {
231   // At this point we assume the file is an object file -- and if that doesn't
232   // work, we treat it as YAML.
233   // FIXME: Extend to support non-ELF and non-x86_64 binaries.
234 
235   InstrumentationMap Map;
236   auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
237   if (!ObjectFileOrError) {
238     auto E = ObjectFileOrError.takeError();
239     // We try to load it as YAML if the ELF load didn't work.
240     Expected<sys::fs::file_t> FdOrErr = sys::fs::openNativeFileForRead(Filename);
241     if (!FdOrErr) {
242       // Report the ELF load error if YAML failed.
243       consumeError(FdOrErr.takeError());
244       return std::move(E);
245     }
246 
247     uint64_t FileSize;
248     if (sys::fs::file_size(Filename, FileSize))
249       return std::move(E);
250 
251     // If the file is empty, we return the original error.
252     if (FileSize == 0)
253       return std::move(E);
254 
255     // From this point on the errors will be only for the YAML parts, so we
256     // consume the errors at this point.
257     consumeError(std::move(E));
258     if (auto E = loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,
259                           Map.FunctionAddresses, Map.FunctionIds))
260       return std::move(E);
261   } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
262                                 Map.FunctionAddresses, Map.FunctionIds)) {
263     return std::move(E);
264   }
265   return Map;
266 }
267