LLVM 20.0.0git
ObjectFileTransformer.cpp
Go to the documentation of this file.
1//===- ObjectFileTransformer.cpp --------------------------------*- C++ -*-===//
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
14
18
19using namespace llvm;
20using namespace gsym;
21
23
24static std::vector<uint8_t> getUUID(const object::ObjectFile &Obj) {
25 // Extract the UUID from the object file
26 std::vector<uint8_t> UUID;
27 if (auto *MachO = dyn_cast<object::MachOObjectFile>(&Obj)) {
28 const ArrayRef<uint8_t> MachUUID = MachO->getUuid();
29 if (!MachUUID.empty())
30 UUID.assign(MachUUID.data(), MachUUID.data() + MachUUID.size());
31 } else if (isa<object::ELFObjectFileBase>(&Obj)) {
32 const StringRef GNUBuildID(".note.gnu.build-id");
33 for (const object::SectionRef &Sect : Obj.sections()) {
34 Expected<StringRef> SectNameOrErr = Sect.getName();
35 if (!SectNameOrErr) {
36 consumeError(SectNameOrErr.takeError());
37 continue;
38 }
39 StringRef SectName(*SectNameOrErr);
40 if (SectName != GNUBuildID)
41 continue;
42 StringRef BuildIDData;
43 Expected<StringRef> E = Sect.getContents();
44 if (E)
45 BuildIDData = *E;
46 else {
48 continue;
49 }
50 DataExtractor Decoder(BuildIDData, Obj.makeTriple().isLittleEndian(), 8);
51 uint64_t Offset = 0;
52 const uint32_t NameSize = Decoder.getU32(&Offset);
53 const uint32_t PayloadSize = Decoder.getU32(&Offset);
54 const uint32_t PayloadType = Decoder.getU32(&Offset);
56 if (Name == "GNU" && PayloadType == NT_GNU_BUILD_ID_TAG) {
57 Offset = alignTo(Offset, 4);
58 StringRef UUIDBytes(Decoder.getBytes(&Offset, PayloadSize));
59 if (!UUIDBytes.empty()) {
60 auto Ptr = reinterpret_cast<const uint8_t *>(UUIDBytes.data());
61 UUID.assign(Ptr, Ptr + UUIDBytes.size());
62 }
63 }
64 }
65 }
66 return UUID;
67}
68
71 GsymCreator &Gsym) {
72 using namespace llvm::object;
73
74 const bool IsMachO = isa<MachOObjectFile>(&Obj);
75 const bool IsELF = isa<ELFObjectFileBase>(&Obj);
76
77 // Read build ID.
78 Gsym.setUUID(getUUID(Obj));
79
80 // Parse the symbol table.
81 size_t NumBefore = Gsym.getNumFunctionInfos();
82 for (const object::SymbolRef &Sym : Obj.symbols()) {
83 Expected<SymbolRef::Type> SymType = Sym.getType();
84 if (!SymType) {
85 consumeError(SymType.takeError());
86 continue;
87 }
88 Expected<uint64_t> AddrOrErr = Sym.getValue();
89 if (!AddrOrErr)
90 // TODO: Test this error.
91 return AddrOrErr.takeError();
92
93 if (SymType.get() != SymbolRef::Type::ST_Function ||
94 !Gsym.IsValidTextAddress(*AddrOrErr))
95 continue;
96 // Function size for MachO files will be 0
97 constexpr bool NoCopy = false;
98 const uint64_t size = IsELF ? ELFSymbolRef(Sym).getSize() : 0;
99 Expected<StringRef> Name = Sym.getName();
100 if (!Name) {
101 if (Out.GetOS())
102 logAllUnhandledErrors(Name.takeError(), *Out.GetOS(),
103 "ObjectFileTransformer: ");
104 else
105 consumeError(Name.takeError());
106 continue;
107 }
108 // Remove the leading '_' character in any symbol names if there is one
109 // for mach-o files.
110 if (IsMachO)
111 Name->consume_front("_");
112 Gsym.addFunctionInfo(
113 FunctionInfo(*AddrOrErr, size, Gsym.insertString(*Name, NoCopy)));
114 }
115 size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore;
116 if (Out.GetOS())
117 *Out.GetOS() << "Loaded " << FunctionsAddedCount
118 << " functions from symbol table.\n";
119 return Error::success();
120}
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
static std::vector< uint8_t > getUUID(const object::ObjectFile &Obj)
constexpr uint32_t NT_GNU_BUILD_ID_TAG
std::pair< llvm::MachO::Target, std::string > UUID
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
const T * data() const
Definition: ArrayRef.h:165
StringRef getFixedLengthString(uint64_t *OffsetPtr, uint64_t Length, StringRef TrimChars={"\0", 1}) const
Extract a fixed length string from *OffsetPtr and consume Length bytes.
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
StringRef getBytes(uint64_t *OffsetPtr, uint64_t Length, Error *Err=nullptr) const
Extract a fixed number of bytes from the specified offset.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
bool isLittleEndian() const
Tests whether the target triple is little endian.
Definition: Triple.cpp:1954
GsymCreator is used to emit GSYM data to a stand alone file or section within a file.
Definition: GsymCreator.h:134
void addFunctionInfo(FunctionInfo &&FI)
Add a function info to this GSYM creator.
uint32_t insertString(StringRef S, bool Copy=true)
Insert a string into the GSYM string table.
void setUUID(llvm::ArrayRef< uint8_t > UUIDBytes)
Set the UUID value.
Definition: GsymCreator.h:397
size_t getNumFunctionInfos() const
Get the current number of FunctionInfo objects contained in this object.
bool IsValidTextAddress(uint64_t Addr) const
Check if an address is a valid code address.
static llvm::Error convert(const object::ObjectFile &Obj, OutputAggregator &Output, GsymCreator &Gsym)
Extract any object file data that is needed by the GsymCreator.
raw_ostream * GetOS() const
uint64_t getSize() const
This class is the base class for all object file types.
Definition: ObjectFile.h:229
Triple makeTriple() const
Create a triple from the data in this object file.
Definition: ObjectFile.cpp:109
section_iterator_range sections() const
Definition: ObjectFile.h:329
symbol_iterator_range symbols() const
Definition: ObjectFile.h:321
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:168
constexpr size_t NameSize
Definition: XCOFF.h:29
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1697
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
Function information in GSYM files encodes information for one contiguous address range.
Definition: FunctionInfo.h:92