| 1 | /*========================== begin_copyright_notice ============================ |
| 2 | |
| 3 | Copyright (C) 2017-2021 Intel Corporation |
| 4 | |
| 5 | SPDX-License-Identifier: MIT |
| 6 | |
| 7 | ============================= end_copyright_notice ===========================*/ |
| 8 | |
| 9 | #include "../headers/LoadBuffer.h" |
| 10 | #include "llvm/Config/llvm-config.h" |
| 11 | #include <cstdlib> |
| 12 | #include <stdio.h> |
| 13 | #include <fstream> |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | #ifdef LLVM_ON_UNIX |
| 18 | #include <dlfcn.h> |
| 19 | #include <inttypes.h> |
| 20 | #include <libgen.h> |
| 21 | #include <link.h> |
| 22 | #include <linux/limits.h> |
| 23 | #include <stddef.h> |
| 24 | #include <stdint.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | char *llvm::LoadCharBufferFromResource(unsigned int ResNumber, const char *pResType, long unsigned int &m_buffSize) { |
| 28 | |
| 29 | // Symbol Name is <type>_<number> |
| 30 | char name[73]; // 64 + 9 for prefix |
| 31 | char size_name[78]; // 64 + 9 for prefix + 5 for suffix |
| 32 | void *module; |
| 33 | void *symbol; |
| 34 | uint32_t size; |
| 35 | |
| 36 | snprintf(name, sizeof(name), "IDR_CTH_H_%s_%u", pResType, ResNumber); |
| 37 | snprintf(size_name, sizeof(size_name), "IDR_CTH_H_%s_%u_size", pResType, ResNumber); |
| 38 | |
| 39 | module = RTLD_DEFAULT; |
| 40 | symbol = dlsym(module, size_name); |
| 41 | if (!symbol) { |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | size = *(uint32_t *)symbol; |
| 46 | |
| 47 | symbol = dlsym(module, name); |
| 48 | if (!symbol) { |
| 49 | return NULL; |
| 50 | } |
| 51 | m_buffSize = size; |
| 52 | |
| 53 | return (char *)symbol; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | #ifdef WIN32 |
| 58 | #include <Windows.h> |
| 59 | |
| 60 | char *llvm::LoadCharBufferFromResource(unsigned int ResNumber, const char *pResType, unsigned long &m_buffSize) { |
| 61 | HMODULE hMod = NULL; |
| 62 | |
| 63 | char ResName[5] = {'-'}; |
| 64 | _snprintf(ResName, sizeof(ResName), "#%d", ResNumber); |
| 65 | |
| 66 | // Get the handle to the current module |
| 67 | GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 68 | (LPCSTR)llvm::LoadCharBufferFromResource, &hMod); |
| 69 | if (hMod != NULL) { |
| 70 | // Locate the resource |
| 71 | HRSRC hRes = FindResourceA(hMod, ResName, pResType); |
| 72 | if (hRes != NULL) { |
| 73 | // Load the resource |
| 74 | HGLOBAL hBytes = LoadResource(hMod, hRes); |
| 75 | if (hBytes != NULL) { |
| 76 | // Get the base address to the resource. This call doesn't really lock it |
| 77 | char *m_buff = (char *)LockResource(hBytes); |
| 78 | if (m_buff != NULL) { |
| 79 | // Get the buffer size |
| 80 | m_buffSize = SizeofResource(hMod, hRes); |
| 81 | if (m_buffSize != 0) { |
| 82 | return m_buff; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | #endif // #ifdef LLVM_ON_WIN32 |