This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
image.d
148 lines (114 loc) · 3.35 KB
/
image.d
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
/**
* Copyright: Copyright Digital Mars 2010.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Jacob Carlborg
* Version: Initial created: Feb 23, 2010
*/
/* Copyright Digital Mars 2010.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module rt.image;
version (OSX):
import src.core.sys.osx.mach.dyld;
import src.core.sys.osx.mach.loader;
import src.core.sys.osx.mach.getsect;
struct Image
{
private mach_header* header_;
private uint index_;
this (uint index)
{
header_ = _dyld_get_image_header(index);
index_ = index;
}
static uint numberOfImages ()
{
return _dyld_image_count;
}
static int opApply (int delegate(ref Image) dg)
{
int result;
for (size_t i = 0; i < numberOfImages; i++)
{
result = dg(Image(i));
if (result)
break;
}
return result;
}
static int opApplyReverse (int delegate(ref Image) dg)
{
int result;
for (int i = numberOfImages - 1; i >= 0; i--)
{
result = dg(Image(i));
if (result)
break;
}
return result;
}
mach_header* header ()
{
return header_;
}
mach_header_64* header64 ()
{
return cast(mach_header_64*) header_;
}
CPU cpu ()
{
return CPU(header_);
}
}
struct CPU
{
private mach_header* header;
static CPU opCall (mach_header* header)
{
CPU cpu;
cpu.header = header;
return cpu;
}
bool is32bit ()
{
return (header.magic == MH_MAGIC);
}
bool is64bit ()
{
return (header.magic == MH_MAGIC_64);
}
}
T[] getSectionData (T, string segmentName, string sectionName) ()
{
T[] array;
const c_segmentName = segmentName.ptr;
const c_sectionName = sectionName.ptr;
void* start;
void* end;
foreach_reverse (image ; Image)
{
if (image.cpu.is32bit)
{
auto header = image.header;
section* sect = getsectbynamefromheader(header, c_segmentName, c_sectionName);
if (sect is null || sect.size == 0)
continue;
start = cast(void*) (cast(byte*) header + sect.offset);
end = cast(void*) (cast(byte*) start + sect.size);
}
else
{
auto header = image.header64;
section_64* sect = getsectbynamefromheader_64(header, c_segmentName, c_sectionName);
if (sect is null || sect.size == 0)
continue;
start = cast(void*) (cast(byte*) header + sect.offset);
end = cast(void*) (cast(byte*) start + sect.size);
}
size_t len = cast(T*)end - cast(T*)start;
array ~= (cast(T*)start)[0 .. len];
}
return array;
}