From 15a8fc065fe865c13be985eb617ea20663926957 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 17 Nov 2020 22:52:21 +0100 Subject: [PATCH 1/3] WinAdapter: Export helper functions that operate on support types The implementation of these support types completely depends on DXC. As such applications using libdxcompiler.so have to guess and/or look at the source code to understand how they are implemented (eg prefix size for BSTR), and assume this is not going to change. Applications using these newly exported symbols can do it "the right way" as if they are on Windows. --- include/dxc/Support/WinAdapter.h | 9 +++++++-- lib/DxcSupport/WinAdapter.cpp | 4 ++-- tools/clang/tools/dxcompiler/dxcapi.cpp | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/dxc/Support/WinAdapter.h b/include/dxc/Support/WinAdapter.h index 52afcbfefd..f7057fc82a 100644 --- a/include/dxc/Support/WinAdapter.h +++ b/include/dxc/Support/WinAdapter.h @@ -216,6 +216,10 @@ #define UInt32Add UIntAdd #define Int32ToUInt32 IntToUInt +#ifndef DXC_API_IMPORT +#define DXC_API_IMPORT __attribute__((visibility("default"))) +#endif + //===--------------------- HRESULT Related Macros -------------------------===// #define S_OK ((HRESULT)0L) @@ -935,9 +939,10 @@ class CHeapPtr : public CHeapPtrBase { //===--------------------------- BSTR Allocation --------------------------===// -void SysFreeString(BSTR bstrString); +extern "C" DXC_API_IMPORT void __stdcall SysFreeString(BSTR bstrString); // Allocate string with length prefix -BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui); +extern "C" DXC_API_IMPORT BSTR __stdcall SysAllocStringLen(const OLECHAR *strIn, + UINT ui); //===--------------------- UTF-8 Related Types ----------------------------===// diff --git a/lib/DxcSupport/WinAdapter.cpp b/lib/DxcSupport/WinAdapter.cpp index 27bd7c3c7e..ce68818581 100644 --- a/lib/DxcSupport/WinAdapter.cpp +++ b/lib/DxcSupport/WinAdapter.cpp @@ -47,14 +47,14 @@ void CAllocator::Free(void *p) throw() { free(p); } //===--------------------------- BSTR Allocation --------------------------===// -void SysFreeString(BSTR bstrString) { +DXC_API_IMPORT void __stdcall SysFreeString(BSTR bstrString) { if (bstrString) free((void *)((uintptr_t)bstrString - sizeof(uint32_t))); } // Allocate string with length prefix // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr -BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui) { +DXC_API_IMPORT BSTR __stdcall SysAllocStringLen(const OLECHAR *strIn, UINT ui) { uint32_t *blobOut = (uint32_t *)malloc(sizeof(uint32_t) + (ui + 1) * sizeof(OLECHAR)); diff --git a/tools/clang/tools/dxcompiler/dxcapi.cpp b/tools/clang/tools/dxcompiler/dxcapi.cpp index d35140b626..255836824c 100644 --- a/tools/clang/tools/dxcompiler/dxcapi.cpp +++ b/tools/clang/tools/dxcompiler/dxcapi.cpp @@ -9,13 +9,15 @@ // // /////////////////////////////////////////////////////////////////////////////// -#include "dxc/Support/WinIncludes.h" - +#ifndef DXC_API_IMPORT #ifdef _WIN32 #define DXC_API_IMPORT __declspec(dllexport) #else #define DXC_API_IMPORT __attribute__ ((visibility ("default"))) #endif +#endif + +#include "dxc/Support/WinIncludes.h" #include "dxc/dxcisense.h" #include "dxc/dxctools.h" From 9200fff35ed9d44893e1a7d6fbbddaad62423a7c Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 17 Nov 2020 23:15:11 +0100 Subject: [PATCH 2/3] WinAdapter: Export SysStringLen helper for BSTR --- include/dxc/Support/WinAdapter.h | 2 ++ lib/DxcSupport/WinAdapter.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/dxc/Support/WinAdapter.h b/include/dxc/Support/WinAdapter.h index f7057fc82a..f5a6918746 100644 --- a/include/dxc/Support/WinAdapter.h +++ b/include/dxc/Support/WinAdapter.h @@ -943,6 +943,8 @@ extern "C" DXC_API_IMPORT void __stdcall SysFreeString(BSTR bstrString); // Allocate string with length prefix extern "C" DXC_API_IMPORT BSTR __stdcall SysAllocStringLen(const OLECHAR *strIn, UINT ui); +extern "C" DXC_API_IMPORT UINT __stdcall SysStringByteLen(BSTR bstr); +extern "C" DXC_API_IMPORT UINT __stdcall SysStringLen(BSTR pbstr); //===--------------------- UTF-8 Related Types ----------------------------===// diff --git a/lib/DxcSupport/WinAdapter.cpp b/lib/DxcSupport/WinAdapter.cpp index ce68818581..1974500417 100644 --- a/lib/DxcSupport/WinAdapter.cpp +++ b/lib/DxcSupport/WinAdapter.cpp @@ -75,6 +75,17 @@ DXC_API_IMPORT BSTR __stdcall SysAllocStringLen(const OLECHAR *strIn, UINT ui) { return strOut; } +DXC_API_IMPORT UINT __stdcall SysStringByteLen(BSTR bstr) { + if (!bstr) + return 0; + + return ((UINT *)bstr)[-1]; +} + +DXC_API_IMPORT UINT __stdcall SysStringLen(BSTR pbstr) { + return SysStringByteLen(pbstr) / 4; +} + //===---------------------- Char converstion ------------------------------===// const char *CPToLocale(uint32_t CodePage) { From 3c42468ba660b319797d0a976b4b9e0b8a19b5f7 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 17 Nov 2020 23:16:22 +0100 Subject: [PATCH 3/3] WinAdapter: Export symbols for CoTaskMem{Alloc,Free} Some functions return lists allocated with CoTaskMemAlloc. Applications using libdxcompiler.so currently have to look into the code to understand that these should be deallocated with free() instead of delete[]. Export the CoTaskMemFree function so that there is no guesswork involved anymore: the application can now call this function and always free it in the right way. --- include/dxc/Support/WinAdapter.h | 8 +++++--- lib/DxcSupport/WinAdapter.cpp | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/dxc/Support/WinAdapter.h b/include/dxc/Support/WinAdapter.h index f5a6918746..72567aadd3 100644 --- a/include/dxc/Support/WinAdapter.h +++ b/include/dxc/Support/WinAdapter.h @@ -42,9 +42,6 @@ #define C_ASSERT(expr) static_assert((expr), "") #define ATLASSERT assert -#define CoTaskMemAlloc malloc -#define CoTaskMemFree free - #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0])) #define _countof(a) (sizeof(a) / sizeof(*(a))) @@ -946,6 +943,11 @@ extern "C" DXC_API_IMPORT BSTR __stdcall SysAllocStringLen(const OLECHAR *strIn, extern "C" DXC_API_IMPORT UINT __stdcall SysStringByteLen(BSTR bstr); extern "C" DXC_API_IMPORT UINT __stdcall SysStringLen(BSTR pbstr); +//===-------------------------- CoTask Allocation -------------------------===// + +extern "C" DXC_API_IMPORT LPVOID __stdcall CoTaskMemAlloc(SIZE_T cb); +extern "C" DXC_API_IMPORT void __stdcall CoTaskMemFree(LPVOID pv); + //===--------------------- UTF-8 Related Types ----------------------------===// // Code Page diff --git a/lib/DxcSupport/WinAdapter.cpp b/lib/DxcSupport/WinAdapter.cpp index 1974500417..78de917b6c 100644 --- a/lib/DxcSupport/WinAdapter.cpp +++ b/lib/DxcSupport/WinAdapter.cpp @@ -86,6 +86,12 @@ DXC_API_IMPORT UINT __stdcall SysStringLen(BSTR pbstr) { return SysStringByteLen(pbstr) / 4; } +//===-------------------------- CoTask Allocation -------------------------===// + +DXC_API_IMPORT LPVOID __stdcall CoTaskMemAlloc(SIZE_T cb) { return malloc(cb); } + +DXC_API_IMPORT void __stdcall CoTaskMemFree(LPVOID pv) { free(pv); } + //===---------------------- Char converstion ------------------------------===// const char *CPToLocale(uint32_t CodePage) {