mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-18 11:24:01 +00:00
[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
This commit is contained in:
@ -7,20 +7,24 @@
|
|||||||
#
|
#
|
||||||
#===------------------------------------------------------------------------===#
|
#===------------------------------------------------------------------------===#
|
||||||
|
|
||||||
|
from ctypes import POINTER
|
||||||
|
from ctypes import c_void_p
|
||||||
from ctypes import cdll
|
from ctypes import cdll
|
||||||
|
|
||||||
import ctypes.util
|
import ctypes.util
|
||||||
import platform
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"find_library",
|
'LLVMObject',
|
||||||
"get_library",
|
'find_library',
|
||||||
|
'get_library',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
LLVMObject = POINTER(c_void_p)
|
||||||
|
|
||||||
def find_library():
|
def find_library():
|
||||||
# FIXME should probably have build system define absolute path of shared
|
# FIXME should probably have build system define absolute path of shared
|
||||||
# library at install time.
|
# library at install time.
|
||||||
for lib in ["LLVM-3.1svn", "LLVM"]:
|
for lib in ['LLVM-3.1svn', 'LLVM']:
|
||||||
result = ctypes.util.find_library(lib)
|
result = ctypes.util.find_library(lib)
|
||||||
if result:
|
if result:
|
||||||
return result
|
return result
|
||||||
@ -32,6 +36,6 @@ def get_library():
|
|||||||
"""Obtain a reference to the llvm library."""
|
"""Obtain a reference to the llvm library."""
|
||||||
lib = find_library()
|
lib = find_library()
|
||||||
if not lib:
|
if not lib:
|
||||||
raise Exception("LLVM shared library not found!")
|
raise Exception('LLVM shared library not found!')
|
||||||
|
|
||||||
return cdll.LoadLibrary(lib)
|
return cdll.LoadLibrary(lib)
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#
|
#
|
||||||
#===------------------------------------------------------------------------===#
|
#===------------------------------------------------------------------------===#
|
||||||
|
|
||||||
|
from .common import LLVMObject
|
||||||
from .common import get_library
|
from .common import get_library
|
||||||
|
|
||||||
from ctypes import POINTER
|
from ctypes import POINTER
|
||||||
@ -33,7 +34,7 @@ class MemoryBuffer(object):
|
|||||||
if filename is None:
|
if filename is None:
|
||||||
raise Exception("filename argument must be defined")
|
raise Exception("filename argument must be defined")
|
||||||
|
|
||||||
memory = c_void_p(None)
|
memory = LLVMObject()
|
||||||
out = c_char_p(None)
|
out = c_char_p(None)
|
||||||
|
|
||||||
result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
|
result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
|
||||||
@ -43,17 +44,23 @@ class MemoryBuffer(object):
|
|||||||
raise Exception("Could not create memory buffer: %s" % out.value)
|
raise Exception("Could not create memory buffer: %s" % out.value)
|
||||||
|
|
||||||
self._memory = memory
|
self._memory = memory
|
||||||
|
self._as_parameter_ = self._memory
|
||||||
|
self._owned = True
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
lib.LLVMDisposeMemoryBuffer(self._memory)
|
if self._owned:
|
||||||
|
lib.LLVMDisposeMemoryBuffer(self._memory)
|
||||||
|
|
||||||
def from_param(self):
|
def from_param(self):
|
||||||
return self._memory
|
return self._as_parameter_
|
||||||
|
|
||||||
|
def release_ownership(self):
|
||||||
|
self._owned = False
|
||||||
|
|
||||||
|
|
||||||
def register_library(library):
|
def register_library(library):
|
||||||
library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
|
library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
|
||||||
POINTER(c_void_p), POINTER(c_char_p)]
|
POINTER(LLVMObject), POINTER(c_char_p)]
|
||||||
library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
|
library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
|
||||||
|
|
||||||
library.LLVMDisposeMemoryBuffer.argtypes = [c_void_p]
|
library.LLVMDisposeMemoryBuffer.argtypes = [c_void_p]
|
||||||
|
@ -11,6 +11,7 @@ from ctypes import c_char_p
|
|||||||
from ctypes import c_uint64
|
from ctypes import c_uint64
|
||||||
from ctypes import c_void_p
|
from ctypes import c_void_p
|
||||||
|
|
||||||
|
from .common import LLVMObject
|
||||||
from .common import get_library
|
from .common import get_library
|
||||||
from .core import MemoryBuffer
|
from .core import MemoryBuffer
|
||||||
|
|
||||||
@ -40,9 +41,14 @@ class ObjectFile(object):
|
|||||||
|
|
||||||
self._memory = contents
|
self._memory = contents
|
||||||
self._obj = lib.LLVMCreateObjectFile(contents)
|
self._obj = lib.LLVMCreateObjectFile(contents)
|
||||||
|
contents.release_ownership()
|
||||||
|
self._as_parameter_ = self._obj
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
lib.LLVMDisposeObjectFile(self._obj)
|
lib.LLVMDisposeObjectFile(self)
|
||||||
|
|
||||||
|
def from_param(self):
|
||||||
|
return self._as_parameter_
|
||||||
|
|
||||||
def get_sections(self):
|
def get_sections(self):
|
||||||
"""Obtain the sections in this object file.
|
"""Obtain the sections in this object file.
|
||||||
@ -143,7 +149,6 @@ class Relocation(object):
|
|||||||
def value_string(self):
|
def value_string(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
ObjectFileRef = c_void_p
|
|
||||||
SectionIteratorRef = c_void_p
|
SectionIteratorRef = c_void_p
|
||||||
SymbolIteratorRef = c_void_p
|
SymbolIteratorRef = c_void_p
|
||||||
RelocationIteratorRef = c_void_p
|
RelocationIteratorRef = c_void_p
|
||||||
@ -153,16 +158,16 @@ def register_library(library):
|
|||||||
|
|
||||||
# Object.h functions
|
# Object.h functions
|
||||||
library.LLVMCreateObjectFile.argtypes = [MemoryBuffer]
|
library.LLVMCreateObjectFile.argtypes = [MemoryBuffer]
|
||||||
library.LLVMCreateObjectFile.restype = ObjectFileRef
|
library.LLVMCreateObjectFile.restype = LLVMObject
|
||||||
|
|
||||||
library.LLVMDisposeObjectFile.argtypes = [ObjectFileRef]
|
library.LLVMDisposeObjectFile.argtypes = [ObjectFile]
|
||||||
|
|
||||||
library.LLVMGetSections.argtypes = [ObjectFileRef]
|
library.LLVMGetSections.argtypes = [ObjectFile]
|
||||||
library.LLVMGetSections.restype = SectionIteratorRef
|
library.LLVMGetSections.restype = SectionIteratorRef
|
||||||
|
|
||||||
library.LLVMDisposeSectionIterator.argtypes = [SectionIteratorRef]
|
library.LLVMDisposeSectionIterator.argtypes = [SectionIteratorRef]
|
||||||
|
|
||||||
library.LLVMIsSectionIteratorAtEnd.argtypes = [ObjectFileRef,
|
library.LLVMIsSectionIteratorAtEnd.argtypes = [ObjectFile,
|
||||||
SectionIteratorRef]
|
SectionIteratorRef]
|
||||||
library.LLVMIsSectionIteratorAtEnd.restype = bool
|
library.LLVMIsSectionIteratorAtEnd.restype = bool
|
||||||
|
|
||||||
@ -171,12 +176,12 @@ def register_library(library):
|
|||||||
library.LLVMMoveToContainingSection.argtypes = [SectionIteratorRef,
|
library.LLVMMoveToContainingSection.argtypes = [SectionIteratorRef,
|
||||||
SymbolIteratorRef]
|
SymbolIteratorRef]
|
||||||
|
|
||||||
library.LLVMGetSymbols.argtypes = [ObjectFileRef]
|
library.LLVMGetSymbols.argtypes = [ObjectFile]
|
||||||
library.LLVMGetSymbols.restype = SymbolIteratorRef
|
library.LLVMGetSymbols.restype = SymbolIteratorRef
|
||||||
|
|
||||||
library.LLVMDisposeSymbolIterator.argtypes = [SymbolIteratorRef]
|
library.LLVMDisposeSymbolIterator.argtypes = [SymbolIteratorRef]
|
||||||
|
|
||||||
library.LLVMIsSymbolIteratorAtEnd.argtypes = [ObjectFileRef,
|
library.LLVMIsSymbolIteratorAtEnd.argtypes = [ObjectFile,
|
||||||
SymbolIteratorRef]
|
SymbolIteratorRef]
|
||||||
library.LLVMIsSymbolIteratorAtEnd.restype = bool
|
library.LLVMIsSymbolIteratorAtEnd.restype = bool
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user