LLVM 19.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
9#include <unordered_set>
10
16
20
21using namespace llvm;
22using namespace gsym;
23
25
26static std::vector<uint8_t> getUUID(const object::ObjectFile &Obj) {
27 // Extract the UUID from the object file
28 std::vector<uint8_t> UUID;
29 if (auto *MachO = dyn_cast<object::MachOObjectFile>(&Obj)) {
30 const ArrayRef<uint8_t> MachUUID = MachO->getUuid();
31 if (!MachUUID.empty())
32 UUID.assign(MachUUID.data(), MachUUID.data() + MachUUID.size());
33 } else if (isa<object::ELFObjectFileBase>(&Obj)) {
34 const StringRef GNUBuildID(".note.gnu.build-id");
35 for (const object::SectionRef &Sect : Obj.sections()) {
36 Expected<StringRef> SectNameOrErr = Sect.getName();
37 if (!SectNameOrErr) {
38 consumeError(SectNameOrErr.takeError());
39 continue;
40 }
41 StringRef SectName(*SectNameOrErr);
42 if (SectName != GNUBuildID)
43 continue;
44 StringRef BuildIDData;
45 Expected<StringRef> E = Sect.getContents();
46 if (E)
47 BuildIDData = *E;
48 else {
50 continue;
51 }
52 DataExtractor Decoder(BuildIDData, Obj.makeTriple().isLittleEndian(), 8);
53 uint64_t Offset = 0;
54 const uint32_t NameSize = Decoder.getU32(&Offset);
55 const uint32_t PayloadSize = Decoder.getU32(&Offset);
56 const uint32_t PayloadType = Decoder.getU32(&Offset);
58 if (Name == "GNU" && PayloadType == NT_GNU_BUILD_ID_TAG) {
59 Offset = alignTo(Offset, 4);
60 StringRef UUIDBytes(Decoder.getBytes(&Offset, PayloadSize));
61 if (!UUIDBytes.empty()) {
62 auto Ptr = reinterpret_cast<const uint8_t *>(UUIDBytes.data());
63 UUID.assign(Ptr, Ptr + UUIDBytes.size());
64 }
65 }
66 }
67 }
68 return UUID;
69}
70
73 GsymCreator &Gsym) {
74 using namespace llvm::object;
75
76 const bool IsMachO = isa<MachOObjectFile>(&Obj);
77 const bool IsELF = isa<ELFObjectFileBase>(&Obj);
78
79 // Read build ID.
80 Gsym.setUUID(getUUID(Obj));
81
82 // Parse the symbol table.
83 size_t NumBefore = Gsym.getNumFunctionInfos();
84 for (const object::SymbolRef &Sym : Obj.symbols()) {
85 Expected<SymbolRef::Type> SymType = Sym.getType();
86 if (!SymType) {
87 consumeError(SymType.takeError());
88 continue;
89 }
90 Expected<uint64_t> AddrOrErr = Sym.getValue();
91 if (!AddrOrErr)
92 // TODO: Test this error.
93 return AddrOrErr.takeError();
94
95 if (SymType.get() != SymbolRef::Type::ST_Function ||
96 !Gsym.IsValidTextAddress(*AddrOrErr))
97 continue;
98 // Function size for MachO files will be 0
99 constexpr bool NoCopy = false;
100 const uint64_t size = IsELF ? ELFSymbolRef(Sym).getSize() : 0;
101 Expected<StringRef> Name = Sym.getName();
102 if (!Name) {
103 if (Out.GetOS())
104 logAllUnhandledErrors(Name.takeError(), *Out.GetOS(),
105 "ObjectFileTransformer: ");
106 else
107 consumeError(Name.takeError());
108 continue;
109 }
110 // Remove the leading '_' character in any symbol names if there is one
111 // for mach-o files.
112 if (IsMachO)
113 Name->consume_front("_");
114 Gsym.addFunctionInfo(
115 FunctionInfo(*AddrOrErr, size, Gsym.insertString(*Name, NoCopy)));
116 }
117 size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore;
118 if (Out.GetOS())
119 *Out.GetOS() << "Loaded " << FunctionsAddedCount
120 << " functions from symbol table.\n";
121 return Error::success();
122}
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:165
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
const T * data() const
Definition: ArrayRef.h:162
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:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
bool isLittleEndian() const
Tests whether the target triple is little endian.
Definition: Triple.cpp:1817
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:369
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:328
symbol_iterator_range symbols() const
Definition: ObjectFile.h:320
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:456
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:1680
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:1041
Function information in GSYM files encodes information for one contiguous address range.
Definition: FunctionInfo.h:88