The Library class provides interaction with ELF files. To open a file from a filename, simply type:
from pylibelf import libelf
lib = libelf.Library("/bin/bash")
print lib
This will print out all the gory details of the library. If you don’t want this behaviour, you can set the sanestr setting to False (see settings for details), or use repr().
Platform : | Unix |
---|
Like ldd, return a list of libraries that an executable or shared library depends on.
Parameters: |
|
---|
Open an ELF file from a path or fd.
Parameters: |
|
---|
Note
Write support is currently experimental and may or may not bite you in the buttocks.
Filename of the object, if path was given to the constructor.
Mode the ELF file is opened in.
Type of ELF file (ET_EXEC, ET_DYN, etc).
Kind of file (ELF_K_ELF, ELF_K_COFF, ELF_K_AR).
The class of the ELF file (e.g. ELFCLASS32, ELFCLASS64).
Byte ordering of the ELF file (e.g. ELFDATA2LSB, ELFDATA2MSB).
OS/ABI identification.
The version of the ABI it wants.
The machine architecture the ELF file was compiled for (e.g. EM_386, EM_X86_64).
The entrypoint of the program.
The segments in the object file.
The sections in the object file.
The string table section holding the section names.
The symbol table section of the object file (SHT_SYMTAB). Set for relocatable object files.
The symbol table section of the object file (SHT_DYNSYM). Set for shared libraries and executables.
The dynamic section of the object file (SHT_DYNAMIC).
Direct dependencies of this object file.
The name of the shared library.
Write the ELF representation to the path given in the constructor, or to path if supplied as an argument.
Explicitly close the library (this would otherwise happen in the deconstructor).
Example:
>>> from pylibelf import libelf, elf, elf_constants
>>> lib = libelf.Library('/bin/ls')
>>> print lib.findsection(name='.interp')
Section(name: '.interp'
type: Type(SHT_PROGBITS, 'Program data')
flags: 0x02
address: 0x400238
offset: 0x238
link: 0x00
info: 0x00
addralign: 0x01
entsize: 0x00
data: SectionData(type: Type(ELF_T_BYTE, 0)
version: 0x01
data: '/lib64/ld-linux-x86-64.so.2\x00'
size: 0x1c))
>>> elf.pretty_print(elf.findall(lib.segments, type=elf_constants.PT_LOAD))
[Segment(type: Type(PT_LOAD, 'Loadable program segment')
readable: 0x01
writable: 0x00
executable: 0x01
alignment: Type('2**21')
offset: 0x00
virtual_address: 0x400000
physical_address: 0x400000
filesize: 0x1aa84
memsize: 0x1aa84
section_names: ['.interp'
'.note.ABI-tag'
'.note.gnu.build-id'
'.hash'
'.gnu.hash'
'.dynsym'
'.dynstr'
'.gnu.version'
'.gnu.version_r'
'.rela.dyn'
'.rela.plt'
'.init'
'.plt'
'.text'
'.fini'
'.rodata'
'.eh_frame_hdr'
'.eh_frame'])
Segment(type: Type(PT_LOAD, 'Loadable program segment')
readable: 0x01
writable: 0x01
executable: 0x00
alignment: Type('2**21')
offset: 0x1ade0
virtual_address: 0x61ade0
physical_address: 0x61ade0
filesize: 0x750
memsize: 0x14a0
section_names: ['.ctors'
'.dtors'
'.jcr'
'.dynamic'
'.got'
'.got.plt'
'.data'
'.bss'])]
Represents an ELF segment. Attributes are as listed in the constructor, they correspond to the p_* attributes of the ELF segment header. Additional attributes are readable, writable and executable.
Represents an ELF section. Attributes are as listed in the constructor, they correspond to the sh_* attributes of the ELF section header.
Indicates whether this section is a symbol table.
Extends Section. symbols should be a list of Symbol. kwargs is passed to the parent constructor.
The list of symbols in the section.
The string table holding the names of the symbols
Find a symbol with name name and optionally attributes specified by kwargs.
Extends Section. strings should be a list of strings.
The list of strings that are in the string table.
Add a bunch of strings to the string table. This method takes care of substrings (e.g. serialize [‘spamham’, ‘ham’] as ‘\0spamham\0’ instead of ‘\0spamham\0ham\0’).
Return the string given the offset into the section data.
Extends Section. strings should be a list of Relocation.
The relocations specified by the section.
The section the relocations apply to.
The associated symbol table.
Extends Section. dyns should be a list of Dyn.
The list of entries with dynamic linking information.
The string table associated with the dynamic section.
List of paths that the library specifies to look for for dependencies.
Equivalent to Library.dependencies
Represents the data of a section, it is the data attribute of a Section.
The data of the section as a byte string.
The type of the items in the data (e.g. ELF_T_BYTE, ELF_T_DYN, etc)
Alignment used by the section.
Represents a single symbol in a symbol table. The constructor arguments are attributes of the instance.
Get the section the symbol was specified in relation to. lib is the Library the symbol was found in.
Represents a single relocation. machine should be one of the EM_* constants. offset, symindex and type are instance attributes. type can have values specified by the R_ constants (constants should be chosen according to machine).
The r_info attribute of the associated Elfxx_Rel structure.
Like Relocation but with an addend.
An entry in the dynamic section. tag and value are instance attributes, and can have values specified by the flags DT_ and DF_ respectively.
Indicates whether self.value should be treated as a pointer.
Open an ar file for reading.
Path to the ar file.
Symbol table of the ar file.
Get the ArLibrary object associated with symbol name symbolname.
Get the ArLibrary object associated with symbol symbol.
Get the ArLibrary object associated with offset offset.
An ArLibrary is like a Library with some extra functionality.
Save the library to a file specified by path. After extraction addtional changes can be saved using the potentially broken save method.
The header in the ar file holding metadata on a contained library. Attributes are name, ``date, uid, gid, mode, dataoffset, size.
A symbol in the ar file. Attribute are name, offset and hash.
Example:
>>> from pylibelf import libelf
>>> ar = libelf.Ar('/usr/lib/libpython2.6.a')
>>> print ar.libs[0].header
ArHeader(name: 'getbuildinfo.o'
date: Type('Fri Apr 16 16:42:48 2010')
uid: 0x00
gid: 0x00
mode: 0x81a4
offset: 0x6b9a
dataoffset: 0x6bd6
size: 0x8a0)
>>> print ar.libs[0].symtab
SymbolTable(name: '.symtab'
type: Type(SHT_SYMTAB, 'Symbol table')
flags: 0x00
address: 0x00
offset: 0x568
link: 0x0d
info: 0x0b
addralign: 0x08
entsize: 0x18
data: SectionData(type: Type(ELF_T_SYM, 11)
version: 0x01
data: '\x00\x00\x00\x00\x00\x00\x00\x00\x ...'
size: 0x180)
symbols: [Symbol(name: ''
value: 0x00
size: 0x00
binding: Type(STB_LOCAL, 'Local symbol')
type: Type(STT_NOTYPE, 'Symbol type is unspecified')
shndx: 0x00)
...
Symbol(name: 'PyOS_snprintf'
value: 0x00
size: 0x00
binding: Type(STB_GLOBAL, 'Global symbol')
type: Type(STT_NOTYPE, 'Symbol type is unspecified')
shndx: 0x00)])