026f89b3ea5dbe63679a39240e08327cdf03b23c
[platform/upstream/coreclr.git] / src / pal / src / loader / modulename.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 /*++
6
7
8
9 Module Name:
10
11     modulename.cpp
12
13 Abstract:
14
15     Implementation of internal functions to get module names
16
17
18
19 --*/
20
21 #include "pal/thread.hpp"
22 #include "pal/malloc.hpp"
23 #include "pal/palinternal.h"
24 #include "pal/dbgmsg.h"
25 #include "pal/modulename.h"
26
27 #if NEED_DLCOMPAT
28 #include "dlcompat.h"
29 #else   // NEED_DLCOMPAT
30 #include <dlfcn.h>
31 #endif  // NEED_DLCOMPAT
32
33 #if defined(_AIX)
34 #include <sys/ldr.h>
35 #endif
36
37 using namespace CorUnix;
38
39 SET_DEFAULT_DEBUG_CHANNEL(LOADER);
40
41 #if defined(_AIX)
42 /*++
43     GetLibRotorNameViaLoadQuery
44
45     Retrieve the full path of the librotor_pal.so using loadquery()
46
47 Parameters:
48     pszBuf - CHAR array of MAX_PATH_FNAME length
49
50 Return value:
51     0 on success
52     -1 on failure, with last error set
53 --*/
54 int GetLibRotorNameViaLoadQuery(LPSTR pszBuf)
55 {
56     CHAR*               pLoadQueryBuf = NULL;
57     UINT                cbBuf = 1024;
58     struct ld_info *    pInfo = NULL;
59     INT                 iLQRetVal = -1;
60     INT                 iRetVal = -1;
61     CPalThread *pThread = NULL;
62
63     if (!pszBuf)
64     {
65         ASSERT("GetLibRotorNameViaLoadQuery requires non-NULL pszBuf\n");
66         SetLastError(ERROR_INTERNAL_ERROR);
67         goto Done;
68     }
69
70     pThread = InternalGetCurrentThread();
71     // Loop trying to call loadquery with enough memory until either 
72     // 1) we succeed, 2) we run out of memory or 3) loadquery throws
73     // an error other than ENOMEM
74     while (iLQRetVal != 0)
75     {
76         pLoadQueryBuf = (CHAR*) InternalMalloc (pThread, cbBuf * sizeof(char));
77         if (!pLoadQueryBuf)
78         {
79             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
80             goto Done;
81         }
82         iLQRetVal = loadquery(L_GETINFO, pLoadQueryBuf, cbBuf);
83         if (iLQRetVal < 0)
84         {
85             free(pThread, pLoadQueryBuf);
86             pLoadQueryBuf = NULL;
87             DWORD dwLastError = GetLastError();
88             if (dwLastError == ERROR_NOT_ENOUGH_MEMORY)
89             {
90                 // The buffer's too small.  Try twice as large as a guess...
91                 cbBuf *= 2;
92             }
93             else
94             {
95                 SetLastError(ERROR_INTERNAL_ERROR);
96                 goto Done;
97             }
98         }
99     }
100
101     // We successfully called loadquery, so now see if we can find 
102     // librotor_pal.a in the module list
103     if (pLoadQueryBuf)
104     {
105         pInfo = (struct ld_info *)pLoadQueryBuf;        
106         while (TRUE)
107         {  
108             if (strstr(pInfo->ldinfo_filename, "librotor_pal.a"))
109             {
110                 UINT cchFileName = strlen(pInfo->ldinfo_filename);
111                 if (cchFileName + 1  > MAX_PATH_FNAME)
112                 {
113                     ASSERT("Filename returned by loadquery was longer than MAX_PATH_FNAME!\n");
114                     SetLastError(ERROR_INTERNAL_ERROR);
115                     goto Done;
116                 }
117                 else
118                 {
119                     // The buffer should be large enough to accomodate the filename.
120                     // So, we send in the size of the filename+1
121                     strcpy_s(pszBuf, MAX_PATH_FNAME, pInfo->ldinfo_filename);
122                     iRetVal = 0;
123                     goto Done;
124                 }
125             }
126             else
127             {
128                 // The (wacky) design of ld_info is that the value of next is an offset in 
129                 // bytes rather than a pointer.  So we need this weird cast to char * to get
130                 // the pointer math correct.
131                 if (pInfo->ldinfo_next == 0)
132                     break;
133                 else
134                     pInfo = (struct ld_info *) ((char *)pInfo + pInfo->ldinfo_next);
135             }
136         }
137     }
138 Done:
139     if (pLoadQueryBuf)
140         free(pThread, pLoadQueryBuf);
141     return iRetVal;
142 }
143 #endif // defined(_AIX)
144
145 /*++
146     PAL_dladdr
147
148     Internal wrapper for dladder used only to get module name
149
150 Parameters:
151     None
152
153 Return value:
154     Pointer to string with the fullpath to the librotor_pal.so being
155     used.
156
157     NULL if error occurred.
158
159 Notes: 
160     The string returned by this function is owned by the OS.
161     If you need to keep it, strdup() it, because it is unknown how long
162     this ptr will point at the string you want (over the lifetime of
163     the system running)  It is only safe to use it immediately after calling
164     this function.
165 --*/
166 const char *PAL_dladdr(LPVOID ProcAddress)
167 {
168 #if defined(_AIX) || defined(__hppa__)
169     /* dladdr is not supported on AIX or 32-bit HPUX-PARISC */
170     return (NULL);
171 #elif defined(_HPUX_) && defined(_IA64_)
172     /* dladdr is not supported on HP-UX/IA64.  That said, PAL_dladdr just returns to module name
173        and we can get that via dlgetname.  So use that for HPUX.  */
174     {
175         char*               pszName = NULL;
176         load_module_desc    desc;
177         __uint64_t          uimodret = NULL;
178         uimodret = dlmodinfo((__uint64_t)ProcAddress, &desc, sizeof(desc), NULL, 0, 0);
179         if (!uimodret)
180         {
181             WARN("dlmodinfo call failed! dlerror says '%s'\n", dlerror());
182             return NULL;
183         }
184         pszName = dlgetname(&desc, sizeof(desc), NULL, 0, 0);
185         if (!pszName)
186         {
187             WARN("dlgetname desc didn't describe a loaded module?! dlerror says '%s'\n", dlerror());
188             return NULL;
189         }
190         return pszName;
191     }
192 #else
193     Dl_info dl_info;
194     if (!dladdr(ProcAddress, &dl_info))
195     {
196         WARN("dladdr() call failed! dlerror says '%s'\n", dlerror());
197         /* If we get an error, return NULL */
198         return (NULL);
199     }
200     else 
201     {
202         /* Return the module name */ 
203         return dl_info.dli_fname;
204     }
205 #endif
206 }
207