Rizin
unix-like reverse engineering framework and cli tools
dl.c File Reference
#include "uv.h"
#include "internal.h"

Go to the source code of this file.

Functions

static int uv__dlerror (uv_lib_t *lib, const char *filename, DWORD errorno)
 
int uv_dlopen (const char *filename, uv_lib_t *lib)
 
void uv_dlclose (uv_lib_t *lib)
 
int uv_dlsym (uv_lib_t *lib, const char *name, void **ptr)
 
const char * uv_dlerror (const uv_lib_t *lib)
 
static void uv__format_fallback_error (uv_lib_t *lib, int errorno)
 

Function Documentation

◆ uv__dlerror()

static int uv__dlerror ( uv_lib_t lib,
const char *  filename,
DWORD  errorno 
)
static

Definition at line 93 of file dl.c.

93  {
94  DWORD_PTR arg;
95  DWORD res;
96  char* msg;
97 
98  if (lib->errmsg) {
99  LocalFree(lib->errmsg);
100  lib->errmsg = NULL;
101  }
102 
103  if (errorno == 0)
104  return 0;
105 
106  res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
107  FORMAT_MESSAGE_FROM_SYSTEM |
108  FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
109  MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
110  (LPSTR) &lib->errmsg, 0, NULL);
111 
112  if (!res && (GetLastError() == ERROR_MUI_FILE_NOT_FOUND ||
113  GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND)) {
114  res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
115  FORMAT_MESSAGE_FROM_SYSTEM |
116  FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
117  0, (LPSTR) &lib->errmsg, 0, NULL);
118  }
119 
120  if (res && errorno == ERROR_BAD_EXE_FORMAT && strstr(lib->errmsg, "%1")) {
121  msg = lib->errmsg;
122  lib->errmsg = NULL;
123  arg = (DWORD_PTR) filename;
124  res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
125  FORMAT_MESSAGE_ARGUMENT_ARRAY |
126  FORMAT_MESSAGE_FROM_STRING,
127  msg,
128  0, 0, (LPSTR) &lib->errmsg, 0, (va_list*) &arg);
129  LocalFree(msg);
130  }
131 
132  if (!res)
133  uv__format_fallback_error(lib, errorno);
134 
135  return -1;
136 }
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
#define NULL
Definition: cris-opc.c:27
const char * filename
Definition: ioapi.h:137
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
char * errmsg
Definition: unix.h:220
static void uv__format_fallback_error(uv_lib_t *lib, int errorno)
Definition: dl.c:78
#define ERROR_MUI_FILE_NOT_FOUND
Definition: winapi.h:4646
DWORD

References arg(), DWORD, uv_lib_t::errmsg, ERROR_MUI_FILE_NOT_FOUND, msg, NULL, and uv__format_fallback_error().

Referenced by uv_dlopen(), and uv_dlsym().

◆ uv__format_fallback_error()

static void uv__format_fallback_error ( uv_lib_t lib,
int  errorno 
)
static

Definition at line 78 of file dl.c.

78  {
79  static const CHAR fallback_error[] = "error: %1!d!";
80  DWORD_PTR args[1];
81  args[0] = (DWORD_PTR) errorno;
82 
83  FormatMessageA(FORMAT_MESSAGE_FROM_STRING |
84  FORMAT_MESSAGE_ARGUMENT_ARRAY |
85  FORMAT_MESSAGE_ALLOCATE_BUFFER,
86  fallback_error, 0, 0,
87  (LPSTR) &lib->errmsg,
88  0, (va_list*) args);
89 }
int args
Definition: mipsasm.c:18

References args, and uv_lib_t::errmsg.

Referenced by uv__dlerror().

◆ uv_dlclose()

void uv_dlclose ( uv_lib_t lib)

Definition at line 52 of file dl.c.

52  {
53  if (lib->errmsg) {
54  LocalFree((void*)lib->errmsg);
55  lib->errmsg = NULL;
56  }
57 
58  if (lib->handle) {
59  /* Ignore errors. No good way to signal them without leaking memory. */
60  FreeLibrary(lib->handle);
61  lib->handle = NULL;
62  }
63 }
void * handle
Definition: unix.h:219

References uv_lib_t::errmsg, uv_lib_t::handle, NULL, and uv__free().

◆ uv_dlerror()

const char* uv_dlerror ( const uv_lib_t lib)

Definition at line 73 of file dl.c.

73  {
74  return lib->errmsg ? lib->errmsg : "no error";
75 }

References uv_lib_t::errmsg.

Referenced by main().

◆ uv_dlopen()

int uv_dlopen ( const char *  filename,
uv_lib_t lib 
)

Definition at line 28 of file dl.c.

28  {
29  WCHAR filename_w[32768];
30 
31  lib->handle = NULL;
32  lib->errmsg = NULL;
33 
34  if (!MultiByteToWideChar(CP_UTF8,
35  0,
36  filename,
37  -1,
38  filename_w,
39  ARRAY_SIZE(filename_w))) {
40  return uv__dlerror(lib, filename, GetLastError());
41  }
42 
43  lib->handle = LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
44  if (lib->handle == NULL) {
45  return uv__dlerror(lib, filename, GetLastError());
46  }
47 
48  return 0;
49 }
#define ARRAY_SIZE(a)
static int uv__dlerror(uv_lib_t *lib, const char *filename, DWORD errorno)
Definition: dl.c:93

References ARRAY_SIZE, uv_lib_t::errmsg, uv_lib_t::handle, NULL, and uv__dlerror().

Referenced by main().

◆ uv_dlsym()

int uv_dlsym ( uv_lib_t lib,
const char *  name,
void **  ptr 
)

Definition at line 66 of file dl.c.

66  {
67  /* Cast though integer to suppress pedantic warning about forbidden cast. */
68  *ptr = (void*)(uintptr_t) GetProcAddress(lib->handle, name);
69  return uv__dlerror(lib, "", *ptr ? 0 : GetLastError());
70 }
_W64 unsigned int uintptr_t
Definition: z80asm.h:102

References uv_lib_t::handle, and uv__dlerror().

Referenced by main().