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