InstrumentationMap.cpp 11 KB

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