Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/XRay/InstrumentationMap.h" |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 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" |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 22 | #include "llvm/Object/ELFObjectFile.h" |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 23 | #include "llvm/Object/ObjectFile.h" |
| 24 | #include "llvm/Support/DataExtractor.h" |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Error.h" |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FileSystem.h" |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 27 | #include "llvm/Support/YAMLTraits.h" |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <cstddef> |
| 30 | #include <cstdint> |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 31 | #include <system_error> |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 32 | #include <vector> |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 33 | |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | using namespace xray; |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 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 | |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 51 | using RelocMap = DenseMap<uint64_t, uint64_t>; |
| 52 | |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 53 | static Error |
David Carlier | f32491f | 2018-09-10 05:00:43 +0000 | [diff] [blame] | 54 | loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile, |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 55 | InstrumentationMap::SledContainer &Sleds, |
| 56 | InstrumentationMap::FunctionAddressMap &FunctionAddresses, |
| 57 | InstrumentationMap::FunctionAddressReverseMap &FunctionIds) { |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 58 | InstrumentationMap Map; |
| 59 | |
| 60 | // Find the section named "xray_instr_map". |
David Carlier | f32491f | 2018-09-10 05:00:43 +0000 | [diff] [blame] | 61 | if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) || |
Tim Shen | 2c44e21 | 2017-02-10 21:03:24 +0000 | [diff] [blame] | 62 | !(ObjFile.getBinary()->getArch() == Triple::x86_64 || |
| 63 | ObjFile.getBinary()->getArch() == Triple::ppc64le)) |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 64 | return make_error<StringError>( |
David Carlier | f32491f | 2018-09-10 05:00:43 +0000 | [diff] [blame] | 65 | "File format not supported (only does ELF and Mach-O little endian 64-bit).", |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 66 | std::make_error_code(std::errc::not_supported)); |
| 67 | |
| 68 | StringRef Contents = ""; |
| 69 | const auto &Sections = ObjFile.getBinary()->sections(); |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 70 | auto I = llvm::find_if(Sections, [&](object::SectionRef Section) { |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 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 | |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 86 | RelocMap Relocs; |
| 87 | if (ObjFile.getBinary()->isELF()) { |
Fangrui Song | 4daae1f | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 88 | uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) { |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 89 | if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile)) |
Fangrui Song | 4daae1f | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 90 | return ELFObj->getELFFile()->getRelativeRelocationType(); |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 91 | else if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(ObjFile)) |
Fangrui Song | 4daae1f | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 92 | return ELFObj->getELFFile()->getRelativeRelocationType(); |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 93 | else if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(ObjFile)) |
Fangrui Song | 4daae1f | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 94 | return ELFObj->getELFFile()->getRelativeRelocationType(); |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 95 | else if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(ObjFile)) |
Fangrui Song | 4daae1f | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 96 | return ELFObj->getELFFile()->getRelativeRelocationType(); |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 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()) { |
Fangrui Song | 4daae1f | 2018-12-14 07:46:58 +0000 | [diff] [blame] | 103 | if (Reloc.getType() != RelativeRelocation) |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 104 | continue; |
| 105 | if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend()) |
| 106 | Relocs.insert({Reloc.getOffset(), *AddendOrErr}); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 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 | |
Petr Hosek | 0f4c901 | 2018-12-14 01:37:56 +0000 | [diff] [blame] | 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 | |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 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; |
Petr Hosek | c8c5302 | 2018-12-14 06:06:19 +0000 | [diff] [blame] | 140 | uint32_t AddrOff = OffsetPtr; |
Petr Hosek | 43a722e | 2018-12-14 05:56:20 +0000 | [diff] [blame] | 141 | Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(&OffsetPtr)); |
Petr Hosek | c8c5302 | 2018-12-14 06:06:19 +0000 | [diff] [blame] | 142 | uint32_t FuncOff = OffsetPtr; |
Petr Hosek | 43a722e | 2018-12-14 05:56:20 +0000 | [diff] [blame] | 143 | Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(&OffsetPtr)); |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 144 | auto Kind = Extractor.getU8(&OffsetPtr); |
| 145 | static constexpr SledEntry::FunctionKinds Kinds[] = { |
| 146 | SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT, |
| 147 | SledEntry::FunctionKinds::TAIL, |
Dean Michael Berris | 5a082a4 | 2017-08-21 00:14:06 +0000 | [diff] [blame] | 148 | SledEntry::FunctionKinds::LOG_ARGS_ENTER, |
| 149 | SledEntry::FunctionKinds::CUSTOM_EVENT}; |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 150 | if (Kind >= sizeof(Kinds)) |
| 151 | return errorCodeToError( |
| 152 | std::make_error_code(std::errc::executable_format_error)); |
| 153 | Entry.Kind = Kinds[Kind]; |
| 154 | Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0; |
| 155 | |
| 156 | // We do replicate the function id generation scheme implemented in the |
| 157 | // XRay runtime. |
| 158 | // FIXME: Figure out how to keep this consistent with the XRay runtime. |
| 159 | if (CurFn == 0) { |
| 160 | CurFn = Entry.Function; |
| 161 | FunctionAddresses[FuncId] = Entry.Function; |
| 162 | FunctionIds[Entry.Function] = FuncId; |
| 163 | } |
| 164 | if (Entry.Function != CurFn) { |
| 165 | ++FuncId; |
| 166 | CurFn = Entry.Function; |
| 167 | FunctionAddresses[FuncId] = Entry.Function; |
| 168 | FunctionIds[Entry.Function] = FuncId; |
| 169 | } |
| 170 | } |
| 171 | return Error::success(); |
| 172 | } |
| 173 | |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 174 | static Error |
| 175 | loadYAML(int Fd, size_t FileSize, StringRef Filename, |
| 176 | InstrumentationMap::SledContainer &Sleds, |
| 177 | InstrumentationMap::FunctionAddressMap &FunctionAddresses, |
| 178 | InstrumentationMap::FunctionAddressReverseMap &FunctionIds) { |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 179 | std::error_code EC; |
| 180 | sys::fs::mapped_file_region MappedFile( |
| 181 | Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC); |
| 182 | if (EC) |
| 183 | return make_error<StringError>( |
| 184 | Twine("Failed memory-mapping file '") + Filename + "'.", EC); |
| 185 | |
| 186 | std::vector<YAMLXRaySledEntry> YAMLSleds; |
| 187 | yaml::Input In(StringRef(MappedFile.data(), MappedFile.size())); |
| 188 | In >> YAMLSleds; |
| 189 | if (In.error()) |
| 190 | return make_error<StringError>( |
| 191 | Twine("Failed loading YAML document from '") + Filename + "'.", |
| 192 | In.error()); |
| 193 | |
| 194 | Sleds.reserve(YAMLSleds.size()); |
| 195 | for (const auto &Y : YAMLSleds) { |
| 196 | FunctionAddresses[Y.FuncId] = Y.Function; |
| 197 | FunctionIds[Y.Function] = Y.FuncId; |
| 198 | Sleds.push_back( |
| 199 | SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument}); |
| 200 | } |
| 201 | return Error::success(); |
| 202 | } |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 203 | |
| 204 | // FIXME: Create error types that encapsulate a bit more information than what |
| 205 | // StringError instances contain. |
Eugene Zelenko | 3a124c0 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 206 | Expected<InstrumentationMap> |
| 207 | llvm::xray::loadInstrumentationMap(StringRef Filename) { |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 208 | // At this point we assume the file is an object file -- and if that doesn't |
| 209 | // work, we treat it as YAML. |
| 210 | // FIXME: Extend to support non-ELF and non-x86_64 binaries. |
| 211 | |
| 212 | InstrumentationMap Map; |
| 213 | auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename); |
| 214 | if (!ObjectFileOrError) { |
| 215 | auto E = ObjectFileOrError.takeError(); |
| 216 | // We try to load it as YAML if the ELF load didn't work. |
| 217 | int Fd; |
| 218 | if (sys::fs::openFileForRead(Filename, Fd)) |
| 219 | return std::move(E); |
| 220 | |
| 221 | uint64_t FileSize; |
| 222 | if (sys::fs::file_size(Filename, FileSize)) |
| 223 | return std::move(E); |
| 224 | |
| 225 | // If the file is empty, we return the original error. |
| 226 | if (FileSize == 0) |
| 227 | return std::move(E); |
| 228 | |
| 229 | // From this point on the errors will be only for the YAML parts, so we |
| 230 | // consume the errors at this point. |
| 231 | consumeError(std::move(E)); |
| 232 | if (auto E = loadYAML(Fd, FileSize, Filename, Map.Sleds, |
| 233 | Map.FunctionAddresses, Map.FunctionIds)) |
| 234 | return std::move(E); |
David Carlier | f32491f | 2018-09-10 05:00:43 +0000 | [diff] [blame] | 235 | } else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds, |
Dean Michael Berris | 93789c8 | 2017-02-01 00:05:29 +0000 | [diff] [blame] | 236 | Map.FunctionAddresses, Map.FunctionIds)) { |
| 237 | return std::move(E); |
| 238 | } |
| 239 | return Map; |
| 240 | } |