LLVM 24.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
11#include "llvm/Object/MachO.h"
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 {
49 consumeError(E.takeError());
50 continue;
51 }
52 DataExtractor Decoder(BuildIDData, Obj.makeTriple().isLittleEndian());
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);
57 StringRef Name(Decoder.getFixedLengthString(&Offset, NameSize));
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
71/// Create function information entries for Mach-O symbol stubs.
72///
73/// Symbol stubs are small chunks of code, all with the same fixed size, that
74/// resolves a function pointer on first call and then jumps to the resolved
75/// function on subsequent calls for functions that live in another shared
76/// library. The stubs are stored in sections whose type is S_SYMBOL_STUBS and
77/// the size of a single stub is stored in the "reserved2" field of the section
78/// header. These stubs have no entries in the symbol table of their own, so
79/// they end up being attributed to whatever function precedes them unless they
80/// are synthesized here.
81///
82/// The name of the function a stub jumps to is found using the indirect symbol
83/// table from the LC_DYSYMTAB load command. The "reserved1" field of the
84/// section header contains the index of the indirect symbol table entry that
85/// describes the first stub in the section, and each subsequent stub is
86/// described by the entry that follows. Each indirect symbol table entry is an
87/// index into the symbol table where the matching undefined (N_UNDF) symbol
88/// supplies the name to use for the stub. Each name gets a "symbol stub for: "
89/// prefix prepended to it so that symbolication makes it clear that the address
90/// is the stub for a function and not the function itself.
91///
92/// \returns The number of function infos that were added to \a Gsym.
94 OutputAggregator &Out, GsymCreator &Gsym) {
95 const MachO::dysymtab_command Dysymtab = MachO.getDysymtabLoadCommand();
96 if (Dysymtab.nindirectsyms == 0)
97 return 0;
98 const uint32_t NumSyms = MachO.getSymtabLoadCommand().nsyms;
99 const bool Is64Bit = MachO.is64Bit();
100
101 size_t NumBefore = Gsym.getNumFunctionInfos();
102 for (const object::SectionRef &Sect : MachO.sections()) {
103 const object::DataRefImpl SectDRI = Sect.getRawDataRefImpl();
104 uint32_t SectFlags, IndirectSymIdxStart, StubByteSize;
105 if (Is64Bit) {
106 const MachO::section_64 S = MachO.getSection64(SectDRI);
107 SectFlags = S.flags;
108 IndirectSymIdxStart = S.reserved1;
109 StubByteSize = S.reserved2;
110 } else {
111 const MachO::section S = MachO.getSection(SectDRI);
112 SectFlags = S.flags;
113 IndirectSymIdxStart = S.reserved1;
114 StubByteSize = S.reserved2;
115 }
116 if ((SectFlags & MachO::SECTION_TYPE) != MachO::S_SYMBOL_STUBS)
117 continue;
118 if (StubByteSize == 0)
119 continue;
120
121 const uint64_t SectAddr = Sect.getAddress();
122 const uint64_t NumStubs = Sect.getSize() / StubByteSize;
123 for (uint64_t StubIdx = 0; StubIdx < NumStubs; ++StubIdx) {
124 const uint64_t StubAddr = SectAddr + StubIdx * StubByteSize;
125 if (!Gsym.IsValidTextAddress(StubAddr))
126 continue;
127 const uint64_t IndirectSymIdx = IndirectSymIdxStart + StubIdx;
128 if (IndirectSymIdx >= Dysymtab.nindirectsyms)
129 continue;
130 const uint32_t SymIdx =
131 MachO.getIndirectSymbolTableEntry(Dysymtab, IndirectSymIdx);
132 // Entries that are absolute or local don't refer to a symbol table entry.
134 continue;
135 if (SymIdx >= NumSyms)
136 continue;
137 const object::symbol_iterator SymIt = MachO.getSymbolByIndex(SymIdx);
138 const object::DataRefImpl SymDRI = SymIt->getRawDataRefImpl();
139 const uint8_t NType = Is64Bit ? MachO.getSymbol64TableEntry(SymDRI).n_type
140 : MachO.getSymbolTableEntry(SymDRI).n_type;
141 // Only undefined symbols name a stub, any other symbol type means the
142 // function itself is in this file and already has a symbol table entry.
143 if ((NType & MachO::N_TYPE) != MachO::N_UNDF)
144 continue;
145 Expected<StringRef> Name = SymIt->getName();
146 if (!Name) {
147 if (Out.GetOS())
148 logAllUnhandledErrors(Name.takeError(), *Out.GetOS(),
149 "ObjectFileTransformer: ");
150 else
151 consumeError(Name.takeError());
152 continue;
153 }
154 // Remove the leading '_' character in any symbol names if there is one
155 // for mach-o files.
156 Name->consume_front("_");
157 if (Name->empty())
158 continue;
159 // Append a "symbol stub for: " prefix so it is clear when symbolicating
160 // that the address is the stub for the function and not the function
161 // itself. The string must be copied into the string table since it is
162 // created here and, unlike the symbol names, has no backing storage in
163 // the object file.
164 constexpr bool Copy = true;
165 const std::string StubName = "symbol stub for: " + Name->str();
166 Gsym.addFunctionInfo(FunctionInfo(StubAddr, StubByteSize,
167 Gsym.insertString(StubName, Copy)));
168 }
169 }
170 return Gsym.getNumFunctionInfos() - NumBefore;
171}
172
174 OutputAggregator &Out,
175 GsymCreator &Gsym) {
176 using namespace llvm::object;
177
178 const auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
179 const bool IsMachO = MachO != nullptr;
180 const bool IsELF = isa<ELFObjectFileBase>(&Obj);
181
182 // Read build ID.
183 Gsym.setUUID(getUUID(Obj));
184
185 // Parse the symbol table.
186 size_t NumBefore = Gsym.getNumFunctionInfos();
187 for (const object::SymbolRef &Sym : Obj.symbols()) {
188 Expected<SymbolRef::Type> SymType = Sym.getType();
189 if (!SymType) {
190 consumeError(SymType.takeError());
191 continue;
192 }
193 Expected<uint64_t> AddrOrErr = Sym.getValue();
194 if (!AddrOrErr)
195 // TODO: Test this error.
196 return AddrOrErr.takeError();
197
198 if (SymType.get() != SymbolRef::Type::ST_Function ||
199 !Gsym.IsValidTextAddress(*AddrOrErr))
200 continue;
201 // Function size for MachO files will be 0
202 constexpr bool NoCopy = false;
203 const uint64_t size = IsELF ? ELFSymbolRef(Sym).getSize() : 0;
204 Expected<StringRef> Name = Sym.getName();
205 if (!Name) {
206 if (Out.GetOS())
207 logAllUnhandledErrors(Name.takeError(), *Out.GetOS(),
208 "ObjectFileTransformer: ");
209 else
210 consumeError(Name.takeError());
211 continue;
212 }
213 // Remove the leading '_' character in any symbol names if there is one
214 // for mach-o files.
215 if (IsMachO)
216 Name->consume_front("_");
217 Gsym.addFunctionInfo(
218 FunctionInfo(*AddrOrErr, size, Gsym.insertString(*Name, NoCopy)));
219 }
220 size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore;
221 if (Out.GetOS())
222 *Out.GetOS() << "Loaded " << FunctionsAddedCount
223 << " functions from symbol table.\n";
224
225 // Mach-O symbol stubs have no symbol table entries of their own, so
226 // synthesize function infos for them using the indirect symbol table.
227 if (IsMachO) {
228 const uint64_t StubsAddedCount = addMachOSymbolStubs(*MachO, Out, Gsym);
229 if (Out.GetOS())
230 *Out.GetOS() << "Loaded " << StubsAddedCount
231 << " functions from symbol stubs.\n";
232 }
233 return Error::success();
234}
unsigned uint64_t
unsigned const MCDisassembler * Decoder
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static uint64_t addMachOSymbolStubs(const object::MachOObjectFile &MachO, OutputAggregator &Out, GsymCreator &Gsym)
Create function information entries for Mach-O symbol stubs.
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
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
const T * data() const
Definition ArrayRef.h:138
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
GsymCreator is used to emit GSYM data to a stand alone file or section within a file.
LLVM_ABI void addFunctionInfo(FunctionInfo &&FI)
Add a function info to this GSYM creator.
LLVM_ABI gsym_strp_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.
LLVM_ABI size_t getNumFunctionInfos() const
Get the current number of FunctionInfo objects contained in this object.
LLVM_ABI bool IsValidTextAddress(uint64_t Addr) const
Check if an address is a valid code address.
static LLVM_ABI llvm::Error convert(const object::ObjectFile &Obj, OutputAggregator &Output, GsymCreator &Gsym)
Extract any object file data that is needed by the GsymCreator.
DataRefImpl getRawDataRefImpl() const
This class is the base class for all object file types.
Definition ObjectFile.h:231
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition ObjectFile.h:170
Expected< StringRef > getName() const
Definition ObjectFile.h:465
@ SECTION_TYPE
Definition MachO.h:114
@ S_SYMBOL_STUBS
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
Definition MachO.h:144
@ INDIRECT_SYMBOL_ABS
Definition MachO.h:222
@ INDIRECT_SYMBOL_LOCAL
Definition MachO.h:221
constexpr size_t NameSize
Definition XCOFF.h:30
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition Error.cpp:61
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:1685
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
uint32_t reserved2
Definition MachO.h:634
uint32_t reserved1
Definition MachO.h:633