1 /**
2  * D header file for GNU/Linux
3  *
4  * $(LINK2 http://sourceware.org/git/?p=glibc.git;a=blob;f=elf/link.h, glibc elf/link.h)
5  */
6 module core.sys.linux.link;
7 
8 version (linux):
9 extern (C):
10 nothrow:
11 
12 version (ARM)     version = ARM_Any;
13 version (AArch64) version = ARM_Any;
14 version (HPPA)    version = HPPA_Any;
15 version (MIPS32)  version = MIPS_Any;
16 version (MIPS64)  version = MIPS_Any;
17 version (PPC)     version = PPC_Any;
18 version (PPC64)   version = PPC_Any;
19 version (RISCV32) version = RISCV_Any;
20 version (RISCV64) version = RISCV_Any;
21 version (S390)    version = IBMZ_Any;
22 version (SPARC)   version = SPARC_Any;
23 version (SPARC64) version = SPARC_Any;
24 version (SystemZ) version = IBMZ_Any;
25 version (X86)     version = X86_Any;
26 version (X86_64)  version = X86_Any;
27 
28 import core.stdc.stdint : uintptr_t, uint32_t, uint64_t;
29 import core.sys.linux.config : __WORDSIZE;
30 import core.sys.linux.dlfcn : Lmid_t;
31 import core.sys.linux.elf;
32 
33 // <bits/elfclass.h>
34 version (Android)
35 {
36     alias __WORDSIZE __ELF_NATIVE_CLASS;
37     version (D_LP64)
38         alias uint64_t Elf_Symndx;
39     else
40         alias uint32_t Elf_Symndx;
41 }
42 else version (X86_Any)
43 {
44     // http://sourceware.org/git/?p=glibc.git;a=blob;f=bits/elfclass.h
45     alias __WORDSIZE __ELF_NATIVE_CLASS;
46     alias uint32_t Elf_Symndx;
47 }
48 else version (HPPA_Any)
49 {
50     // http://sourceware.org/git/?p=glibc.git;a=blob;f=bits/elfclass.h
51     alias __WORDSIZE __ELF_NATIVE_CLASS;
52     alias uint32_t Elf_Symndx;
53 }
54 else version (MIPS_Any)
55 {
56     // http://sourceware.org/git/?p=glibc.git;a=blob;f=bits/elfclass.h
57     alias __WORDSIZE __ELF_NATIVE_CLASS;
58     alias uint32_t Elf_Symndx;
59 }
60 else version (PPC_Any)
61 {
62     // http://sourceware.org/git/?p=glibc.git;a=blob;f=bits/elfclass.h
63     alias __WORDSIZE __ELF_NATIVE_CLASS;
64     alias uint32_t Elf_Symndx;
65 }
66 else version (ARM_Any)
67 {
68     // http://sourceware.org/git/?p=glibc.git;a=blob;f=bits/elfclass.h
69     alias __WORDSIZE __ELF_NATIVE_CLASS;
70     alias uint32_t Elf_Symndx;
71 }
72 else version (RISCV_Any)
73 {
74     // http://sourceware.org/git/?p=glibc.git;a=blob;f=bits/elfclass.h
75     alias __WORDSIZE __ELF_NATIVE_CLASS;
76     alias uint32_t Elf_Symndx;
77 }
78 else version (SPARC_Any)
79 {
80     // http://sourceware.org/git/?p=glibc.git;a=blob;f=bits/elfclass.h
81     alias __WORDSIZE __ELF_NATIVE_CLASS;
82     alias uint32_t Elf_Symndx;
83 }
84 else version (IBMZ_Any)
85 {
86     // http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/s390/bits/elfclass.h
87     alias __WORDSIZE __ELF_NATIVE_CLASS;
88     static if (__WORDSIZE == 64)
89         alias uint64_t Elf_Symndx;
90     else
91         alias uint32_t Elf_Symndx;
92 }
93 else
94     static assert(0, "unimplemented");
95 // <bits/elfclass.h>
96 
97 template ElfW(string type)
98 {
99     mixin("alias Elf"~__ELF_NATIVE_CLASS.stringof~"_"~type~" ElfW;");
100 }
101 
102 enum
103 {
104     RT_CONSISTENT,
105     RT_ADD,
106     RT_DELETE,
107 }
108 
109 struct r_debug
110 {
111     int r_version;
112     link_map* r_map;
113     ElfW!"Addr" r_brk;
114     typeof(RT_CONSISTENT) r_state;
115     ElfW!"Addr" r_ldbase;
116 }
117 
118 extern r_debug _r_debug;
119 extern ElfW!"Dyn"* _DYNAMIC;
120 
121 struct link_map
122 {
123     ElfW!"Addr" l_addr;
124     char* l_name;
125     ElfW!"Dyn"* l_ld;
126     link_map* l_next, l_prev;
127 }
128 
129 enum
130 {
131     LA_ACT_CONSISTENT,
132     LA_ACT_ADD,
133     LA_ACT_DELETE,
134 }
135 
136 enum
137 {
138     LA_SER_ORIG = 0x01,
139     LA_SER_LIBPATH = 0x02,
140     LA_SER_RUNPATH = 0x04,
141     LA_SER_CONFIG = 0x08,
142     LA_SER_DEFAULT = 0x40,
143     LA_SER_SECURE = 0x80,
144 }
145 
146 
147 enum
148 {
149     LA_FLG_BINDTO = 0x01,
150     LA_FLG_BINDFROM = 0x02,
151 }
152 
153 
154 enum
155 {
156     LA_SYMB_NOPLTENTER = 0x01,
157     LA_SYMB_NOPLTEXIT = 0x02,
158     LA_SYMB_STRUCTCALL = 0x04,
159     LA_SYMB_DLSYM = 0x08,
160     LA_SYMB_ALTVALUE = 0x10,
161 }
162 
163 struct dl_phdr_info
164 {
165     ElfW!"Addr" dlpi_addr;
166     const(char)* dlpi_name;
167     const(ElfW!"Phdr")* dlpi_phdr;
168     ElfW!"Half" dlpi_phnum;
169 
170     // check the SIZE argument of the dl_iterate_phdr callback whether
171     // the following members are available
172     ulong dlpi_adds;
173     ulong dlpi_subs;
174 
175     size_t dlpi_tls_modid;
176     void *dlpi_tls_data;
177 }
178 
179 private alias extern(C) int function(dl_phdr_info*, size_t, void *) dl_iterate_phdr_cb;
180 private alias extern(C) int function(dl_phdr_info*, size_t, void *) @nogc dl_iterate_phdr_cb_ngc;
181 extern int dl_iterate_phdr(dl_iterate_phdr_cb __callback, void*__data);
182 extern int dl_iterate_phdr(dl_iterate_phdr_cb_ngc __callback, void*__data) @nogc;
183 
184 // ld.so auditing interfaces prototypes have to be defined by the auditing DSO.
185 extern uint la_version(uint __version);
186 extern void la_activity(uintptr_t *__cookie, uint __flag);
187 extern char* la_objsearch(const(char)* __name, uintptr_t* __cookie,
188                           uint __flag);
189 extern uint la_objopen(link_map* __map, Lmid_t __lmid,
190                        uintptr_t* __cookie);
191 extern void la_preinit(uintptr_t* __cookie);
192 extern uintptr_t la_symbind32(Elf32_Sym* __sym, uint __ndx,
193                               uintptr_t* __refcook, uintptr_t* __defcook,
194                               uint *__flags, const(char)* __symname);
195 extern uintptr_t la_symbind64(Elf64_Sym* __sym, uint __ndx,
196                               uintptr_t* __refcook, uintptr_t* __defcook,
197                               uint* __flags, const(char)* __symname);
198 extern uint la_objclose(uintptr_t *__cookie);