30#include <system_error>
37 auto I = FunctionIds.find(
Addr);
38 if (
I != FunctionIds.end())
43std::optional<uint64_t>
45 auto I = FunctionAddresses.find(
FuncId);
46 if (
I != FunctionAddresses.end())
67 return make_error<StringError>(
68 "File format not supported (only does ELF and Mach-O little endian "
70 std::make_error_code(std::errc::not_supported));
73 const auto &Sections = ObjFile.
getBinary()->sections();
79 return *NameOrErr ==
"xray_instr_map";
85 if (
I == Sections.end())
86 return make_error<StringError>(
87 "Failed to find XRay instrumentation map.",
88 std::make_error_code(std::errc::executable_format_error));
90 if (
Error E =
I->getContents().moveInto(Contents))
96 if (
const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
97 return ELFObj->getELFFile().getRelativeRelocationType();
98 else if (
const auto *ELFObj =
99 dyn_cast<object::ELF32BEObjectFile>(ObjFile))
100 return ELFObj->getELFFile().getRelativeRelocationType();
101 else if (
const auto *ELFObj =
102 dyn_cast<object::ELF64LEObjectFile>(ObjFile))
103 return ELFObj->getELFFile().getRelativeRelocationType();
104 else if (
const auto *ELFObj =
105 dyn_cast<object::ELF64BEObjectFile>(ObjFile))
106 return ELFObj->getELFFile().getRelativeRelocationType();
119 if (Supports && Supports(Reloc.getType())) {
127 }
else if (Supports && Supports(Reloc.getType())) {
129 auto A = AddendOrErr ? *AddendOrErr : 0;
137 }
else if (Reloc.getType() == RelativeRelocation) {
139 Relocs.
insert({Reloc.getOffset(), *AddendOrErr});
147 bool Is32Bit = ObjFile.
getBinary()->makeTriple().isArch32Bit();
148 size_t ELFSledEntrySize = Is32Bit ? 16 : 32;
150 if ((
C - Contents.
bytes_end()) % ELFSledEntrySize != 0)
151 return make_error<StringError>(
152 Twine(
"Instrumentation map entries not evenly divisible by size of "
153 "an XRay sled entry."),
154 std::make_error_code(std::errc::executable_format_error));
160 if (R != Relocs.
end())
166 const int WordSize = Is32Bit ? 4 : 8;
169 for (;
C != Contents.
bytes_end();
C += ELFSledEntrySize) {
171 StringRef(
reinterpret_cast<const char *
>(
C), ELFSledEntrySize),
true,
174 auto &Entry = Sleds.back();
178 Entry.Address = RelocateOrElse(AddrOff, Extractor.
getU32(&OffsetPtr));
180 Entry.Address = RelocateOrElse(AddrOff, Extractor.
getU64(&OffsetPtr));
183 Entry.Function = RelocateOrElse(FuncOff, Extractor.
getU32(&OffsetPtr));
185 Entry.Function = RelocateOrElse(FuncOff, Extractor.
getU64(&OffsetPtr));
186 auto Kind = Extractor.
getU8(&OffsetPtr);
192 if (Kind >= std::size(Kinds))
194 std::make_error_code(std::errc::executable_format_error));
195 Entry.Kind = Kinds[Kind];
196 Entry.AlwaysInstrument = Extractor.
getU8(&OffsetPtr) != 0;
197 Entry.Version = Extractor.
getU8(&OffsetPtr);
198 if (Entry.Version >= 2) {
207 CurFn = Entry.Function;
208 FunctionAddresses[
FuncId] = Entry.Function;
209 FunctionIds[Entry.Function] =
FuncId;
211 if (Entry.Function != CurFn) {
213 CurFn = Entry.Function;
214 FunctionAddresses[
FuncId] = Entry.Function;
215 FunctionIds[Entry.Function] =
FuncId;
231 return make_error<StringError>(
232 Twine(
"Failed memory-mapping file '") + Filename +
"'.", EC);
234 std::vector<YAMLXRaySledEntry> YAMLSleds;
238 return make_error<StringError>(
239 Twine(
"Failed loading YAML document from '") + Filename +
"'.",
242 Sleds.reserve(YAMLSleds.size());
243 for (
const auto &
Y : YAMLSleds) {
244 FunctionAddresses[
Y.FuncId] =
Y.Function;
245 FunctionIds[
Y.Function] =
Y.FuncId;
246 Sleds.push_back(
SledEntry{
Y.Address,
Y.Function,
Y.Kind,
Y.AlwaysInstrument,
262 if (!ObjectFileOrError) {
263 auto E = ObjectFileOrError.takeError();
284 if (
auto E =
loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,
285 Map.FunctionAddresses, Map.FunctionIds))
287 }
else if (
auto E =
loadObj(Filename, *ObjectFileOrError, Map.Sleds,
288 Map.FunctionAddresses, Map.FunctionIds)) {
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file defines the DenseMap class.
static void getAddend(uint64_t &, const Elf_Rel_Impl< ELFT, false > &)
static Error loadObj(StringRef Filename, object::OwningBinary< object::ObjectFile > &ObjFile, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
static Error loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename, InstrumentationMap::SledContainer &Sleds, InstrumentationMap::FunctionAddressMap &FunctionAddresses, InstrumentationMap::FunctionAddressReverseMap &FunctionIds)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
StringRef - Represent a constant reference to a string, i.e.
const unsigned char * bytes_end() const
const unsigned char * bytes_begin() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Expected< int64_t > getAddend() const
This class is the base class for all object file types.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
This is a value type class that represents a single relocation in the list of relocations in the obje...
This is a value type class that represents a single section in the list of sections in the object fil...
This class represents a memory mapped file.
@ readonly
May only access map via const_data as read only.
The InstrumentationMap represents the computed function id's and indicated function addresses from an...
std::unordered_map< int32_t, uint64_t > FunctionAddressMap
std::unordered_map< uint64_t, int32_t > FunctionAddressReverseMap
std::vector< SledEntry > SledContainer
std::optional< int32_t > getFunctionId(uint64_t Addr) const
Returns an XRay computed function id, provided a function address.
std::optional< uint64_t > getFunctionAddr(int32_t FuncId) const
Returns the function address for a function id.
@ C
The default llvm calling convention, compatible with C.
uint64_t(*)(uint64_t Type, uint64_t Offset, uint64_t S, uint64_t LocData, int64_t Addend) RelocationResolver
bool(*)(uint64_t) SupportsRelocation
uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
std::pair< SupportsRelocation, RelocationResolver > getRelocationResolver(const ObjectFile &Obj)
std::error_code closeFile(file_t &F)
Close the file object.
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 file_size(const Twine &Path, uint64_t &Result)
Get file size.
Expected< InstrumentationMap > loadInstrumentationMap(StringRef Filename)
Loads the instrumentation map from |Filename|.
This is an optimization pass for GlobalISel generic memory operations.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
void consumeError(Error Err)
Consume a Error without doing anything.
Represents an XRay instrumentation sled entry from an object file.
FunctionKinds
Each entry here represents the kinds of supported instrumentation map entries.