llvm.org GIT mirror llvm / 07c3221
[llvm.py] Make ObjectFile destructor work Previous code had a double free in MemoryBuffer. The tests now pass. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152422 91177308-0d34-0410-b5e6-96231b3b80d8 Gregory Szorc 7 years ago
3 changed file(s) with 35 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
66 #
77 #===------------------------------------------------------------------------===#
88
9 from ctypes import POINTER
10 from ctypes import c_void_p
911 from ctypes import cdll
1012
1113 import ctypes.util
12 import platform
1314
1415 __all__ = [
15 "find_library",
16 "get_library",
16 'LLVMObject',
17 'find_library',
18 'get_library',
1719 ]
20
21 LLVMObject = POINTER(c_void_p)
1822
1923 def find_library():
2024 # FIXME should probably have build system define absolute path of shared
2125 # library at install time.
22 for lib in ["LLVM-3.1svn", "LLVM"]:
26 for lib in ['LLVM-3.1svn', 'LLVM']:
2327 result = ctypes.util.find_library(lib)
2428 if result:
2529 return result
3135 """Obtain a reference to the llvm library."""
3236 lib = find_library()
3337 if not lib:
34 raise Exception("LLVM shared library not found!")
38 raise Exception('LLVM shared library not found!')
3539
3640 return cdll.LoadLibrary(lib)
66 #
77 #===------------------------------------------------------------------------===#
88
9 from .common import LLVMObject
910 from .common import get_library
1011
1112 from ctypes import POINTER
3233 if filename is None:
3334 raise Exception("filename argument must be defined")
3435
35 memory = c_void_p(None)
36 memory = LLVMObject()
3637 out = c_char_p(None)
3738
3839 result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
4243 raise Exception("Could not create memory buffer: %s" % out.value)
4344
4445 self._memory = memory
46 self._as_parameter_ = self._memory
47 self._owned = True
4548
4649 def __del__(self):
47 lib.LLVMDisposeMemoryBuffer(self._memory)
50 if self._owned:
51 lib.LLVMDisposeMemoryBuffer(self._memory)
4852
4953 def from_param(self):
50 return self._memory
54 return self._as_parameter_
55
56 def release_ownership(self):
57 self._owned = False
5158
5259
5360 def register_library(library):
5461 library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
55 POINTER(c_void_p), POINTER(c_char_p)]
62 POINTER(LLVMObject), POINTER(c_char_p)]
5663 library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
5764
5865 library.LLVMDisposeMemoryBuffer.argtypes = [c_void_p]
1010 from ctypes import c_uint64
1111 from ctypes import c_void_p
1212
13 from .common import LLVMObject
1314 from .common import get_library
1415 from .core import MemoryBuffer
1516
3940
4041 self._memory = contents
4142 self._obj = lib.LLVMCreateObjectFile(contents)
43 contents.release_ownership()
44 self._as_parameter_ = self._obj
4245
4346 def __del__(self):
44 lib.LLVMDisposeObjectFile(self._obj)
47 lib.LLVMDisposeObjectFile(self)
48
49 def from_param(self):
50 return self._as_parameter_
4551
4652 def get_sections(self):
4753 """Obtain the sections in this object file.
142148 def value_string(self):
143149 pass
144150
145 ObjectFileRef = c_void_p
146151 SectionIteratorRef = c_void_p
147152 SymbolIteratorRef = c_void_p
148153 RelocationIteratorRef = c_void_p
152157
153158 # Object.h functions
154159 library.LLVMCreateObjectFile.argtypes = [MemoryBuffer]
155 library.LLVMCreateObjectFile.restype = ObjectFileRef
156
157 library.LLVMDisposeObjectFile.argtypes = [ObjectFileRef]
158
159 library.LLVMGetSections.argtypes = [ObjectFileRef]
160 library.LLVMCreateObjectFile.restype = LLVMObject
161
162 library.LLVMDisposeObjectFile.argtypes = [ObjectFile]
163
164 library.LLVMGetSections.argtypes = [ObjectFile]
160165 library.LLVMGetSections.restype = SectionIteratorRef
161166
162167 library.LLVMDisposeSectionIterator.argtypes = [SectionIteratorRef]
163168
164 library.LLVMIsSectionIteratorAtEnd.argtypes = [ObjectFileRef,
169 library.LLVMIsSectionIteratorAtEnd.argtypes = [ObjectFile,
165170 SectionIteratorRef]
166171 library.LLVMIsSectionIteratorAtEnd.restype = bool
167172
170175 library.LLVMMoveToContainingSection.argtypes = [SectionIteratorRef,
171176 SymbolIteratorRef]
172177
173 library.LLVMGetSymbols.argtypes = [ObjectFileRef]
178 library.LLVMGetSymbols.argtypes = [ObjectFile]
174179 library.LLVMGetSymbols.restype = SymbolIteratorRef
175180
176181 library.LLVMDisposeSymbolIterator.argtypes = [SymbolIteratorRef]
177182
178 library.LLVMIsSymbolIteratorAtEnd.argtypes = [ObjectFileRef,
183 library.LLVMIsSymbolIteratorAtEnd.argtypes = [ObjectFile,
179184 SymbolIteratorRef]
180185 library.LLVMIsSymbolIteratorAtEnd.restype = bool
181186