1 /**
2  * D header file for $(LINK2 https://opensource.apple.com/source/cctools/cctools-895/include/mach-o/getsect.h.auto.html, mach-o/getsect.h).
3  *
4  * Copyright: Copyright Digital Mars 2010-2018.
5  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6  * Authors:   Jacob Carlborg
7  * Version: Initial created: Mar 16, 2010
8  * Source: $(DRUNTIMESRC core/sys/darwin/mach/_getsect.d)
9  */
10 module core.sys.darwin.mach.getsect;
11 
12 extern (C):
13 nothrow:
14 @nogc:
15 
16 version (CoreDdoc)
17 {
18     import core.stdc.config : c_ulong;
19 
20     /**
21      * In reality this will be $(REF mach_header, core, sys, darwin, mach, loader)
22      * on 32-bit platforms and $(REF mach_header_64, core, sys, darwin, mach, loader)
23      * 64-bit platforms.
24      */
25     struct MachHeader;
26 
27     /**
28      * In reality this will be $(REF segment_command, core, sys, darwin, mach, loader)
29      * on 32-bit platforms and $(REF segment_command_64, core, sys, darwin, mach, loader)
30      * 64-bit platforms.
31      */
32     struct SegmentCommand;
33 
34     /**
35      * In reality this will be $(REF section, core, sys, darwin, mach, loader)
36      * on 32-bit platforms and $(REF section_64, core, sys, darwin, mach, loader)
37      * 64-bit platforms.
38      */
39     struct Section;
40 
41     /**
42      * Returns the section data of section with the given section name.
43      *
44      * Returns the section data of the given section in the given segment in the
45      * mach executable it is linked into.
46      *
47      * ---
48      * void main()
49      * {
50      *      import core.sys.darwin.mach.getsect;
51      *      int size;
52      *      assert(getsectdata("__TEXT", "__text", &size));
53      *      assert(size > 0);
54      * }
55      * ---
56      *
57      * Params:
58      *  segname = the name of the segment
59      *  sectname = the name of the section
60      *  size = this will be set to the size of the section or 0 if the section
61      *      doesn't exist
62      *
63      * Returns: a pointer to the section data or `null` if it doesn't exist
64      */
65     char* getsectdata(
66         const scope char* segname,
67         const scope char* sectname,
68         c_ulong *size
69     );
70 
71     /**
72      * Returns the section data of section with the given section name.
73      *
74      * Returns the section data of the given section in the given segment in the
75      * given framework.
76      *
77      * ---
78      * void main()
79      * {
80      *      import core.sys.darwin.mach.getsect;
81      *      int size;
82      *      assert(getsectdatafromFramework("Foundation", "__TEXT", "__text", &size));
83      *      assert(size > 0);
84      * }
85      * ---
86      *
87      * Params:
88      *  FrameworkName = the name of the framework to get the section data from
89      *  segname = the name of the segment
90      *  sectname = the name of the section
91      *  size = this will be set to the size of the section or 0 if the section
92      *      doesn't exist
93      *
94      * Returns: a pointer to the section data or `null` if it doesn't exist
95      */
96     char* getsectdatafromFramework(
97         const scope char* FrameworkName,
98         const scope char* segname,
99         const scope char* sectname,
100         c_ulong* size
101     );
102 
103     ///
104     c_ulong get_end();
105 
106     ///
107     c_ulong get_etext();
108 
109     ///
110     c_ulong get_edata();
111 
112     /**
113      * Returns the section with the given section name.
114      *
115      * Returns the section structure of the given section in the given segment
116      * in the mach executable it is linked into.
117      *
118      * ---
119      * void main()
120      * {
121      *      import core.sys.darwin.mach.getsect;
122      *      assert(getsectbyname("__TEXT", "__text"));
123      * }
124      * ---
125      *
126      * Params:
127      *  segname = the name of the segment
128      *  sectname = the name of the section
129      *
130      * Returns: a pointer to the section structure or `null` if it doesn't exist
131      */
132     const(Section)* getsectbyname(
133         const scope char* segname,
134         const scope char* sectname
135     );
136 
137     /**
138      * Returns the section data of section with the given section name.
139      *
140      * Returns the section data of the given section in the given segment in the
141      * image pointed to by the given mach header.
142      *
143      * ---
144      * void main()
145      * {
146      *      import core.sys.darwin.mach.getsect;
147      *      import core.sys.darwin.crt_externs;
148      *
149      *      auto mph = _NSGetMachExecuteHeader();
150      *      int size;
151      *      assert(getsectiondata(mph, "__TEXT", "__text", &size));
152      *      assert(size > 0);
153      * }
154      * ---
155      *
156      * Params:
157      *  mhp = the mach header to get the section data from
158      *  segname = the name of the segment
159      *  sectname = the name of the section
160      *  size = this will be set to the size of the section or 0 if the section
161      *      doesn't exist
162      *
163      * Returns: a pointer to the section data or `null` if it doesn't exist
164      */
165     ubyte* getsectiondata(
166         const scope MachHeader* mhp,
167         const scope char* segname,
168         const scope char* sectname,
169         c_ulong* size
170     );
171 
172     /**
173      * Returns the segment with the given segment name.
174      *
175      * Returns the segment structure of the given segment in the mach executable
176      * it is linked into.
177      *
178      * ---
179      * void main()
180      * {
181      *      import core.sys.darwin.mach.getsect;
182      *      assert(getsegbyname("__TEXT"));
183      * }
184      * ---
185      *
186      * Params:
187      *  segname = the name of the segment
188      *
189      * Returns: a pointer to the section structure or `null` if it doesn't exist
190      */
191     const(SegmentCommand)* getsegbyname(
192         const scope char* segname
193     );
194 
195     /**
196      * Returns the segment data of segment with the given segment name.
197      *
198      * Returns the segment data of the given segment in the image pointed to by
199      * the given mach header.
200      *
201      * ---
202      * void main()
203      * {
204      *      import core.sys.darwin.mach.getsect;
205      *      import core.sys.darwin.crt_externs;
206      *
207      *      auto mph = _NSGetMachExecuteHeader();
208      *      int size;
209      *      assert(getsegmentdata(mph, "__TEXT", &size));
210      *      assert(size > 0);
211      * }
212      * ---
213      *
214      * Params:
215      *  mhp = the mach header to get the section data from
216      *  segname = the name of the segment
217      *  size = this will be set to the size of the section or 0 if the section
218      *      doesn't exist
219      *
220      * Returns: a pointer to the section data or `null` if it doesn't exist
221      */
222     ubyte* getsegmentdata(
223         const scope MachHeader* mhp,
224         const scope char* segname,
225         c_ulong* size
226     );
227 
228     struct mach_header;
229     struct mach_header_64;
230     struct section;
231     struct section_64;
232 
233     /**
234      * Returns the section data of section with the given section name.
235      *
236      * Returns the section data of the given section in the given segment in the
237      * image pointed to by the given mach header.
238      *
239      * ---
240      * void main()
241      * {
242      *      import core.sys.darwin.mach.getsect;
243      *      import core.sys.darwin.crt_externs;
244      *
245      *      auto mph = _NSGetMachExecuteHeader();
246      *      int size;
247      *      assert(getsectdatafromheader(mph, "__TEXT", "__text", &size));
248      *      assert(size > 0);
249      * }
250      * ---
251      *
252      * Params:
253      *  mhp = the mach header to get the section data from
254      *  segname = the name of the segment
255      *  sectname = the name of the section
256      *  size = this will be set to the size of the section or 0 if the section
257      *      doesn't exist
258      *
259      * Returns: a pointer to the section data or `null` if it doesn't exist
260      */
261     ubyte* getsectdatafromheader(
262         const scope mach_header* mhp,
263         const scope char* segname,
264         const scope char* sectname,
265         c_ulong* size
266     );
267 
268     /// ditto
269     ubyte* getsectdatafromheader_64(
270         const scope mach_header_64* mhp,
271         const scope char* segname,
272         const scope char* sectname,
273         c_ulong* size
274     );
275 
276 
277     /**
278      * Returns the section with the given section name.
279      *
280      * Returns the section structure of the given section in the given segment
281      * in image pointed to by the given mach header.
282      *
283      * ---
284      * void main()
285      * {
286      *      import core.sys.darwin.mach.getsect;
287      *      import core.sys.darwin.crt_externs;
288      *
289      *      auto mph = _NSGetMachExecuteHeader();
290      *      assert(getsectbynamefromheader(mph, "__TEXT", "__text"));
291      * }
292      * ---
293      *
294      * Params:
295      *  mhp = the mach header to get the section from
296      *  segname = the name of the segment
297      *  sectname = the name of the section
298      *
299      * Returns: a pointer to the section structure or `null` if it doesn't exist
300      */
301     const(section)* getsectbynamefromheader(
302         const scope mach_header* mhp,
303         const scope char* segname,
304         const scope char* sectname
305     );
306 
307     /// ditto
308     const(section_64)* getsectbynamefromheader_64(
309         const scope mach_header_64* mhp,
310         const scope char* segname,
311         const scope char* sectname
312     );
313 
314     /**
315      * Returns the section with the given section name.
316      *
317      * Returns the section structure of the given section in the given segment
318      * in image pointed to by the given mach header.
319      *
320      * Params:
321      *  mhp = the mach header to get the section from
322      *  segname = the name of the segment
323      *  section = the name of the section
324      *  fSwap = ?
325      *
326      * Returns: a pointer to the section structure or `null` if it doesn't exist
327      */
328     const(section)* getsectbynamefromheaderwithswap(
329         const scope mach_header* mhp,
330         const scope char* segname,
331         const scope char* section,
332         int fSwap
333     );
334 
335     /// ditto
336     const(section)* getsectbynamefromheaderwithswap_64(
337         const scope mach_header_64* mhp,
338         const scope char* segname,
339         const scope char* section,
340         int fSwap
341     );
342 }
343 
344 else version (OSX)
345     version = Darwin;
346 else version (iOS)
347     version = Darwin;
348 else version (TVOS)
349     version = Darwin;
350 else version (WatchOS)
351     version = Darwin;
352 
353 version (Darwin):
354 
355 public import core.sys.darwin.mach.loader;
356 
357 import core.stdc.config : c_ulong;
358 
359 char* getsectdata(
360     const scope char* segname,
361     const scope char* sectname,
362     c_ulong *size
363 );
364 
365 char* getsectdatafromFramework(
366     const scope char* FrameworkName,
367     const scope char* segname,
368     const scope char* sectname,
369     c_ulong* size
370 );
371 
372 c_ulong get_end();
373 c_ulong get_etext();
374 c_ulong get_edata();
375 
376 // Runtime interfaces for 64-bit Mach-O programs.
377 version (D_LP64)
378 {
379     const(section_64)* getsectbyname(
380         const scope char* segname,
381         const scope char* sectname
382     );
383 
384     ubyte* getsectiondata(
385         const scope mach_header_64* mhp,
386         const scope char* segname,
387         const scope char* sectname,
388         c_ulong* size
389     );
390 
391     const(segment_command_64)* getsegbyname(
392         const scope char* segname
393     );
394 
395     ubyte* getsegmentdata(
396         const scope mach_header_64* mhp,
397         const scope char* segname,
398         c_ulong* size
399     );
400 }
401 
402 // Runtime interfaces for 32-bit Mach-O programs.
403 else
404 {
405     const(section)* getsectbyname(
406         const scope char* segname,
407         const scope char* sectname
408     );
409 
410     ubyte* getsectiondata(
411         const scope mach_header* mhp,
412         const scope char* segname,
413         const scope char* sectname,
414         c_ulong* size
415     );
416 
417     const(segment_command)* getsegbyname(
418         const scope char* segname
419     );
420 
421     ubyte* getsegmentdata(
422         const scope mach_header* mhp,
423         const scope char* segname,
424         c_ulong* size
425     );
426 }
427 
428 // Interfaces for tools working with 32-bit Mach-O files.
429 
430 ubyte* getsectdatafromheader(
431     const scope mach_header* mhp,
432     const scope char* segname,
433     const scope char* sectname,
434     c_ulong* size
435 );
436 
437 const(section)* getsectbynamefromheader(
438     const scope mach_header* mhp,
439     const scope char* segname,
440     const scope char* sectname
441 );
442 
443 const(section)* getsectbynamefromheaderwithswap(
444     const scope mach_header* mhp,
445     const scope char* segname,
446     const scope char* section,
447     int fSwap
448 );
449 
450 // Interfaces for tools working with 64-bit Mach-O files.
451 
452 ubyte* getsectdatafromheader_64(
453     const scope mach_header_64* mhp,
454     const scope char* segname,
455     const scope char* sectname,
456     c_ulong* size
457 );
458 
459 const(section_64)* getsectbynamefromheader_64(
460     const scope mach_header_64* mhp,
461     const scope char* segname,
462     const scope char* sectname
463 );
464 
465 const(section)* getsectbynamefromheaderwithswap_64(
466     const scope mach_header_64* mhp,
467     const scope char* segname,
468     const scope char* section,
469     int fSwap
470 );