File indexing completed on 2026-05-10 08:42:47
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLDB_EXPRESSION_IREXECUTIONUNIT_H
0010 #define LLDB_EXPRESSION_IREXECUTIONUNIT_H
0011
0012 #include <atomic>
0013 #include <memory>
0014 #include <string>
0015 #include <vector>
0016
0017 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
0018 #include "llvm/IR/Module.h"
0019
0020 #include "lldb/Expression/IRMemoryMap.h"
0021 #include "lldb/Expression/ObjectFileJIT.h"
0022 #include "lldb/Symbol/SymbolContext.h"
0023 #include "lldb/Utility/DataBufferHeap.h"
0024 #include "lldb/lldb-forward.h"
0025 #include "lldb/lldb-private.h"
0026
0027 namespace llvm {
0028
0029 class Module;
0030 class ExecutionEngine;
0031 class ObjectCache;
0032
0033 }
0034
0035 namespace lldb_private {
0036
0037 class Status;
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 class IRExecutionUnit : public std::enable_shared_from_this<IRExecutionUnit>,
0057 public IRMemoryMap,
0058 public ObjectFileJITDelegate {
0059 public:
0060
0061 IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_up,
0062 std::unique_ptr<llvm::Module> &module_up, ConstString &name,
0063 const lldb::TargetSP &target_sp, const SymbolContext &sym_ctx,
0064 std::vector<std::string> &cpu_features);
0065
0066
0067 ~IRExecutionUnit() override;
0068
0069 ConstString GetFunctionName() { return m_name; }
0070
0071 llvm::Module *GetModule() { return m_module; }
0072
0073 llvm::Function *GetFunction() {
0074 return ((m_module != nullptr) ? m_module->getFunction(m_name.GetStringRef())
0075 : nullptr);
0076 }
0077
0078 void GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
0079 lldb::addr_t &func_end);
0080
0081
0082
0083
0084
0085 lldb::addr_t WriteNow(const uint8_t *bytes, size_t size, Status &error);
0086
0087 void FreeNow(lldb::addr_t allocation);
0088
0089
0090 lldb::ByteOrder GetByteOrder() const override;
0091
0092 uint32_t GetAddressByteSize() const override;
0093
0094 void PopulateSymtab(lldb_private::ObjectFile *obj_file,
0095 lldb_private::Symtab &symtab) override;
0096
0097 void PopulateSectionList(lldb_private::ObjectFile *obj_file,
0098 lldb_private::SectionList §ion_list) override;
0099
0100 ArchSpec GetArchitecture() override;
0101
0102 lldb::ModuleSP GetJITModule();
0103
0104 lldb::addr_t FindSymbol(ConstString name, bool &missing_weak);
0105
0106 void GetStaticInitializers(std::vector<lldb::addr_t> &static_initializers);
0107
0108
0109
0110
0111
0112
0113
0114
0115 struct JittedEntity {
0116 ConstString m_name;
0117 lldb::addr_t m_local_addr;
0118 lldb::addr_t
0119 m_remote_addr;
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135 JittedEntity(const char *name,
0136 lldb::addr_t local_addr = LLDB_INVALID_ADDRESS,
0137 lldb::addr_t remote_addr = LLDB_INVALID_ADDRESS)
0138 : m_name(name), m_local_addr(local_addr), m_remote_addr(remote_addr) {}
0139 };
0140
0141 struct JittedFunction : JittedEntity {
0142 bool m_external;
0143 JittedFunction(const char *name, bool external,
0144 lldb::addr_t local_addr = LLDB_INVALID_ADDRESS,
0145 lldb::addr_t remote_addr = LLDB_INVALID_ADDRESS)
0146 : JittedEntity(name, local_addr, remote_addr), m_external(external) {}
0147 };
0148
0149 struct JittedGlobalVariable : JittedEntity {
0150 JittedGlobalVariable(const char *name,
0151 lldb::addr_t local_addr = LLDB_INVALID_ADDRESS,
0152 lldb::addr_t remote_addr = LLDB_INVALID_ADDRESS)
0153 : JittedEntity(name, local_addr, remote_addr) {}
0154 };
0155
0156 const std::vector<JittedFunction> &GetJittedFunctions() {
0157 return m_jitted_functions;
0158 }
0159
0160 const std::vector<JittedGlobalVariable> &GetJittedGlobalVariables() {
0161 return m_jitted_global_variables;
0162 }
0163
0164 private:
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174 lldb::addr_t GetRemoteAddressForLocal(lldb::addr_t local_address);
0175
0176 typedef std::pair<lldb::addr_t, uintptr_t> AddrRange;
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187 AddrRange GetRemoteRangeForLocal(lldb::addr_t local_address);
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198 bool CommitAllocations(lldb::ProcessSP &process_sp);
0199
0200
0201
0202
0203
0204 void ReportAllocations(llvm::ExecutionEngine &engine);
0205
0206
0207
0208
0209
0210
0211
0212
0213 bool WriteData(lldb::ProcessSP &process_sp);
0214
0215 Status DisassembleFunction(Stream &stream, lldb::ProcessSP &process_sp);
0216
0217 void CollectCandidateCNames(std::vector<ConstString> &C_names,
0218 ConstString name);
0219
0220 void CollectCandidateCPlusPlusNames(std::vector<ConstString> &CPP_names,
0221 const std::vector<ConstString> &C_names,
0222 const SymbolContext &sc);
0223
0224 lldb::addr_t FindInSymbols(const std::vector<ConstString> &names,
0225 const lldb_private::SymbolContext &sc,
0226 bool &symbol_was_missing_weak);
0227
0228 lldb::addr_t FindInRuntimes(const std::vector<ConstString> &names,
0229 const lldb_private::SymbolContext &sc);
0230
0231 lldb::addr_t FindInUserDefinedSymbols(const std::vector<ConstString> &names,
0232 const lldb_private::SymbolContext &sc);
0233
0234 void ReportSymbolLookupError(ConstString name);
0235
0236 class MemoryManager : public llvm::SectionMemoryManager {
0237 public:
0238 MemoryManager(IRExecutionUnit &parent);
0239
0240 ~MemoryManager() override;
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
0257 unsigned SectionID,
0258 llvm::StringRef SectionName) override;
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
0277 unsigned SectionID,
0278 llvm::StringRef SectionName,
0279 bool IsReadOnly) override;
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289 bool finalizeMemory(std::string *ErrMsg) override {
0290
0291
0292
0293
0294 return false;
0295 }
0296
0297
0298 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
0299 size_t Size) override {}
0300 void deregisterEHFrames() override {}
0301
0302 uint64_t getSymbolAddress(const std::string &Name) override;
0303
0304
0305
0306 uint64_t GetSymbolAddressAndPresence(const std::string &Name,
0307 bool &missing_weak);
0308
0309 llvm::JITSymbol findSymbol(const std::string &Name) override;
0310
0311 void *getPointerToNamedFunction(const std::string &Name,
0312 bool AbortOnFailure = true) override;
0313
0314 private:
0315 std::unique_ptr<SectionMemoryManager> m_default_mm_up;
0316
0317
0318
0319
0320
0321
0322 IRExecutionUnit &m_parent;
0323 };
0324
0325 static const unsigned eSectionIDInvalid = (unsigned)-1;
0326
0327 enum class AllocationKind { Stub, Code, Data, Global, Bytes };
0328
0329 static lldb::SectionType
0330 GetSectionTypeFromSectionName(const llvm::StringRef &name,
0331 AllocationKind alloc_kind);
0332
0333
0334
0335
0336
0337 struct AllocationRecord {
0338 std::string m_name;
0339 lldb::addr_t m_process_address;
0340 uintptr_t m_host_address;
0341 uint32_t m_permissions;
0342 lldb::SectionType m_sect_type;
0343 size_t m_size;
0344 unsigned m_alignment;
0345 unsigned m_section_id;
0346
0347 AllocationRecord(uintptr_t host_address, uint32_t permissions,
0348 lldb::SectionType sect_type, size_t size,
0349 unsigned alignment, unsigned section_id, const char *name)
0350 : m_process_address(LLDB_INVALID_ADDRESS), m_host_address(host_address),
0351 m_permissions(permissions), m_sect_type(sect_type), m_size(size),
0352 m_alignment(alignment), m_section_id(section_id) {
0353 if (name && name[0])
0354 m_name = name;
0355 }
0356
0357 void dump(Log *log);
0358 };
0359
0360 bool CommitOneAllocation(lldb::ProcessSP &process_sp, Status &error,
0361 AllocationRecord &record);
0362
0363 typedef std::vector<AllocationRecord> RecordVector;
0364 RecordVector m_records;
0365
0366 std::unique_ptr<llvm::LLVMContext> m_context_up;
0367 std::unique_ptr<llvm::ExecutionEngine> m_execution_engine_up;
0368 std::unique_ptr<llvm::ObjectCache> m_object_cache_up;
0369 std::unique_ptr<llvm::Module>
0370 m_module_up;
0371 llvm::Module *m_module;
0372 std::vector<std::string> m_cpu_features;
0373 std::vector<JittedFunction> m_jitted_functions;
0374
0375
0376 std::vector<JittedGlobalVariable> m_jitted_global_variables;
0377
0378
0379
0380
0381 const ConstString m_name;
0382 SymbolContext m_sym_ctx;
0383 std::vector<ConstString> m_failed_lookups;
0384
0385 std::atomic<bool> m_did_jit;
0386
0387 lldb::addr_t m_function_load_addr;
0388 lldb::addr_t m_function_end_load_addr;
0389
0390 bool m_strip_underscore = true;
0391
0392 bool m_reported_allocations;
0393
0394
0395
0396
0397
0398
0399 };
0400
0401 }
0402
0403 #endif