xref: /llvm-project/llvm/lib/XRay/InstrumentationMap.cpp (revision 493a08248353ea95a310a8a515e69068305e6f6c)
1 //===- InstrumentationMap.cpp - XRay Instrumentation Map ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implementation of the InstrumentationMap type for XRay sleds.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/XRay/InstrumentationMap.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/None.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Object/Binary.h"
22 #include "llvm/Object/ELFObjectFile.h"
23 #include "llvm/Object/ObjectFile.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     return make_error<StringError>(
65         "File format not supported (only does ELF and Mach-O little endian 64-bit).",
66         std::make_error_code(std::errc::not_supported));
67 
68   StringRef Contents = "";
69   const auto &Sections = ObjFile.getBinary()->sections();
70   auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
71     StringRef Name = "";
72     if (Section.getName(Name))
73       return false;
74     return Name == "xray_instr_map";
75   });
76 
77   if (I == Sections.end())
78     return make_error<StringError>(
79         "Failed to find XRay instrumentation map.",
80         std::make_error_code(std::errc::executable_format_error));
81 
82   if (I->getContents(Contents))
83     return errorCodeToError(
84         std::make_error_code(std::errc::executable_format_error));
85 
86   RelocMap Relocs;
87   if (ObjFile.getBinary()->isELF()) {
88     uint32_t RelrRelocationType = [](object::ObjectFile *ObjFile) {
89       if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
90         return ELFObj->getELFFile()->getRelrRelocationType();
91       else if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(ObjFile))
92         return ELFObj->getELFFile()->getRelrRelocationType();
93       else if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(ObjFile))
94         return ELFObj->getELFFile()->getRelrRelocationType();
95       else if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(ObjFile))
96         return ELFObj->getELFFile()->getRelrRelocationType();
97       else
98         return static_cast<uint32_t>(0);
99     }(ObjFile.getBinary());
100 
101     for (const object::SectionRef &Section : Sections) {
102       for (const object::RelocationRef &Reloc : Section.relocations()) {
103         if (Reloc.getType() != RelrRelocationType)
104           continue;
105         if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
106           Relocs.insert({Reloc.getOffset(), *AddendOrErr});
107       }
108     }
109   }
110 
111   // Copy the instrumentation map data into the Sleds data structure.
112   auto C = Contents.bytes_begin();
113   static constexpr size_t ELF64SledEntrySize = 32;
114 
115   if ((C - Contents.bytes_end()) % ELF64SledEntrySize != 0)
116     return make_error<StringError>(
117         Twine("Instrumentation map entries not evenly divisible by size of "
118               "an XRay sled entry in ELF64."),
119         std::make_error_code(std::errc::executable_format_error));
120 
121   auto RelocateOrElse = [&](uint32_t Offset, uint64_t Address) {
122     if (!Address) {
123       uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
124       RelocMap::const_iterator R = Relocs.find(A);
125       if (R != Relocs.end())
126         return R->second;
127     }
128     return Address;
129   };
130 
131   int32_t FuncId = 1;
132   uint64_t CurFn = 0;
133   for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) {
134     DataExtractor Extractor(
135         StringRef(reinterpret_cast<const char *>(C), ELF64SledEntrySize), true,
136         8);
137     Sleds.push_back({});
138     auto &Entry = Sleds.back();
139     uint32_t OffsetPtr = 0;
140     Entry.Address = RelocateOrElse(OffsetPtr, Extractor.getU64(&OffsetPtr));
141     Entry.Function = RelocateOrElse(OffsetPtr, Extractor.getU64(&OffsetPtr));
142     auto Kind = Extractor.getU8(&OffsetPtr);
143     static constexpr SledEntry::FunctionKinds Kinds[] = {
144         SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
145         SledEntry::FunctionKinds::TAIL,
146         SledEntry::FunctionKinds::LOG_ARGS_ENTER,
147         SledEntry::FunctionKinds::CUSTOM_EVENT};
148     if (Kind >= sizeof(Kinds))
149       return errorCodeToError(
150           std::make_error_code(std::errc::executable_format_error));
151     Entry.Kind = Kinds[Kind];
152     Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
153 
154     // We do replicate the function id generation scheme implemented in the
155     // XRay runtime.
156     // FIXME: Figure out how to keep this consistent with the XRay runtime.
157     if (CurFn == 0) {
158       CurFn = Entry.Function;
159       FunctionAddresses[FuncId] = Entry.Function;
160       FunctionIds[Entry.Function] = FuncId;
161     }
162     if (Entry.Function != CurFn) {
163       ++FuncId;
164       CurFn = Entry.Function;
165       FunctionAddresses[FuncId] = Entry.Function;
166       FunctionIds[Entry.Function] = FuncId;
167     }
168   }
169   return Error::success();
170 }
171 
172 static Error
173 loadYAML(int Fd, size_t FileSize, StringRef Filename,
174          InstrumentationMap::SledContainer &Sleds,
175          InstrumentationMap::FunctionAddressMap &FunctionAddresses,
176          InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
177   std::error_code EC;
178   sys::fs::mapped_file_region MappedFile(
179       Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
180   if (EC)
181     return make_error<StringError>(
182         Twine("Failed memory-mapping file '") + Filename + "'.", EC);
183 
184   std::vector<YAMLXRaySledEntry> YAMLSleds;
185   yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
186   In >> YAMLSleds;
187   if (In.error())
188     return make_error<StringError>(
189         Twine("Failed loading YAML document from '") + Filename + "'.",
190         In.error());
191 
192   Sleds.reserve(YAMLSleds.size());
193   for (const auto &Y : YAMLSleds) {
194     FunctionAddresses[Y.FuncId] = Y.Function;
195     FunctionIds[Y.Function] = Y.FuncId;
196     Sleds.push_back(
197         SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument});
198   }
199   return Error::success();
200 }
201 
202 // FIXME: Create error types that encapsulate a bit more information than what
203 // StringError instances contain.
204 Expected<InstrumentationMap>
205 llvm::xray::loadInstrumentationMap(StringRef Filename) {
206   // At this point we assume the file is an object file -- and if that doesn't
207   // work, we treat it as YAML.
208   // FIXME: Extend to support non-ELF and non-x86_64 binaries.
209 
210   InstrumentationMap Map;
211   auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
212   if (!ObjectFileOrError) {
213     auto E = ObjectFileOrError.takeError();
214     // We try to load it as YAML if the ELF load didn't work.
215     int Fd;
216     if (sys::fs::openFileForRead(Filename, Fd))
217       return std::move(E);
218 
219     uint64_t FileSize;
220     if (sys::fs::file_size(Filename, FileSize))
221       return std::move(E);
222 
223     // If the file is empty, we return the original error.
224     if (FileSize == 0)
225       return std::move(E);
226 
227     // From this point on the errors will be only for the YAML parts, so we
228     // consume the errors at this point.
229     consumeError(std::move(E));
230     if (auto E = loadYAML(Fd, FileSize, Filename, Map.Sleds,
231                           Map.FunctionAddresses, Map.FunctionIds))
232       return std::move(E);
233   } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
234                                 Map.FunctionAddresses, Map.FunctionIds)) {
235     return std::move(E);
236   }
237   return Map;
238 }
239