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 get_library
15 from .core import MemoryBuffer
25 class ObjectFile(object):
26 """Represents an object/binary file."""
28 def __init__(self, filename=None, contents=None):
29 """Construct an instance from a filename or binary data.
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.
36 assert isinstance(contents, MemoryBuffer)
38 if filename is not None:
39 contents = MemoryBuffer(filename=filename)
41 self._memory = contents
42 self._obj = lib.LLVMCreateObjectFile(contents)
45 lib.LLVMDisposeObjectFile(self._obj)
47 def get_sections(self):
48 """Obtain the sections in this object file.
50 This is an iterator for llvm.object.Section instances.
54 def get_symbols(self):
55 """Obtain the symbols in this object file.
57 This is an iterator for llvm.object.Symbol instances.
60 class Section(object):
61 """Represents a section in an object file."""
63 def __init__(self, obj=None):
64 """Construct a new section instance.
66 Section instances can currently only be created from an ObjectFile
67 instance. Therefore, this constructor should not be used outside of
91 # TODO consider exposing more Pythonic interface, like __contains__
92 def has_symbol(self, symbol):
95 def get_relocations(self):
111 def file_offset(self):
118 class Relocation(object):
143 def value_string(self):
146 ObjectFileRef = c_void_p
147 SectionIteratorRef = c_void_p
148 SymbolIteratorRef = c_void_p
149 RelocationIteratorRef = c_void_p
151 def register_library(library):
152 """Register function prototypes with LLVM library instance."""
155 library.LLVMCreateObjectFile.argtypes = [MemoryBuffer]
156 library.LLVMCreateObjectFile.restype = ObjectFileRef
158 library.LLVMDisposeObjectFile.argtypes = [ObjectFileRef]
160 library.LLVMGetSections.argtypes = [ObjectFileRef]
161 library.LLVMGetSections.restype = SectionIteratorRef
163 library.LLVMDisposeSectionIterator.argtypes = [SectionIteratorRef]
165 library.LLVMIsSectionIteratorAtEnd.argtypes = [ObjectFileRef,
167 library.LLVMIsSectionIteratorAtEnd.restype = bool
169 library.LLVMMoveToNextSection.argtypes = [SectionIteratorRef]
171 library.LLVMMoveToContainingSection.argtypes = [SectionIteratorRef,
174 library.LLVMGetSymbols.argtypes = [ObjectFileRef]
175 library.LLVMGetSymbols.restype = SymbolIteratorRef
177 library.LLVMDisposeSymbolIterator.argtypes = [SymbolIteratorRef]
179 library.LLVMIsSymbolIteratorAtEnd.argtypes = [ObjectFileRef,
181 library.LLVMIsSymbolIteratorAtEnd.restype = bool
183 library.LLVMMoveToNextSymbol.argtypes = [SymbolIteratorRef]
185 library.LLVMGetSectionName.argtypes = [SectionIteratorRef]
186 library.LLVMGetSectionName.restype = c_char_p
188 library.LLVMGetSectionSize.argtypes = [SectionIteratorRef]
189 library.LLVMGetSectionSize.restype = c_uint64
191 library.LLVMGetSectionContents.argtypes = [SectionIteratorRef]
192 library.LLVMGetSectionContents.restype = c_char_p
194 library.LLVMGetSectionAddress.argtypes = [SectionIteratorRef]
195 library.LLVMGetSectionAddress.restype = c_uint64
197 library.LLVMGetSectionContainsSymbol.argtypes = [SectionIteratorRef,
199 library.LLVMGetSectionContainsSymbol.restype = bool
201 library.LLVMGetRelocations.argtypes = [SectionIteratorRef]
202 library.LLVMGetRelocations.restype = RelocationIteratorRef
204 library.LLVMDisposeRelocationIterator.argtypes = [RelocationIteratorRef]
206 library.LLVMIsRelocationIteratorAtEnd.argtypes = [SectionIteratorRef,
207 RelocationIteratorRef]
208 library.LLVMIsRelocationIteratorAtEnd.restype = bool
210 library.LLVMMoveToNextRelocation.argtypes = [RelocationIteratorRef]
212 library.LLVMGetSymbolName.argtypes = [SymbolIteratorRef]
213 library.LLVMGetSymbolName.restype = c_char_p
215 library.LLVMGetSymbolAddress.argtypes = [SymbolIteratorRef]
216 library.LLVMGetSymbolAddress.restype = c_uint64
218 library.LLVMGetSymbolFileOffset.argtypes = [SymbolIteratorRef]
219 library.LLVMGetSymbolFileOffset.restype = c_uint64
221 library.LLVMGetSymbolSize.argtypes = [SymbolIteratorRef]
222 library.LLVMGetSymbolSize.restype = c_uint64
224 library.LLVMGetRelocationAddress.argtypes = [SymbolIteratorRef]
225 library.LLVMGetRelocationAddress.restype = c_uint64
227 library.LLVMGetRelocationOffset.argtypes = [RelocationIteratorRef]
228 library.LLVMGetRelocationOffset.restype = c_uint64
230 library.LLVMGetRelocationSymbol.argtypes = [RelocationIteratorRef]
231 library.LLVMGetRelocationSymbol.restype = SymbolIteratorRef
233 library.LLVMGetRelocationType.argtypes = [RelocationIteratorRef]
234 library.LLVMGetRelocationType.restype = c_uint64
236 library.LLVMGetRelocationTypeName.argtypes = [RelocationIteratorRef]
237 library.LLVMGetRelocationTypeName.restype = c_char_p
239 library.LLVMGetRelocationValueString.argtypes = [RelocationIteratorRef]
240 library.LLVMGetRelocationValueString.restype = c_char_p
243 register_library(lib)