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())
64 return make_error<StringError>(
65 "File format not supported (only does ELF and Mach-O little endian 64-bit).",
74 return Name ==
"xray_instr_map";
77 if (
I == Sections.end())
78 return make_error<StringError>(
79 "Failed to find XRay instrumentation map.",
82 if (
I->getContents(Contents))
89 if (
const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
90 return ELFObj->getELFFile()->getRelativeRelocationType();
91 else if (
const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(ObjFile))
92 return ELFObj->getELFFile()->getRelativeRelocationType();
93 else if (
const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(ObjFile))
94 return ELFObj->getELFFile()->getRelativeRelocationType();
95 else if (
const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(ObjFile))
96 return ELFObj->getELFFile()->getRelativeRelocationType();
103 if (Reloc.getType() != RelativeRelocation)
106 Relocs.
insert({Reloc.getOffset(), *AddendOrErr});
112 auto C = Contents.bytes_begin();
113 static constexpr
size_t ELF64SledEntrySize = 32;
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."),
123 uint64_t A =
I->getAddress() +
C - Contents.bytes_begin() +
Offset;
125 if (R != Relocs.
end())
133 for (;
C != Contents.bytes_end();
C += ELF64SledEntrySize) {
135 StringRef(reinterpret_cast<const char *>(
C), ELF64SledEntrySize),
true,
138 auto &Entry = Sleds.back();
141 Entry.Address = RelocateOrElse(AddrOff, Extractor.
getU64(&OffsetPtr));
143 Entry.Function = RelocateOrElse(FuncOff, Extractor.
getU64(&OffsetPtr));
150 if (Kind >=
sizeof(Kinds))
153 Entry.Kind = Kinds[
Kind];
154 Entry.AlwaysInstrument = Extractor.
getU8(&OffsetPtr) != 0;
160 CurFn = Entry.Function;
161 FunctionAddresses[
FuncId] = Entry.Function;
162 FunctionIds[Entry.Function] =
FuncId;
164 if (Entry.Function != CurFn) {
166 CurFn = Entry.Function;
167 FunctionAddresses[
FuncId] = Entry.Function;
168 FunctionIds[Entry.Function] =
FuncId;
181 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
183 return make_error<StringError>(
184 Twine(
"Failed memory-mapping file '") + Filename +
"'.", EC);
186 std::vector<YAMLXRaySledEntry> YAMLSleds;
190 return make_error<StringError>(
191 Twine(
"Failed loading YAML document from '") + Filename +
"'.",
194 Sleds.reserve(YAMLSleds.size());
195 for (
const auto &
Y : YAMLSleds) {
196 FunctionAddresses[
Y.FuncId] =
Y.Function;
197 FunctionIds[
Y.Function] =
Y.FuncId;
199 SledEntry{
Y.Address,
Y.Function,
Y.Kind,
Y.AlwaysInstrument});
214 if (!ObjectFileOrError) {
215 auto E = ObjectFileOrError.takeError();
232 if (
auto E =
loadYAML(Fd, FileSize, Filename, Map.Sleds,
233 Map.FunctionAddresses, Map.FunctionIds))
235 }
else if (
auto E =
loadObj(Filename, *ObjectFileOrError, Map.Sleds,
236 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.
std::error_code openFileForRead(const Twine &Name, int &ResultFD, 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.
FunctionKinds
Each entry here represents the kinds of supported instrumentation map entries.
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
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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 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.
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 Error loadYAML(int Fd, size_t FileSize, StringRef Filename, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
static ErrorSuccess success()
Create a success value.
std::error_code getName(StringRef &Result) const
virtual Triple::ArchType getArch() const =0
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
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...