31 #include <system_error> 38 auto I = FunctionIds.find(Addr);
39 if (
I != FunctionIds.end())
45 auto I = FunctionAddresses.find(FuncId);
46 if (
I != FunctionAddresses.end())
65 return make_error<StringError>(
66 "File format not supported (only does ELF and Mach-O little endian 64-bit).",
74 return *NameOrErr ==
"xray_instr_map";
79 if (
I == Sections.end())
80 return make_error<StringError>(
81 "Failed to find XRay instrumentation map.",
92 if (
const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
93 return ELFObj->getELFFile()->getRelativeRelocationType();
94 else if (
const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(ObjFile))
95 return ELFObj->getELFFile()->getRelativeRelocationType();
96 else if (
const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(ObjFile))
97 return ELFObj->getELFFile()->getRelativeRelocationType();
98 else if (
const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(ObjFile))
99 return ELFObj->getELFFile()->getRelativeRelocationType();
104 bool (*SupportsRelocation)(uint64_t);
106 std::tie(SupportsRelocation, Resolver) =
111 if (SupportsRelocation && SupportsRelocation(Reloc.getType())) {
113 auto A = AddendOrErr ? *AddendOrErr : 0;
114 uint64_t resolved = Resolver(Reloc, Reloc.getSymbol()->getValue(), A);
115 Relocs.
insert({Reloc.getOffset(), resolved});
116 }
else if (Reloc.getType() == RelativeRelocation) {
118 Relocs.
insert({Reloc.getOffset(), *AddendOrErr});
125 auto C = Contents.bytes_begin();
126 static constexpr
size_t ELF64SledEntrySize = 32;
128 if ((
C - Contents.bytes_end()) % ELF64SledEntrySize != 0)
129 return make_error<StringError>(
130 Twine(
"Instrumentation map entries not evenly divisible by size of " 131 "an XRay sled entry in ELF64."),
134 auto RelocateOrElse = [&](uint64_t
Offset, uint64_t
Address) {
136 uint64_t A =
I->getAddress() +
C - Contents.bytes_begin() +
Offset;
138 if (R != Relocs.
end())
146 for (;
C != Contents.bytes_end();
C += ELF64SledEntrySize) {
148 StringRef(reinterpret_cast<const char *>(
C), ELF64SledEntrySize),
true,
151 auto &
Entry = Sleds.back();
152 uint64_t OffsetPtr = 0;
153 uint64_t AddrOff = OffsetPtr;
154 Entry.Address = RelocateOrElse(AddrOff, Extractor.
getU64(&OffsetPtr));
155 uint64_t FuncOff = OffsetPtr;
156 Entry.Function = RelocateOrElse(FuncOff, Extractor.
getU64(&OffsetPtr));
163 if (Kind >=
sizeof(Kinds))
167 Entry.AlwaysInstrument = Extractor.
getU8(&OffsetPtr) != 0;
173 CurFn =
Entry.Function;
177 if (
Entry.Function != CurFn) {
179 CurFn =
Entry.Function;
194 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
197 return make_error<StringError>(
198 Twine(
"Failed memory-mapping file '") + Filename +
"'.", EC);
200 std::vector<YAMLXRaySledEntry> YAMLSleds;
204 return make_error<StringError>(
205 Twine(
"Failed loading YAML document from '") + Filename +
"'.",
208 Sleds.reserve(YAMLSleds.size());
209 for (
const auto &
Y : YAMLSleds) {
210 FunctionAddresses[
Y.FuncId] =
Y.Function;
211 FunctionIds[
Y.Function] =
Y.FuncId;
213 SledEntry{
Y.Address,
Y.Function,
Y.Kind,
Y.AlwaysInstrument});
228 if (!ObjectFileOrError) {
229 auto E = ObjectFileOrError.takeError();
249 if (
auto E =
loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,
250 Map.FunctionAddresses, Map.FunctionIds))
252 }
else if (
auto E =
loadObj(Filename, *ObjectFileOrError, Map.Sleds,
253 Map.FunctionAddresses, Map.FunctionIds)) {
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Optional< uint64_t > getFunctionAddr(int32_t FuncId) const
Returns the function address for a function id.
This class represents lattice values for constants.
FunctionKinds
Each entry here represents the kinds of supported instrumentation map entries.
std::pair< bool(*)(uint64_t), RelocationResolver > getRelocationResolver(const ObjectFile &Obj)
This class is the base class for all object file types.
This class represents a memory mapped file.
std::unordered_map< uint64_t, int32_t > FunctionAddressReverseMap
Error takeError()
Take ownership of the stored error.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::vector< SledEntry > SledContainer
This is a value type class that represents a single relocation in the list of relocations in the obje...
std::error_code make_error_code(BitcodeError E)
std::error_code file_size(const Twine &Path, uint64_t &Result)
Get file size.
Tagged union holding either a T or a Error.
Expected< InstrumentationMap > loadInstrumentationMap(StringRef Filename)
Loads the instrumentation map from |Filename|.
static Error loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
static Error loadObj(StringRef Filename, object::OwningBinary< object::ObjectFile > &ObjFile, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
Represents an XRay instrumentation sled entry from an object file.
section_iterator_range sections() const
iterator find(const_arg_type_t< KeyT > Val)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Expected< int64_t > getAddend() const
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
void consumeError(Error Err)
Consume a Error without doing anything.
static ErrorSuccess success()
Create a success value.
virtual Triple::ArchType getArch() const =0
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
std::error_code closeFile(file_t &F)
Close the file object.
Expected< StringRef > getName() const
Optional< int32_t > getFunctionId(uint64_t Addr) const
Returns an XRay computed function id, provided a function address.
iterator_range< relocation_iterator > relocations() const
std::unordered_map< int32_t, uint64_t > FunctionAddressMap
uint64_t(*)(RelocationRef R, uint64_t S, uint64_t A) RelocationResolver
The InstrumentationMap represents the computed function id's and indicated function addresses from an...
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
This is a value type class that represents a single section in the list of sections in the object fil...