Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 1b5d296

Browse files
authored
Merge pull request #2002 from dkgroot/dragonfly-core.sys.dragonflybsd
Add DragonFlyBSD support : Split PR : src/core/sys/dragonflybsd related changes merged-on-behalf-of: unknown
2 parents 2613173 + 2110fc8 commit 1b5d296

File tree

21 files changed

+2485
-2
lines changed

21 files changed

+2485
-2
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ src/core/sys/darwin/* @jacob-carlborg @klickverbot @etcimon @MartinNowak
3333
src/core/sys/freebsd/* @redstar @MartinNowak @Calrama @jmdavis
3434
src/core/sys/linux/* @Burgos @redstar @MartinNowak
3535
src/core/sys/netbsd/* @nrTQgc @joakim-noah
36+
src/core/sys/dragonflybsd/* @dkgroot
3637
src/core/sys/openbsd/* @redstar
3738
src/core/sys/osx/* @jacob-carlborg @klickverbot @etcimon @MartinNowak
3839
src/core/sys/posix/* @CyberShadow @MartinNowak @joakim-noah @redstar

mak/COPY

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ COPY=\
8686
$(IMPDIR)\core\sys\freebsd\sys\mman.d \
8787
$(IMPDIR)\core\sys\freebsd\time.d \
8888
\
89+
$(IMPDIR)\core\sys\dragonflybsd\dlfcn.d \
90+
$(IMPDIR)\core\sys\dragonflybsd\execinfo.d \
91+
\
92+
$(IMPDIR)\core\sys\dragonflybsd\netinet\in_.d \
93+
\
94+
$(IMPDIR)\core\sys\dragonflybsd\sys\cdefs.d \
95+
$(IMPDIR)\core\sys\dragonflybsd\pthread_np.d \
96+
$(IMPDIR)\core\sys\dragonflybsd\sys\_bitset.d \
97+
$(IMPDIR)\core\sys\dragonflybsd\sys\_cpuset.d \
98+
$(IMPDIR)\core\sys\dragonflybsd\sys\elf.d \
99+
$(IMPDIR)\core\sys\dragonflybsd\sys\elf32.d \
100+
$(IMPDIR)\core\sys\dragonflybsd\sys\elf64.d \
101+
$(IMPDIR)\core\sys\dragonflybsd\sys\elf_common.d \
102+
$(IMPDIR)\core\sys\dragonflybsd\sys\event.d \
103+
$(IMPDIR)\core\sys\dragonflybsd\sys\link_elf.d \
104+
$(IMPDIR)\core\sys\dragonflybsd\sys\mman.d \
105+
$(IMPDIR)\core\sys\dragonflybsd\time.d \
106+
\
89107
$(IMPDIR)\core\sys\linux\config.d \
90108
$(IMPDIR)\core\sys\linux\dlfcn.d \
91109
$(IMPDIR)\core\sys\linux\elf.d \

mak/SRCS

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ SRCS=\
7171
src\core\sys\freebsd\sys\mman.d \
7272
src\core\sys\freebsd\time.d \
7373
\
74+
src\core\sys\dragonflybsd\dlfcn.d \
75+
src\core\sys\dragonflybsd\execinfo.d \
76+
src\core\sys\dragonflybsd\netinet\in_.d \
77+
src\core\sys\dragonflybsd\sys\_bitset.d \
78+
src\core\sys\dragonflybsd\sys\_cpuset.d \
79+
src\core\sys\dragonflybsd\sys\cdefs.d \
80+
src\core\sys\dragonflybsd\sys\elf_common.d \
81+
src\core\sys\dragonflybsd\sys\elf.d \
82+
src\core\sys\dragonflybsd\sys\elf32.d \
83+
src\core\sys\dragonflybsd\sys\elf64.d \
84+
src\core\sys\dragonflybsd\sys\event.d \
85+
src\core\sys\dragonflybsd\sys\link_elf.d \
86+
src\core\sys\dragonflybsd\sys\mman.d \
87+
src\core\sys\dragonflybsd\time.d \
88+
\
7489
src\core\sys\linux\netinet\in_.d \
7590
src\core\sys\linux\netinet\tcp.d \
7691
src\core\sys\linux\stdio.d \

posix.mak

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ endif
5757

5858
# build with shared library support
5959
# (defaults to true on supported platforms, can be overridden w/ make SHARED=0)
60-
SHARED=$(if $(findstring $(OS),linux freebsd),1,)
60+
SHARED=$(if $(findstring $(OS),linux freebsd dragonflybsd),1,)
6161

6262
LINKDL=$(if $(findstring $(OS),linux),-L-ldl,)
6363

@@ -204,7 +204,7 @@ $(IMPDIR)/%.d : src/%.d
204204
######################## Build DMD if non-existent ##############################
205205

206206
$(DMD):
207-
make -C $(DMD_DIR)/src -f posix.mak BUILD=$(BUILD) OS=$(OS) MODEL=$(MODEL)
207+
$(MAKE) -C $(DMD_DIR)/src -f posix.mak BUILD=$(BUILD) OS=$(OS) MODEL=$(MODEL)
208208

209209
################### C/ASM Targets ############################
210210

src/core/sys/dragonflybsd/dlfcn.d

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* D header file for DragonFlyBSD
3+
*
4+
* Copyright: Copyright Martin Nowak 2012.
5+
* License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6+
* Authors: Martin Nowak,Diederik de Groot(port:DragonFlyBSD)
7+
* Copied: From core/sys/freebsd/sys
8+
*/
9+
module core.sys.dragonflybsd.dlfcn;
10+
11+
version (DragonFlyBSD):
12+
13+
public import core.sys.posix.dlfcn;
14+
15+
extern (C) nothrow @nogc @system:
16+
17+
/*
18+
* Modes and flags for dlopen().
19+
*/
20+
static assert(RTLD_LAZY == 1);
21+
static assert(RTLD_NOW == 2);
22+
enum RTLD_MODEMASK = 0x3;
23+
static assert(RTLD_GLOBAL == 0x100);
24+
static assert(RTLD_LOCAL == 0);
25+
enum RTLD_TRACE = 0x200;
26+
enum RTLD_NODELETE = 0x01000;
27+
enum RTLD_NOLOAD = 0x02000;
28+
29+
/*
30+
* Request arguments for dlinfo().
31+
*/
32+
enum RTLD_DI_LINKMAP = 2; /* Obtain link map. */
33+
enum RTLD_DI_SERINFO = 4; /* Obtain search path info. */
34+
enum RTLD_DI_SERINFOSIZE = 5; /* ... query for required space. */
35+
enum RTLD_DI_ORIGIN = 6; /* Obtain object origin */
36+
enum RTLD_DI_MAX = RTLD_DI_ORIGIN;
37+
38+
/*
39+
* Special handle arguments for dlsym()/dlinfo().
40+
*/
41+
enum RTLD_NEXT = cast(void *)-1; /* Search subsequent objects. */
42+
enum RTLD_DEFAULT = cast(void *)-2; /* Use default search algorithm. */
43+
enum RTLD_SELF = cast(void *)-3; /* Search the caller itself. */
44+
45+
/*
46+
* Structure filled in by dladdr().
47+
*/
48+
struct Dl_info {
49+
const(char) *dli_fname; /* Pathname of shared object. */
50+
void *dli_fbase; /* Base address of shared object. */
51+
const(char) *dli_sname; /* Name of nearest symbol. */
52+
void *dli_saddr; /* Address of nearest symbol. */
53+
};
54+
55+
56+
/*
57+
* Structures, returned by the RTLD_DI_SERINFO dlinfo() request.
58+
*/
59+
struct Dl_serpath {
60+
char * dls_name; /* single search path entry */
61+
uint dls_flags; /* path information */
62+
};
63+
64+
struct Dl_serinfo {
65+
size_t dls_size; /* total buffer size */
66+
uint dls_cnt; /* number of path entries */
67+
Dl_serpath[1] dls_serpath; /* there may be more than one */
68+
};
69+
70+
/*-
71+
* The actual type declared by this typedef is immaterial, provided that
72+
* it is a function pointer. Its purpose is to provide a return type for
73+
* dlfunc() which can be cast to a function pointer type without depending
74+
* on behavior undefined by the C standard, which might trigger a compiler
75+
* diagnostic. We intentionally declare a unique type signature to force
76+
* a diagnostic should the application not cast the return value of dlfunc()
77+
* appropriately.
78+
*/
79+
struct __dlfunc_arg {
80+
int __dlfunc_dummy;
81+
};
82+
83+
alias dlfunc_t = void function(__dlfunc_arg);
84+
85+
private template __externC(RT, P...)
86+
{
87+
alias __externC = extern(C) RT function(P) nothrow @nogc @system;
88+
}
89+
90+
/* XSI functions first. */
91+
static assert(is(typeof(&dlclose) == __externC!(int, void*)));
92+
static assert(is(typeof(&dlerror) == __externC!(char*)));
93+
static assert(is(typeof(&dlopen) == __externC!(void*, const char*, int)));
94+
static assert(is(typeof(&dlsym) == __externC!(void*, void*, const char*)));
95+
96+
void* fdlopen(int, int);
97+
int dladdr(const(void)*, Dl_info*);
98+
dlfunc_t dlfunc(void*, const(char)*);
99+
int dlinfo(void*, int, void*);
100+
/*void dllockinit(void* _context,
101+
void* function(void* _context) _lock_create,
102+
void function(void* _lock) _rlock_acquire,
103+
void function(void* _lock) _wlock_acquire,
104+
void function(void* _lock) _lock_release,
105+
void function(void* _lock) _lock_destroy,
106+
void function(void* _context) _context_destroy);*/
107+
void* dlvsym(void*, const(char)*, const(char)*);

