The OpenD Programming Language

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 version (LoongArch64)
94 {
95     // http://sourceware.org/git/?p=glibc.git;a=blob;f=bits/elfclass.h
96     alias __WORDSIZE __ELF_NATIVE_CLASS;
97     alias uint32_t Elf_Symndx;
98 }
99 else version (WebAssembly)
100 {
101     // FIXME: remotely right?
102     alias __WORDSIZE __ELF_NATIVE_CLASS;
103     alias uint32_t Elf_Symndx;
104 }
105 
106 else
107     static assert(0, "unimplemented");
108 // <bits/elfclass.h>
109 
110 template ElfW(string type)
111 {
112     mixin("alias Elf"~__ELF_NATIVE_CLASS.stringof~"_"~type~" ElfW;");
113 }
114 
115 enum
116 {
117     RT_CONSISTENT,
118     RT_ADD,
119     RT_DELETE,
120 }
121 
122 struct r_debug
123 {
124     int r_version;
125     link_map* r_map;
126     ElfW!"Addr" r_brk;
127     typeof(RT_CONSISTENT) r_state;
128     ElfW!"Addr" r_ldbase;
129 }
130 
131 extern r_debug _r_debug;
132 extern ElfW!"Dyn"* _DYNAMIC;
133 
134 struct link_map
135 {
136     ElfW!"Addr" l_addr;
137     char* l_name;
138     ElfW!"Dyn"* l_ld;
139     link_map* l_next, l_prev;
140 }
141 
142 enum
143 {
144     LA_ACT_CONSISTENT,
145     LA_ACT_ADD,
146     LA_ACT_DELETE,
147 }
148 
149 enum
150 {
151     LA_SER_ORIG = 0x01,
152     LA_SER_LIBPATH = 0x02,
153     LA_SER_RUNPATH = 0x04,
154     LA_SER_CONFIG = 0x08,
155     LA_SER_DEFAULT = 0x40,
156     LA_SER_SECURE = 0x80,
157 }
158 
159 
160 enum
161 {
162     LA_FLG_BINDTO = 0x01,
163     LA_FLG_BINDFROM = 0x02,
164 }
165 
166 
167 enum
168 {
169     LA_SYMB_NOPLTENTER = 0x01,
170     LA_SYMB_NOPLTEXIT = 0x02,
171     LA_SYMB_STRUCTCALL = 0x04,
172     LA_SYMB_DLSYM = 0x08,
173     LA_SYMB_ALTVALUE = 0x10,
174 }
175 
176 struct dl_phdr_info
177 {
178     ElfW!"Addr" dlpi_addr;
179     const(char)* dlpi_name;
180     const(ElfW!"Phdr")* dlpi_phdr;
181     ElfW!"Half" dlpi_phnum;
182 
183     // check the SIZE argument of the dl_iterate_phdr callback whether
184     // the following members are available
185     ulong dlpi_adds;
186     ulong dlpi_subs;
187 
188     size_t dlpi_tls_modid;
189     void *dlpi_tls_data;
190 }
191 
192 private alias extern(C) int function(dl_phdr_info*, size_t, void *) dl_iterate_phdr_cb;
193 private alias extern(C) int function(dl_phdr_info*, size_t, void *) @nogc dl_iterate_phdr_cb_ngc;
194 extern int dl_iterate_phdr(dl_iterate_phdr_cb __callback, void*__data);
195 extern int dl_iterate_phdr(dl_iterate_phdr_cb_ngc __callback, void*__data) @nogc;
196 
197 // ld.so auditing interfaces prototypes have to be defined by the auditing DSO.
198 extern uint la_version(uint __version);
199 extern void la_activity(uintptr_t *__cookie, uint __flag);
200 extern char* la_objsearch(const(char)* __name, uintptr_t* __cookie,
201                           uint __flag);
202 extern uint la_objopen(link_map* __map, Lmid_t __lmid,
203                        uintptr_t* __cookie);
204 extern void la_preinit(uintptr_t* __cookie);
205 extern uintptr_t la_symbind32(Elf32_Sym* __sym, uint __ndx,
206                               uintptr_t* __refcook, uintptr_t* __defcook,
207                               uint *__flags, const(char)* __symname);
208 extern uintptr_t la_symbind64(Elf64_Sym* __sym, uint __ndx,
209                               uintptr_t* __refcook, uintptr_t* __defcook,
210                               uint* __flags, const(char)* __symname);
211 extern uint la_objclose(uintptr_t *__cookie);