summaryrefslogtreecommitdiff
path: root/bindings/python/llvm/object.py
blob: f633f609536a35ef05c9ee653797edd9ada2c5e0 (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
#===- object.py - Python Object Bindings --------------------*- python -*--===#
#
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
#===------------------------------------------------------------------------===#

from ctypes import c_char_p
from ctypes import c_uint64
from ctypes import c_void_p

from .common import LLVMObject
from .common import get_library
from .core import MemoryBuffer

__all__ = [
    "lib",
    "ObjectFile",
    "Relocation",
    "Section",
    "Symbol",
]

class ObjectFile(object):
    """Represents an object/binary file."""

    def __init__(self, filename=None, contents=None):
        """Construct an instance from a filename or binary data.

        filename must be a path to a file that can be opened with open().
        contents can be either a native Python buffer type (like str) or a
        llvm.core.MemoryBuffer instance.
        """
        if contents:
            assert isinstance(contents, MemoryBuffer)

        if filename is not None:
            contents = MemoryBuffer(filename=filename)

        self._memory = contents
        self._obj = lib.LLVMCreateObjectFile(contents)
        contents.release_ownership()
        self._as_parameter_ = self._obj

    def __del__(self):
        lib.LLVMDisposeObjectFile(self)

    def from_param(self):
        return self._as_parameter_

    def get_sections(self):
        """Obtain the sections in this object file.

        This is an iterator for llvm.object.Section instances.
        """
        pass

    def get_symbols(self):
        """Obtain the symbols in this object file.

        This is an iterator for llvm.object.Symbol instances.
        """

class Section(object):
    """Represents a section in an object file."""

    def __init__(self, obj=None):
        """Construct a new section instance.

        Section instances can currently only be created from an ObjectFile
        instance. Therefore, this constructor should not be used outside of
        this module.
        """
        pass

    def __del__(self):
        pass

    @property
    def name(self):
        pass

    @property
    def size(self):
        pass

    @property
    def contents(self):
        pass

    @property
    def address(self):
        pass

    # TODO consider exposing more Pythonic interface, like __contains__
    def has_symbol(self, symbol):
        pass

    def get_relocations(self):
        pass

class Symbol(object):
    def __init__(self):
        pass

    @property
    def name(self):
        pass

    @property
    def address(self):
        pass

    @property
    def file_offset(self):
        pass

    @property
    def size(self):
        pass

class Relocation(object):
    def __init__(self):
        pass

    @property
    def address(self):
        pass

    @property
    def offset(self):
        pass

    @property
    def symbol(self):
        pass

    @property
    def type(self):
        pass

    @property
    def type_name(self):
        pass

    @property
    def value_string(self):
        pass

SectionIteratorRef = c_void_p
SymbolIteratorRef = c_void_p
RelocationIteratorRef = c_void_p

def register_library(library):
    """Register function prototypes with LLVM library instance."""

    # Object.h functions
    library.LLVMCreateObjectFile.argtypes = [MemoryBuffer]
    library.LLVMCreateObjectFile.restype = LLVMObject

    library.LLVMDisposeObjectFile.argtypes = [ObjectFile]

    library.LLVMGetSections.argtypes = [ObjectFile]
    library.LLVMGetSections.restype = SectionIteratorRef

    library.LLVMDisposeSectionIterator.argtypes = [SectionIteratorRef]

    library.LLVMIsSectionIteratorAtEnd.argtypes = [ObjectFile,
            SectionIteratorRef]
    library.LLVMIsSectionIteratorAtEnd.restype = bool

    library.LLVMMoveToNextSection.argtypes = [SectionIteratorRef]

    library.LLVMMoveToContainingSection.argtypes = [SectionIteratorRef,
            SymbolIteratorRef]

    library.LLVMGetSymbols.argtypes = [ObjectFile]
    library.LLVMGetSymbols.restype = SymbolIteratorRef

    library.LLVMDisposeSymbolIterator.argtypes = [SymbolIteratorRef]

    library.LLVMIsSymbolIteratorAtEnd.argtypes = [ObjectFile,
            SymbolIteratorRef]
    library.LLVMIsSymbolIteratorAtEnd.restype = bool

    library.LLVMMoveToNextSymbol.argtypes = [SymbolIteratorRef]

    library.LLVMGetSectionName.argtypes = [SectionIteratorRef]
    library.LLVMGetSectionName.restype = c_char_p

    library.LLVMGetSectionSize.argtypes = [SectionIteratorRef]
    library.LLVMGetSectionSize.restype = c_uint64

    library.LLVMGetSectionContents.argtypes = [SectionIteratorRef]
    library.LLVMGetSectionContents.restype = c_char_p

    library.LLVMGetSectionAddress.argtypes = [SectionIteratorRef]
    library.LLVMGetSectionAddress.restype = c_uint64

    library.LLVMGetSectionContainsSymbol.argtypes = [SectionIteratorRef,
            SymbolIteratorRef]
    library.LLVMGetSectionContainsSymbol.restype = bool

    library.LLVMGetRelocations.argtypes = [SectionIteratorRef]
    library.LLVMGetRelocations.restype = RelocationIteratorRef

    library.LLVMDisposeRelocationIterator.argtypes = [RelocationIteratorRef]

    library.LLVMIsRelocationIteratorAtEnd.argtypes = [SectionIteratorRef,
            RelocationIteratorRef]
    library.LLVMIsRelocationIteratorAtEnd.restype = bool

    library.LLVMMoveToNextRelocation.argtypes = [RelocationIteratorRef]

    library.LLVMGetSymbolName.argtypes = [SymbolIteratorRef]
    library.LLVMGetSymbolName.restype = c_char_p

    library.LLVMGetSymbolAddress.argtypes = [SymbolIteratorRef]
    library.LLVMGetSymbolAddress.restype = c_uint64

    library.LLVMGetSymbolFileOffset.argtypes = [SymbolIteratorRef]
    library.LLVMGetSymbolFileOffset.restype = c_uint64

    library.LLVMGetSymbolSize.argtypes = [SymbolIteratorRef]
    library.LLVMGetSymbolSize.restype = c_uint64

    library.LLVMGetRelocationAddress.argtypes = [SymbolIteratorRef]
    library.LLVMGetRelocationAddress.restype = c_uint64

    library.LLVMGetRelocationOffset.argtypes = [RelocationIteratorRef]
    library.LLVMGetRelocationOffset.restype = c_uint64

    library.LLVMGetRelocationSymbol.argtypes = [RelocationIteratorRef]
    library.LLVMGetRelocationSymbol.restype = SymbolIteratorRef

    library.LLVMGetRelocationType.argtypes = [RelocationIteratorRef]
    library.LLVMGetRelocationType.restype = c_uint64

    library.LLVMGetRelocationTypeName.argtypes = [RelocationIteratorRef]
    library.LLVMGetRelocationTypeName.restype = c_char_p

    library.LLVMGetRelocationValueString.argtypes = [RelocationIteratorRef]
    library.LLVMGetRelocationValueString.restype = c_char_p

lib = get_library()
register_library(lib)