src/core/sys/dragonflybsd/execinfo.d

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* DragonFlyBSD implementation of glibc's $(LINK2 http://www.gnu.org/software/libc/manual/html_node/Backtraces.html backtrace) facility.
3+
*
4+
* Copyright: Copyright Martin Nowak 2012.
5+
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6+
* Authors: Martin Nowak,Diederik de Groot(port:DragonFlyBSD)
7+
* Copied: From core/sys/freebsd/sys
8+
*/
9+
module core.sys.dragonflybsd.execinfo;
10+
11+
version (DragonFlyBSD):
12+
13+
extern (C) nothrow @system:
14+
15+
import core.sys.dragonflybsd.dlfcn;
16+
17+
// Use extern (D) so that these functions don't collide with libexecinfo.
18+
19+
extern (D) int backtrace(void** buffer, int size)
20+
{
21+
import core.thread : thread_stackBottom;
22+
23+
void** p, pend=cast(void**)thread_stackBottom();
24+
version (D_InlineAsm_X86)
25+
asm nothrow @trusted { mov p[EBP], EBP; }
26+
else version (D_InlineAsm_X86_64)
27+
asm nothrow @trusted { mov p[RBP], RBP; }
28+
else
29+
static assert(false, "Architecture not supported.");
30+
31+
int i;
32+
for (; i < size && p < pend; ++i)
33+
{
34+
buffer[i] = *(p + 1);
35+
auto pnext = cast(void**)*p;
36+
if (pnext <= p) break;
37+
p = pnext;
38+
}
39+
return i;
40+
}
41+
42+
43+
extern (D) char** backtrace_symbols(const(void*)* buffer, int size)
44+
{
45+
static void* realloc(void* p, size_t len) nothrow
46+
{
47+
static import cstdlib=core.stdc.stdlib;
48+
auto res = cstdlib.realloc(p, len);
49+
if (res is null) cstdlib.free(p);
50+
return res;
51+
}
52+
53+
if (size <= 0) return null;
54+
55+
size_t pos = size * (char*).sizeof;
56+
char** p = cast(char**)realloc(null, pos);
57+
if (p is null) return null;
58+
59+
Dl_info info;
60+
foreach (i, addr; buffer[0 .. size])
61+
{
62+
if (dladdr(addr, &info) == 0)
63+
(cast(ubyte*)&info)[0 .. info.sizeof] = 0;
64+
fixupDLInfo(addr, info);
65+
66+
immutable len = formatStackFrame(null, 0, addr, info);
67+
assert(len > 0);
68+
69+
p = cast(char**)realloc(p, pos + len);
70+
if (p is null) return null;
71+
72+
formatStackFrame(cast(char*)p + pos, len, addr, info) == len || assert(0);
73+
74+
p[i] = cast(char*)pos;
75+
pos += len;
76+
}
77+
foreach (i; 0 .. size)
78+
{
79+
pos = cast(size_t)p[i];
80+
p[i] = cast(char*)p + pos;
81+
}
82+
return p;
83+
}
84+
85+
86+
extern (D) void backtrace_symbols_fd(const(void*)* buffer, int size, int fd)
87+
{
88+
import core.sys.posix.unistd : write;
89+
import core.stdc.stdlib : alloca;
90+
91+
if (size <= 0) return;
92+
93+
Dl_info info;
94+
foreach (i, addr; buffer[0 .. size])
95+
{
96+
if (dladdr(addr, &info) == 0)
97+
(cast(ubyte*)&info)[0 .. info.sizeof] = 0;
98+
fixupDLInfo(addr, info);
99+
100+
enum maxAlloca = 1024;
101+
enum min = (size_t a, size_t b) => a <= b ? a : b;
102+
immutable len = min(formatStackFrame(null, 0, addr, info), maxAlloca);
103+
assert(len > 0);
104+
105+
auto p = cast(char*)alloca(len);
106+
if (p is null) return;
107+
108+
formatStackFrame(p, len, addr, info) >= len || assert(0);
109+
p[len - 1] = '\n';
110+
write(fd, p, len);
111+
}
112+
}
113+
114+
115+
private void fixupDLInfo(const(void)* addr, ref Dl_info info)
116+
{
117+
if (info.dli_fname is null) info.dli_fname = "???";
118+
if (info.dli_fbase is null) info.dli_fbase = null;
119+
if (info.dli_sname is null) info.dli_sname = "???";
120+
if (info.dli_saddr is null) info.dli_saddr = cast(void*)addr;
121+
}
122+
123+
124+
private size_t formatStackFrame(char* p, size_t plen, const(void)* addr, const ref Dl_info info)
125+
{
126+
import core.stdc.stdio : snprintf;
127+
128+
immutable off = addr - info.dli_saddr;
129+
immutable len = snprintf(p, plen, "%p <%s+%zd> at %s",
130+
addr, info.dli_sname, off, info.dli_fname);
131+
assert(len > 0);
132+
return cast(size_t)len + 1; // + '\0'
133+
}

0 commit comments

Comments
 (0)