f633f609536a35ef05c9ee653797edd9ada2c5e0
[oota-llvm.git] / bindings / python / llvm / object.py
1 #===- object.py - Python Object Bindings --------------------*- python -*--===#
2 #
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
7 #
8 #===------------------------------------------------------------------------===#
9
10 from ctypes import c_char_p
11 from ctypes import c_uint64
12 from ctypes import c_void_p
13
14 from .common import LLVMObject
15 from .common import get_library
16 from .core import MemoryBuffer
17
18 __all__ = [
19     "lib",
20     "ObjectFile",
21     "Relocation",
22     "Section",
23     "Symbol",
24 ]
25
26 class ObjectFile(object):
27     """Represents an object/binary file."""
28
29     def __init__(self, filename=None, contents=None):
30         """Construct an instance from a filename or binary data.
31
32         filename must be a path to a file that can be opened with open().
33         contents can be either a native Python buffer type (like str) or a
34         llvm.core.MemoryBuffer instance.
35         """
36         if contents:
37             assert isinstance(contents, MemoryBuffer)
38
39         if filename is not None:
40             contents = MemoryBuffer(filename=filename)
41
42         self._memory = contents
43         self._obj = lib.LLVMCreateObjectFile(contents)
44         contents.release_ownership()
45         self._as_parameter_ = self._obj
46
47     def __del__(self):
48         lib.LLVMDisposeObjectFile(self)
49
50     def from_param(self):
51         return self._as_parameter_
52
53     def get_sections(self):
54         """Obtain the sections in this object file.
55
56         This is an iterator for llvm.object.Section instances.
57         """
58         pass
59
60     def get_symbols(self):
61         """Obtain the symbols in this object file.
62
63         This is an iterator for llvm.object.Symbol instances.
64         """
65
66 class Section(object):
67     """Represents a section in an object file."""
68
69     def __init__(self, obj=None):
70         """Construct a new section instance.
71
72         Section instances can currently only be created from an ObjectFile
73         instance. Therefore, this constructor should not be used outside of
74         this module.
75         """
76         pass
77
78     def __del__(self):
79         pass
80
81     @property
82     def name(self):
83         pass
84
85     @property
86     def size(self):
87         pass
88
89     @property
90     def contents(self):
91         pass
92
93     @property
94     def address(self):
95         pass
96
97     # TODO consider exposing more Pythonic interface, like __contains__
98     def has_symbol(self, symbol):
99         pass
100
101     def get_relocations(self):
102         pass
103
104 class Symbol(object):
105     def __init__(self):
106         pass
107
108     @property
109     def name(self):
110         pass
111
112     @property
113     def address(self):
114         pass
115
116     @property
117     def file_offset(self):
118         pass
119
120     @property
121     def size(self):
122         pass
123
124 class Relocation(object):
125     def __init__(self):
126         pass
127
128     @property
129     def address(self):
130         pass
131
132     @property
133     def offset(self):
134         pass
135
136     @property
137     def symbol(self):
138         pass
139
140     @property
141     def type(self):
142         pass
143
144     @property
145     def type_name(self):
146         pass
147
148     @property
149     def value_string(self):
150         pass
151
152 SectionIteratorRef = c_void_p
153 SymbolIteratorRef = c_void_p
154 RelocationIteratorRef = c_void_p
155
156 def register_library(library):
157     """Register function prototypes with LLVM library instance."""
158
159     # Object.h functions
160     library.LLVMCreateObjectFile.argtypes = [MemoryBuffer]
161     library.LLVMCreateObjectFile.restype = LLVMObject
162
163     library.LLVMDisposeObjectFile.argtypes = [ObjectFile]
164
165     library.LLVMGetSections.argtypes = [ObjectFile]
166     library.LLVMGetSections.restype = SectionIteratorRef
167
168     library.LLVMDisposeSectionIterator.argtypes = [SectionIteratorRef]
169
170     library.LLVMIsSectionIteratorAtEnd.argtypes = [ObjectFile,
171             SectionIteratorRef]
172     library.LLVMIsSectionIteratorAtEnd.restype = bool
173
174     library.LLVMMoveToNextSection.argtypes = [SectionIteratorRef]
175
176     library.LLVMMoveToContainingSection.argtypes = [SectionIteratorRef,
177             SymbolIteratorRef]
178
179     library.LLVMGetSymbols.argtypes = [ObjectFile]
180     library.LLVMGetSymbols.restype = SymbolIteratorRef
181
182     library.LLVMDisposeSymbolIterator.argtypes = [SymbolIteratorRef]
183
184     library.LLVMIsSymbolIteratorAtEnd.argtypes = [ObjectFile,
185             SymbolIteratorRef]
186     library.LLVMIsSymbolIteratorAtEnd.restype = bool
187
188     library.LLVMMoveToNextSymbol.argtypes = [SymbolIteratorRef]
189
190     library.LLVMGetSectionName.argtypes = [SectionIteratorRef]
191     library.LLVMGetSectionName.restype = c_char_p
192
193     library.LLVMGetSectionSize.argtypes = [SectionIteratorRef]
194     library.LLVMGetSectionSize.restype = c_uint64
195
196     library.LLVMGetSectionContents.argtypes = [SectionIteratorRef]
197     library.LLVMGetSectionContents.restype = c_char_p
198
199     library.LLVMGetSectionAddress.argtypes = [SectionIteratorRef]
200     library.LLVMGetSectionAddress.restype = c_uint64
201
202     library.LLVMGetSectionContainsSymbol.argtypes = [SectionIteratorRef,
203             SymbolIteratorRef]
204     library.LLVMGetSectionContainsSymbol.restype = bool
205
206     library.LLVMGetRelocations.argtypes = [SectionIteratorRef]
207     library.LLVMGetRelocations.restype = RelocationIteratorRef
208
209     library.LLVMDisposeRelocationIterator.argtypes = [RelocationIteratorRef]
210
211     library.LLVMIsRelocationIteratorAtEnd.argtypes = [SectionIteratorRef,
212             RelocationIteratorRef]
213     library.LLVMIsRelocationIteratorAtEnd.restype = bool
214
215     library.LLVMMoveToNextRelocation.argtypes = [RelocationIteratorRef]
216
217     library.LLVMGetSymbolName.argtypes = [SymbolIteratorRef]
218     library.LLVMGetSymbolName.restype = c_char_p
219
220     library.LLVMGetSymbolAddress.argtypes = [SymbolIteratorRef]
221     library.LLVMGetSymbolAddress.restype = c_uint64
222
223     library.LLVMGetSymbolFileOffset.argtypes = [SymbolIteratorRef]
224     library.LLVMGetSymbolFileOffset.restype = c_uint64
225
226     library.LLVMGetSymbolSize.argtypes = [SymbolIteratorRef]
227     library.LLVMGetSymbolSize.restype = c_uint64
228
229     library.LLVMGetRelocationAddress.argtypes = [SymbolIteratorRef]
230     library.LLVMGetRelocationAddress.restype = c_uint64
231
232     library.LLVMGetRelocationOffset.argtypes = [RelocationIteratorRef]
233     library.LLVMGetRelocationOffset.restype = c_uint64
234
235     library.LLVMGetRelocationSymbol.argtypes = [RelocationIteratorRef]
236     library.LLVMGetRelocationSymbol.restype = SymbolIteratorRef
237
238     library.LLVMGetRelocationType.argtypes = [RelocationIteratorRef]
239     library.LLVMGetRelocationType.restype = c_uint64
240
241     library.LLVMGetRelocationTypeName.argtypes = [RelocationIteratorRef]
242     library.LLVMGetRelocationTypeName.restype = c_char_p
243
244     library.LLVMGetRelocationValueString.argtypes = [RelocationIteratorRef]
245     library.LLVMGetRelocationValueString.restype = c_char_p
246
247 lib = get_library()
248 register_library(lib)