Skip to content

Commit e2bd221

Browse files
committed
Prefer C DILabel API for POSIX, fallback to CPP
1 parent 37cbc6c commit e2bd221

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

src/llvm_backend_debug.cpp

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,30 +1302,75 @@ gb_internal void add_debug_info_for_global_constant_from_entity(lbGenerator *gen
13021302
// on POSIX systems so that older solibs that don't have the C API still work.
13031303
#ifndef _WIN32
13041304

1305-
// Don't you just love a good ol' mangled name? I know I do.
1306-
// Mangled name of "llvm::DIBuilder::createLabel(DIScope, StringRef, DIFile, unsigned, bool)"
1307-
extern "C" LLVMMetadataRef _ZN4llvm9DIBuilder11createLabelEPNS_7DIScopeENS_9StringRefEPNS_6DIFileEjb(
1305+
typedef LLVMMetadataRef (*LLVMDIBuilderCreateLabelPosixCPP)(
13081306
LLVMDIBuilderRef Builder,
13091307
LLVMMetadataRef Scope,
1310-
String Name, // Not technically proper to do, but it's ABI compatible for now...
1308+
// Using String here is not technically proper to do, but it's ABI
1309+
// compatible for the ABIs and older solib versions that we care about.
1310+
String Name,
13111311
LLVMMetadataRef File,
13121312
unsigned int LineNo,
13131313
bool AlwaysPreserve);
1314+
typedef LLVMMetadataRef (*LLVMDIBuilderCreateLabelPosixC)(
1315+
LLVMDIBuilderRef Builder, LLVMMetadataRef Context, const char *Name, size_t NameLen,
1316+
LLVMMetadataRef File, unsigned LineNo, LLVMBool AlwaysPreserve);
1317+
static inline LLVMMetadataRef LLVMDIBuilderCreateLabelPosix(
1318+
LLVMDIBuilderRef Builder, LLVMMetadataRef Context, const char *Name, size_t NameLen,
1319+
LLVMMetadataRef File, unsigned LineNo, LLVMBool AlwaysPreserve) {
1320+
static LLVMDIBuilderCreateLabelPosixC capi = nullptr;
1321+
static LLVMDIBuilderCreateLabelPosixCPP cppapi = nullptr;
1322+
if (capi == nullptr && cppapi == nullptr) {
1323+
capi = (LLVMDIBuilderCreateLabelPosixC)dlsym(RTLD_NEXT,
1324+
"LLVMDIBuilderCreateLabel");
1325+
1326+
// Mangled name of "llvm::DIBuilder::createLabel(DIScope, StringRef, DIFile, unsigned, bool)"
1327+
if (capi == nullptr)
1328+
cppapi = (LLVMDIBuilderCreateLabelPosixCPP)dlsym(RTLD_NEXT,
1329+
"_ZN4llvm9DIBuilder11createLabelEPNS_7DIScopeENS_9StringRefEPNS_6DIFileEjb");
1330+
}
1331+
if (capi != nullptr)
1332+
return capi(Builder, Context, Name, NameLen, File, LineNo, AlwaysPreserve);
1333+
if (cppapi != nullptr)
1334+
return cppapi(Builder, Context, String{(u8 *)Name, (isize)NameLen}, File, LineNo, AlwaysPreserve);
1335+
return nullptr;
1336+
}
13141337

1315-
// Mangled name of "llvm::DIBuilder::insertLabel(DILabel, DILocation, BasicBlock)"
1316-
extern "C" LLVMMetadataRef _ZN4llvm9DIBuilder11insertLabelEPNS_7DILabelEPKNS_10DILocationEPNS_10BasicBlockE(
1338+
typedef LLVMDbgRecordRef (*LLVMDIBuilderInsertLabelAtEndPosixCPP)(
1339+
LLVMDIBuilderRef Builder,
1340+
LLVMMetadataRef Label,
1341+
LLVMMetadataRef Location,
1342+
LLVMBasicBlockRef Block);
1343+
typedef LLVMDbgRecordRef (*LLVMDIBuilderInsertLabelAtEndPosixC)(
13171344
LLVMDIBuilderRef Builder,
13181345
LLVMMetadataRef Label,
13191346
LLVMMetadataRef Location,
13201347
LLVMBasicBlockRef Block);
1348+
static inline LLVMDbgRecordRef LLVMDIBuilderInsertLabelAtEndPosix(
1349+
LLVMDIBuilderRef Builder, LLVMMetadataRef LabelInfo,
1350+
LLVMMetadataRef Location, LLVMBasicBlockRef InsertAtEnd) {
1351+
static LLVMDIBuilderInsertLabelAtEndPosixC capi = nullptr;
1352+
static LLVMDIBuilderInsertLabelAtEndPosixCPP cppapi = nullptr;
1353+
if (capi == nullptr && cppapi == nullptr) {
1354+
capi = (LLVMDIBuilderInsertLabelAtEndPosixC)dlsym(RTLD_NEXT,
1355+
"LLVMDIBuilderInsertLabelAtEnd");
1356+
1357+
// Mangled name of "llvm::DIBuilder::insertLabel(DILabel, DILocation, BasicBlock)"
1358+
if (capi == nullptr)
1359+
cppapi = (LLVMDIBuilderInsertLabelAtEndPosixCPP)dlsym(RTLD_NEXT,
1360+
"_ZN4llvm9DIBuilder11insertLabelEPNS_7DILabelEPKNS_10DILocationEPNS_10BasicBlockE");
1361+
}
1362+
if (capi != nullptr)
1363+
return capi(Builder, LabelInfo, Location, InsertAtEnd);
1364+
if (cppapi != nullptr)
1365+
return cppapi(Builder, LabelInfo, Location, InsertAtEnd);
1366+
return nullptr;
1367+
}
13211368

13221369
#define LLVMDIBuilderCreateLabel(Builder, Context, Name, NameLen, File, LineNo, AlwaysPreserve) \
1323-
_ZN4llvm9DIBuilder11createLabelEPNS_7DIScopeENS_9StringRefEPNS_6DIFileEjb( \
1324-
Builder, Context, String{(u8 *)Name,(isize)NameLen}, File, LineNo, AlwaysPreserve)
1370+
LLVMDIBuilderCreateLabelPosix(Builder, Context, Name, NameLen, File, LineNo, AlwaysPreserve)
13251371

13261372
#define LLVMDIBuilderInsertLabelAtEnd(Builder, LabelInfo, Location, InsertAtEnd) \
1327-
_ZN4llvm9DIBuilder11insertLabelEPNS_7DILabelEPKNS_10DILocationEPNS_10BasicBlockE( \
1328-
Builder, LabelInfo, Location, InsertAtEnd)
1373+
LLVMDIBuilderInsertLabelAtEndPosix(Builder, LabelInfo, Location, InsertAtEnd)
13291374

13301375
#endif
13311376
gb_internal void lb_add_debug_label(lbProcedure *p, Ast *label, lbBlock *target) {

0 commit comments

Comments
 (0)