PageRenderTime 56ms CodeModel.GetById 18ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/rt/image.d

http://github.com/AlexeyProkhin/druntime
D | 150 lines | 105 code | 34 blank | 11 comment | 13 complexity | a075df7bd46a6984f617c53e47288a08 MD5 | raw file
  1/**
  2 * Copyright: Copyright Digital Mars 2010.
  3 * License:   <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
  4 * Authors:   Jacob Carlborg
  5 * Version: Initial created: Feb 23, 2010
  6 */
  7
  8/*          Copyright Digital Mars 2010.
  9 * Distributed under the Boost Software License, Version 1.0.
 10 *    (See accompanying file LICENSE or copy at
 11 *          http://www.boost.org/LICENSE_1_0.txt)
 12 */
 13module rt.image;
 14
 15version (OSX):
 16
 17import core.sys.osx.mach.dyld;
 18import core.sys.osx.mach.loader;
 19import core.sys.osx.mach.getsect;
 20
 21struct Image
 22{
 23        private mach_header* header_;
 24        private uint index_;
 25
 26        this (uint index)
 27        {
 28                header_ = _dyld_get_image_header(index);
 29                index_ = index;
 30        }
 31
 32        static uint numberOfImages ()
 33        {
 34                return _dyld_image_count;
 35        }
 36
 37        static int opApply (int delegate(ref Image) dg)
 38        {
 39                int result;
 40
 41                for (uint i = 0; i < numberOfImages; i++)
 42                {
 43                        auto image = Image(i);
 44                        result = dg(image);
 45
 46                        if (result)
 47                                break;
 48                }
 49
 50                return result;
 51        }
 52
 53        static int opApplyReverse (int delegate(ref Image) dg)
 54        {
 55                int result;
 56
 57                for (int i = numberOfImages - 1; i >= 0; i--)
 58                {
 59                        auto image = Image(i);
 60                        result = dg(image);
 61
 62                        if (result)
 63                                break;
 64                }
 65
 66                return result;
 67        }
 68
 69        mach_header* header ()
 70        {
 71                return header_;
 72        }
 73
 74        mach_header_64* header64 ()
 75        {
 76                return cast(mach_header_64*) header_;
 77        }
 78
 79        CPU cpu ()
 80        {
 81                return CPU(header_);
 82        }
 83}
 84
 85struct CPU
 86{
 87        private mach_header* header;
 88
 89        static CPU opCall (mach_header* header)
 90        {
 91                CPU cpu;
 92                cpu.header = header;
 93
 94                return cpu;
 95        }
 96
 97        bool is32bit ()
 98        {
 99                return (header.magic == MH_MAGIC);
100        }
101
102        bool is64bit ()
103        {
104                return (header.magic == MH_MAGIC_64);
105        }
106}
107
108T[] getSectionData (T, string segmentName, string sectionName) ()
109{
110    T[] array;
111
112    const c_segmentName = segmentName.ptr;
113    const c_sectionName = sectionName.ptr;
114
115    void* start;
116    void* end;
117
118    foreach_reverse (image ; Image)
119    {
120        if (image.cpu.is32bit)
121        {
122            auto header = image.header;
123            section* sect = getsectbynamefromheader(header, c_segmentName, c_sectionName);
124
125            if (sect is null || sect.size == 0)
126                continue;
127
128            start = cast(void*) (cast(byte*) header + sect.offset);
129            end = cast(void*) (cast(byte*) start + sect.size);
130        }
131
132        else
133        {
134            auto header = image.header64;
135            section_64* sect = getsectbynamefromheader_64(header, c_segmentName, c_sectionName);
136
137            if (sect is null || sect.size == 0)
138                continue;
139
140            start = cast(void*) (cast(byte*) header + sect.offset);
141            end = cast(void*) (cast(byte*) start + sect.size);
142        }
143
144            size_t len = cast(T*)end - cast(T*)start;
145                array ~= (cast(T*)start)[0 .. len];
146    }
147
148    return array;
149}
150