@@ -24,6 +24,59 @@ version(linux) public import core.sys.linux.elf;
2424version (FreeBSD ) public  import  core.sys.freebsd.sys.elf ;
2525version (DragonFlyBSD ) public  import  core.sys.dragonflybsd.sys.elf ;
2626
27+ const (void )[] getAbsoluteAddressRangeForExecutable () @nogc  nothrow 
28+ {
29+     import  core.sys.posix.unistd  : readlink, getpid;
30+     import  core.stdc.stdio , core.stdc.stdlib , core.stdc.string ;
31+ 
32+     //  get absolute path to executable
33+     char [1024 ] selfPath = void ;
34+     version  (FreeBSD )
35+     {
36+         getFreeBSDExePath(selfPath[]);
37+     }
38+     else 
39+     {
40+         version  (linux )
41+         {
42+             auto  selfLink = " /proc/self/exe" 
43+         }
44+         else version  (DragonFlyBSD )
45+         {
46+             auto  selfLink = " /proc/curproc/file" 
47+         }
48+ 
49+         const  length = readlink(selfLink, selfPath.ptr, selfPath.length);
50+         assert (length >  0  &&  length <  selfPath.length);
51+         selfPath[length] = 0 ;
52+     }
53+ 
54+     //  open the current process' maps file
55+     char [1024 ] selfMapsPath = void ;
56+     snprintf(selfMapsPath.ptr, selfMapsPath.length, " /proc/%d/maps" 
57+     FILE *  fp = fopen(selfMapsPath.ptr, " r" 
58+     assert (fp);
59+     scope (exit) fclose (fp);
60+ 
61+     //  use the address range of the first line for the executable file
62+     char [128 ] line = void ;
63+     while  (fgets(line.ptr, line.length, fp) ! is  null )
64+     {
65+         line[strlen(line.ptr) -  1 ] = ' \0 ' //  remove trailing '\n'
66+         char *  path = strchr(line.ptr, ' /' 
67+         if  (path &&  strcmp(selfPath.ptr, path) ==  0 )
68+         {
69+             char *  tail;
70+             const  start = cast (void * ) strtoul(line.ptr, &tail, 16 );
71+             ++ tail; //  skip over '-'
72+             const  end = cast (void * ) strtoul(tail, &tail, 16 );
73+             return  start[0  ..  end -  start];
74+         }
75+     }
76+ 
77+     assert (0 );
78+ }
79+ 
2780struct  ElfFile 
2881{
2982    static  bool  openSelf (ElfFile*  file) @nogc  nothrow 
0 commit comments