Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Fri Jul 18 2025 11:09:34 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp