summaryrefslogtreecommitdiff
path: root/libphobos/libdruntime/core/sys/darwin/mach/getsect.d
blob: b6e10a81b00f9a9a82ff3da0bfe408446a1d6cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
 * D header file for $(LINK2 https://opensource.apple.com/source/cctools/cctools-895/include/mach-o/getsect.h.auto.html, mach-o/getsect.h).
 *
 * Copyright: Copyright Digital Mars 2010-2018.
 * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
 * Authors:   Jacob Carlborg
 * Version: Initial created: Mar 16, 2010
 * Source: $(DRUNTIMESRC core/sys/darwin/mach/_getsect.d)
 */
module core.sys.darwin.mach.getsect;

extern (C):
nothrow:
@nogc:

version (CoreDdoc)
{
    import core.stdc.config : c_ulong;

    /**
     * In reality this will be $(REF mach_header, core, sys, darwin, mach, loader)
     * on 32-bit platforms and $(REF mach_header_64, core, sys, darwin, mach, loader)
     * 64-bit platforms.
     */
    struct MachHeader;

    /**
     * In reality this will be $(REF segment_command, core, sys, darwin, mach, loader)
     * on 32-bit platforms and $(REF segment_command_64, core, sys, darwin, mach, loader)
     * 64-bit platforms.
     */
    struct SegmentCommand;

    /**
     * In reality this will be $(REF section, core, sys, darwin, mach, loader)
     * on 32-bit platforms and $(REF section_64, core, sys, darwin, mach, loader)
     * 64-bit platforms.
     */
    struct Section;

    /**
     * Returns the section data of section with the given section name.
     *
     * Returns the section data of the given section in the given segment in the
     * mach executable it is linked into.
     *
     * ---
     * void main()
     * {
     *      import core.sys.darwin.mach.getsect;
     *      int size;
     *      assert(getsectdata("__TEXT", "__text", &size));
     *      assert(size > 0);
     * }
     * ---
     *
     * Params:
     *  segname = the name of the segment
     *  sectname = the name of the section
     *  size = this will be set to the size of the section or 0 if the section
     *      doesn't exist
     *
     * Returns: a pointer to the section data or `null` if it doesn't exist
     */
    char* getsectdata(
        const scope char* segname,
        const scope char* sectname,
        c_ulong *size
    );

    /**
     * Returns the section data of section with the given section name.
     *
     * Returns the section data of the given section in the given segment in the
     * given framework.
     *
     * ---
     * void main()
     * {
     *      import core.sys.darwin.mach.getsect;
     *      int size;
     *      assert(getsectdatafromFramework("Foundation", "__TEXT", "__text", &size));
     *      assert(size > 0);
     * }
     * ---
     *
     * Params:
     *  FrameworkName = the name of the framework to get the section data from
     *  segname = the name of the segment
     *  sectname = the name of the section
     *  size = this will be set to the size of the section or 0 if the section
     *      doesn't exist
     *
     * Returns: a pointer to the section data or `null` if it doesn't exist
     */
    char* getsectdatafromFramework(
        const scope char* FrameworkName,
        const scope char* segname,
        const scope char* sectname,
        c_ulong* size
    );

    ///
    c_ulong get_end();

    ///
    c_ulong get_etext();

    ///
    c_ulong get_edata();

    /**
     * Returns the section with the given section name.
     *
     * Returns the section structure of the given section in the given segment
     * in the mach executable it is linked into.
     *
     * ---
     * void main()
     * {
     *      import core.sys.darwin.mach.getsect;
     *      assert(getsectbyname("__TEXT", "__text"));
     * }
     * ---
     *
     * Params:
     *  segname = the name of the segment
     *  sectname = the name of the section
     *
     * Returns: a pointer to the section structure or `null` if it doesn't exist
     */
    const(Section)* getsectbyname(
        const scope char* segname,
        const scope char* sectname
    );

    /**
     * Returns the section data of section with the given section name.
     *
     * Returns the section data of the given section in the given segment in the
     * image pointed to by the given mach header.
     *
     * ---
     * void main()
     * {
     *      import core.sys.darwin.mach.getsect;
     *      import core.sys.darwin.crt_externs;
     *
     *      auto mph = _NSGetMachExecuteHeader();
     *      int size;
     *      assert(getsectiondata(mph, "__TEXT", "__text", &size));
     *      assert(size > 0);
     * }
     * ---
     *
     * Params:
     *  mhp = the mach header to get the section data from
     *  segname = the name of the segment
     *  sectname = the name of the section
     *  size = this will be set to the size of the section or 0 if the section
     *      doesn't exist
     *
     * Returns: a pointer to the section data or `null` if it doesn't exist
     */
    ubyte* getsectiondata(
        const scope MachHeader* mhp,
        const scope char* segname,
        const scope char* sectname,
        c_ulong* size
    );

    /**
     * Returns the segment with the given segment name.
     *
     * Returns the segment structure of the given segment in the mach executable
     * it is linked into.
     *
     * ---
     * void main()
     * {
     *      import core.sys.darwin.mach.getsect;
     *      assert(getsegbyname("__TEXT"));
     * }
     * ---
     *
     * Params:
     *  segname = the name of the segment
     *
     * Returns: a pointer to the section structure or `null` if it doesn't exist
     */
    const(SegmentCommand)* getsegbyname(
        const scope char* segname
    );

    /**
     * Returns the segment data of segment with the given segment name.
     *
     * Returns the segment data of the given segment in the image pointed to by
     * the given mach header.
     *
     * ---
     * void main()
     * {
     *      import core.sys.darwin.mach.getsect;
     *      import core.sys.darwin.crt_externs;
     *
     *      auto mph = _NSGetMachExecuteHeader();
     *      int size;
     *      assert(getsegmentdata(mph, "__TEXT", &size));
     *      assert(size > 0);
     * }
     * ---
     *
     * Params:
     *  mhp = the mach header to get the section data from
     *  segname = the name of the segment
     *  size = this will be set to the size of the section or 0 if the section
     *      doesn't exist
     *
     * Returns: a pointer to the section data or `null` if it doesn't exist
     */
    ubyte* getsegmentdata(
        const scope MachHeader* mhp,
        const scope char* segname,
        c_ulong* size
    );

    struct mach_header;
    struct mach_header_64;
    struct section;
    struct section_64;

    /**
     * Returns the section data of section with the given section name.
     *
     * Returns the section data of the given section in the given segment in the
     * image pointed to by the given mach header.
     *
     * ---
     * void main()
     * {
     *      import core.sys.darwin.mach.getsect;
     *      import core.sys.darwin.crt_externs;
     *
     *      auto mph = _NSGetMachExecuteHeader();
     *      int size;
     *      assert(getsectdatafromheader(mph, "__TEXT", "__text", &size));
     *      assert(size > 0);
     * }
     * ---
     *
     * Params:
     *  mhp = the mach header to get the section data from
     *  segname = the name of the segment
     *  sectname = the name of the section
     *  size = this will be set to the size of the section or 0 if the section
     *      doesn't exist
     *
     * Returns: a pointer to the section data or `null` if it doesn't exist
     */
    ubyte* getsectdatafromheader(
        const scope mach_header* mhp,
        const scope char* segname,
        const scope char* sectname,
        c_ulong* size
    );

    /// ditto
    ubyte* getsectdatafromheader_64(
        const scope mach_header_64* mhp,
        const scope char* segname,
        const scope char* sectname,
        c_ulong* size
    );


    /**
     * Returns the section with the given section name.
     *
     * Returns the section structure of the given section in the given segment
     * in image pointed to by the given mach header.
     *
     * ---
     * void main()
     * {
     *      import core.sys.darwin.mach.getsect;
     *      import core.sys.darwin.crt_externs;
     *
     *      auto mph = _NSGetMachExecuteHeader();
     *      assert(getsectbynamefromheader(mph, "__TEXT", "__text"));
     * }
     * ---
     *
     * Params:
     *  mhp = the mach header to get the section from
     *  segname = the name of the segment
     *  sectname = the name of the section
     *
     * Returns: a pointer to the section structure or `null` if it doesn't exist
     */
    const(section)* getsectbynamefromheader(
        const scope mach_header* mhp,
        const scope char* segname,
        const scope char* sectname
    );

    /// ditto
    const(section_64)* getsectbynamefromheader_64(
        const scope mach_header_64* mhp,
        const scope char* segname,
        const scope char* sectname
    );

    /**
     * Returns the section with the given section name.
     *
     * Returns the section structure of the given section in the given segment
     * in image pointed to by the given mach header.
     *
     * Params:
     *  mhp = the mach header to get the section from
     *  segname = the name of the segment
     *  section = the name of the section
     *  fSwap = ?
     *
     * Returns: a pointer to the section structure or `null` if it doesn't exist
     */
    const(section)* getsectbynamefromheaderwithswap(
        const scope mach_header* mhp,
        const scope char* segname,
        const scope char* section,
        int fSwap
    );

    /// ditto
    const(section)* getsectbynamefromheaderwithswap_64(
        const scope mach_header_64* mhp,
        const scope char* segname,
        const scope char* section,
        int fSwap
    );
}

else version (OSX)
    version = Darwin;
else version (iOS)
    version = Darwin;
else version (TVOS)
    version = Darwin;
else version (WatchOS)
    version = Darwin;

version (Darwin):

public import core.sys.darwin.mach.loader;

import core.stdc.config : c_ulong;

char* getsectdata(
    const scope char* segname,
    const scope char* sectname,
    c_ulong *size
);

char* getsectdatafromFramework(
    const scope char* FrameworkName,
    const scope char* segname,
    const scope char* sectname,
    c_ulong* size
);

c_ulong get_end();
c_ulong get_etext();
c_ulong get_edata();

// Runtime interfaces for 64-bit Mach-O programs.
version (D_LP64)
{
    const(section_64)* getsectbyname(
        const scope char* segname,
        const scope char* sectname
    );

    ubyte* getsectiondata(
        const scope mach_header_64* mhp,
        const scope char* segname,
        const scope char* sectname,
        c_ulong* size
    );

    const(segment_command_64)* getsegbyname(
        const scope char* segname
    );

    ubyte* getsegmentdata(
        const scope mach_header_64* mhp,
        const scope char* segname,
        c_ulong* size
    );
}

// Runtime interfaces for 32-bit Mach-O programs.
else
{
    const(section)* getsectbyname(
        const scope char* segname,
        const scope char* sectname
    );

    ubyte* getsectiondata(
        const scope mach_header* mhp,
        const scope char* segname,
        const scope char* sectname,
        c_ulong* size
    );

    const(segment_command)* getsegbyname(
        const scope char* segname
    );

    ubyte* getsegmentdata(
        const scope mach_header* mhp,
        const scope char* segname,
        c_ulong* size
    );
}

// Interfaces for tools working with 32-bit Mach-O files.

ubyte* getsectdatafromheader(
    const scope mach_header* mhp,
    const scope char* segname,
    const scope char* sectname,
    c_ulong* size
);

const(section)* getsectbynamefromheader(
    const scope mach_header* mhp,
    const scope char* segname,
    const scope char* sectname
);

const(section)* getsectbynamefromheaderwithswap(
    const scope mach_header* mhp,
    const scope char* segname,
    const scope char* section,
    int fSwap
);

// Interfaces for tools working with 64-bit Mach-O files.

ubyte* getsectdatafromheader_64(
    const scope mach_header_64* mhp,
    const scope char* segname,
    const scope char* sectname,
    c_ulong* size
);

const(section_64)* getsectbynamefromheader_64(
    const scope mach_header_64* mhp,
    const scope char* segname,
    const scope char* sectname
);

const(section)* getsectbynamefromheaderwithswap_64(
    const scope mach_header_64* mhp,
    const scope char* segname,
    const scope char* section,
    int fSwap
);