1 #===- object.py - Python Object Bindings --------------------*- python -*--===#
3 # The LLVM Compiler Infrastructure
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
8 #===------------------------------------------------------------------------===#
10 from ctypes import c_char_p
11 from ctypes import c_uint64
12 from ctypes import c_void_p
14 from .common import LLVMObject
15 from .common import get_library
16 from .core import MemoryBuffer
26 class ObjectFile(object):
27 """Represents an object/binary file."""
29 def __init__(self, filename=None, contents=None):
30 """Construct an instance from a filename or binary data.
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.
37 assert isinstance(contents, MemoryBuffer)
39 if filename is not None:
40 contents = MemoryBuffer(filename=filename)
42 self._memory = contents
43 self._obj = lib.LLVMCreateObjectFile(contents)
44 contents.release_ownership()
45 self._as_parameter_ = self._obj
48 lib.LLVMDisposeObjectFile(self)
51 return self._as_parameter_
53 def get_sections(self):
54 """Obtain the sections in this object file.
56 This is an iterator for llvm.object.Section instances.
60 def get_symbols(self):
61 """Obtain the symbols in this object file.
63 This is an iterator for llvm.object.Symbol instances.
66 class Section(object):
67 """Represents a section in an object file."""
69 def __init__(self, obj=None):
70 """Construct a new section instance.
72 Section instances can currently only be created from an ObjectFile
73 instance. Therefore, this constructor should not be used outside of
97 # TODO consider exposing more Pythonic interface, like __contains__
98 def has_symbol(self, symbol):
101 def get_relocations(self):
104 class Symbol(object):
117 def file_offset(self):
124 class Relocation(object):
149 def value_string(self):
152 SectionIteratorRef = c_void_p
153 SymbolIteratorRef = c_void_p
154 RelocationIteratorRef = c_void_p
156 def register_library(library):
157 """Register function prototypes with LLVM library instance."""
160 library.LLVMCreateObjectFile.argtypes = [MemoryBuffer]
161 library.LLVMCreateObjectFile.restype = LLVMObject
163 library.LLVMDisposeObjectFile.argtypes = [ObjectFile]
165 library.LLVMGetSections.argtypes = [ObjectFile]
166 library.LLVMGetSections.restype = SectionIteratorRef
168 library.LLVMDisposeSectionIterator.argtypes = [SectionIteratorRef]
170 library.LLVMIsSectionIteratorAtEnd.argtypes = [ObjectFile,
172 library.LLVMIsSectionIteratorAtEnd.restype = bool
174 library.LLVMMoveToNextSection.argtypes = [SectionIteratorRef]
176 library.LLVMMoveToContainingSection.argtypes = [SectionIteratorRef,
179 library.LLVMGetSymbols.argtypes = [ObjectFile]
180 library.LLVMGetSymbols.restype = SymbolIteratorRef
182 library.LLVMDisposeSymbolIterator.argtypes = [SymbolIteratorRef]
184 library.LLVMIsSymbolIteratorAtEnd.argtypes = [ObjectFile,
186 library.LLVMIsSymbolIteratorAtEnd.restype = bool
188 library.LLVMMoveToNextSymbol.argtypes = [SymbolIteratorRef]
190 library.LLVMGetSectionName.argtypes = [SectionIteratorRef]
191 library.LLVMGetSectionName.restype = c_char_p
193 library.LLVMGetSectionSize.argtypes = [SectionIteratorRef]
194 library.LLVMGetSectionSize.restype = c_uint64
196 library.LLVMGetSectionContents.argtypes = [SectionIteratorRef]
197 library.LLVMGetSectionContents.restype = c_char_p
199 library.LLVMGetSectionAddress.argtypes = [SectionIteratorRef]
200 library.LLVMGetSectionAddress.restype = c_uint64
202 library.LLVMGetSectionContainsSymbol.argtypes = [SectionIteratorRef,
204 library.LLVMGetSectionContainsSymbol.restype = bool
206 library.LLVMGetRelocations.argtypes = [SectionIteratorRef]
207 library.LLVMGetRelocations.restype = RelocationIteratorRef
209 library.LLVMDisposeRelocationIterator.argtypes = [RelocationIteratorRef]
211 library.LLVMIsRelocationIteratorAtEnd.argtypes = [SectionIteratorRef,
212 RelocationIteratorRef]
213 library.LLVMIsRelocationIteratorAtEnd.restype = bool
215 library.LLVMMoveToNextRelocation.argtypes = [RelocationIteratorRef]
217 library.LLVMGetSymbolName.argtypes = [SymbolIteratorRef]
218 library.LLVMGetSymbolName.restype = c_char_p
220 library.LLVMGetSymbolAddress.argtypes = [SymbolIteratorRef]
221 library.LLVMGetSymbolAddress.restype = c_uint64
223 library.LLVMGetSymbolFileOffset.argtypes = [SymbolIteratorRef]
224 library.LLVMGetSymbolFileOffset.restype = c_uint64
226 library.LLVMGetSymbolSize.argtypes = [SymbolIteratorRef]
227 library.LLVMGetSymbolSize.restype = c_uint64
229 library.LLVMGetRelocationAddress.argtypes = [SymbolIteratorRef]
230 library.LLVMGetRelocationAddress.restype = c_uint64
232 library.LLVMGetRelocationOffset.argtypes = [RelocationIteratorRef]
233 library.LLVMGetRelocationOffset.restype = c_uint64
235 library.LLVMGetRelocationSymbol.argtypes = [RelocationIteratorRef]
236 library.LLVMGetRelocationSymbol.restype = SymbolIteratorRef
238 library.LLVMGetRelocationType.argtypes = [RelocationIteratorRef]
239 library.LLVMGetRelocationType.restype = c_uint64
241 library.LLVMGetRelocationTypeName.argtypes = [RelocationIteratorRef]
242 library.LLVMGetRelocationTypeName.restype = c_char_p
244 library.LLVMGetRelocationValueString.argtypes = [RelocationIteratorRef]
245 library.LLVMGetRelocationValueString.restype = c_char_p
248 register_library(lib)