Skip to content

Commit aca15d1

Browse files
committed
Reduce code duplication for obtaining libc file name
1 parent 88044b9 commit aca15d1

3 files changed

Lines changed: 28 additions & 40 deletions

File tree

src/native/libs/System.Native/pal_dynamicload.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,13 @@
77
#include <dlfcn.h>
88
#include <string.h>
99

10-
#if HAVE_GNU_LIBNAMES_H
11-
#include <gnu/lib-names.h>
12-
#endif
13-
1410
void* SystemNative_LoadLibrary(const char* filename)
1511
{
16-
// Check whether we have been requested to load 'libc'. If that's the case, then:
17-
// * For Linux, use the full name of the library that is defined in <gnu/lib-names.h> by the
18-
// LIBC_SO constant. The problem is that calling dlopen("libc.so") will fail for libc even
19-
// though it works for other libraries. The reason is that libc.so is just linker script
20-
// (i.e. a test file).
21-
// As a result, we have to use the full name (i.e. lib.so.6) that is defined by LIBC_SO.
22-
// * For macOS, use constant value absolute path "/usr/lib/libc.dylib".
23-
// * For FreeBSD, use constant value "libc.so.7".
24-
// * For rest of Unices, use constant value "libc.so".
12+
// Check whether we have been requested to load 'libc'. If that's the case, then use the
13+
// correct file name based on the current platform.
2514
if (strcmp(filename, "libc") == 0)
2615
{
27-
#if defined(__APPLE__)
28-
filename = "/usr/lib/libc.dylib";
29-
#elif defined(__FreeBSD__)
30-
filename = "libc.so.7";
31-
#elif defined(LIBC_SO)
32-
filename = LIBC_SO;
33-
#else
34-
filename = "libc.so";
35-
#endif
16+
filename = LIBC_FILENAME;
3617
}
3718

3819
return dlopen(filename, RTLD_LAZY);

src/native/libs/System.Native/pal_dynamicload.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@
66
#include "pal_compiler.h"
77
#include "pal_types.h"
88

9+
#if HAVE_GNU_LIBNAMES_H
10+
#include <gnu/lib-names.h>
11+
#endif
12+
13+
// libc file name:
14+
// * For Linux, use the full name of the library that is defined in <gnu/lib-names.h> by the
15+
// LIBC_SO constant. The problem is that calling dlopen("libc.so") will fail for libc even
16+
// though it works for other libraries. The reason is that libc.so is just linker script
17+
// (i.e. a test file).
18+
// As a result, we have to use the full name (i.e. lib.so.6) that is defined by LIBC_SO.
19+
// * For macOS, use constant value absolute path "/usr/lib/libc.dylib".
20+
// * For FreeBSD, use constant value "libc.so.7".
21+
// * For rest of Unices, use constant value "libc.so".
22+
#if defined(__APPLE__)
23+
#define LIBC_FILENAME "/usr/lib/libc.dylib"
24+
#elif defined(__FreeBSD__)
25+
#define LIBC_FILENAME "libc.so.7"
26+
#elif defined(LIBC_SO)
27+
#define LIBC_FILENAME LIBC_SO
28+
#else
29+
#define LIBC_FILENAME "libc.so"
30+
#endif
31+
932
PALEXPORT void* SystemNative_LoadLibrary(const char* filename);
1033

1134
PALEXPORT void* SystemNative_GetProcAddress(void* handle, const char* symbol);

src/native/libs/System.Native/pal_interfaceaddresses.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pal_utilities.h"
88
#include "pal_safecrt.h"
99
#include "pal_networking.h"
10+
#include "pal_dynamicload.h"
1011

1112
#include <stdlib.h>
1213
#include <sys/types.h>
@@ -55,13 +56,6 @@
5556
#endif
5657
#endif
5758

58-
#if !HAVE_GETIFADDRS
59-
#include <dlfcn.h>
60-
#if HAVE_GNU_LIBNAMES_H
61-
#include <gnu/lib-names.h>
62-
#endif
63-
#endif
64-
6559
// Convert mask to prefix length e.g. 255.255.255.0 -> 24
6660
// mask parameter is pointer to buffer where address starts and length is
6761
// buffer length e.g. 4 for IPv4 and 16 for IPv6.
@@ -139,17 +133,7 @@ static bool ensure_getifaddrs_impl_available() {
139133
{
140134
loading_getifaddrs_already_attempted = true;
141135

142-
#if defined(__APPLE__)
143-
const char *libc_path = "/usr/lib/libc.dylib";
144-
#elif defined(__FreeBSD__)
145-
const char *libc_path = "libc.so.7";
146-
#elif defined(LIBC_SO)
147-
const char *libc_path = LIBC_SO;
148-
#else
149-
const char *libc_path = "libc.so";
150-
#endif
151-
152-
void *libc = dlopen(libc_path, RTLD_NOW);
136+
void *libc = dlopen(LIBC_FILENAME, RTLD_NOW);
153137
if (libc)
154138
{
155139
getifaddrs_impl = (getifaddrs_fptr)dlsym(libc, "getifaddrs");

0 commit comments

Comments
 (0)