This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathsystem-library-linux.hpp
More file actions
121 lines (100 loc) · 4.09 KB
/
system-library-linux.hpp
File metadata and controls
121 lines (100 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*-c++-*-
Copyright (C) 2017 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "system-private.h"
#include <foundation-auto.h>
/* ================================================================
* Linux Handle Class
* ================================================================ */
/* The __MCSLibraryHandleLinux class is a specialization of the POSIX handle
* implementation. It overrides CreateWithAddress to handle the case where an
* address in the executable is given, in this case, dladdr does not return
* a dlopen'able dli_fname, but the link_map does. Additionally, it implements
* CopyNativePath by using dlinfo and RTLD_DI_LINKMAP. This returns a usable
* path in all cases *except* when the handle belongs to the executable image.
* In this case, /proc/self/exe is fetched, resolved and returned as the
* path of the library.
*/
#include "system-library-posix.hpp"
#include <link.h>
#include <sys/stat.h>
#include <unistd.h>
class __MCSLibraryHandleLinux: public __MCSLibraryHandlePosix
{
public:
/* This function will not work 100% correctly (for all addresses) until
* we compile with PIC. */
bool CreateWithAddress(void *p_address)
{
void *t_dl_handle = nullptr;
/* We use dladdr1 to get both the Dl_info and the link map entry. */
Dl_info t_addr_info;
struct link_map *t_link_map = nullptr;
if (dladdr1(p_address,
&t_addr_info,
(void **)&t_link_map,
RTLD_DL_LINKMAP) != 0)
{
/* If the link_map has a non-empty l_name, then we can use that to
* open another handle to the library.
* Otherwise, if it has a non-empty Dl_info.fli_name then we take it
* to be in the main executable. */
if (*(t_link_map->l_name) != '\0')
{
t_dl_handle = dlopen(t_link_map->l_name,
RTLD_LAZY);
}
else if (*(t_addr_info.dli_fname) != '\0')
{
t_dl_handle = dlopen(NULL,
RTLD_LAZY);
}
}
if (t_dl_handle == nullptr)
{
/* TODO: Use dlerror */
return __MCSLibraryThrowCreateWithAddressFailed(p_address);
}
m_handle = t_dl_handle;
return true;
}
bool
CopyNativePath(MCStringRef& r_native_path) const
{
struct link_map *t_link_map = nullptr;
if (dlinfo(m_handle,
RTLD_DI_LINKMAP,
&t_link_map) != 0)
{
return __MCSLibraryThrowResolvePathFailed();
}
/* If the link_map is NULL, or the l_name field is NULL or the l_name
* field is "", then we take it to be the main executable and use a
* different approach. */
if (t_link_map == nullptr ||
t_link_map->l_name == NULL ||
*(t_link_map->l_name) == '\0')
{
char *t_path =
realpath("/proc/self/exe",
nullptr);
if (t_path == nullptr)
{
return __MCSLibraryThrowResolvePathFailed();
}
return MCStringCreateWithSysString(t_path,
r_native_path);
}
return MCStringCreateWithSysString(t_link_map->l_name, r_native_path);
}
};
typedef class __MCSLibraryHandleLinux __MCSLibraryHandle;