Object format

Compiled code is stored in a different format then the usual ELF format. A new format is used, coined ‘oj’. It is valid json and contains all the information that is also in ELF, but then in more plain text format. You can open and even edit an oj-object file with a text editor.

Object files are used to store assembled code.

Information contained is code, symbol table and relocation information.

The hierarchy is as follows:

  • an object file contains zero or more sections.
  • an object file may contains memory definitions, which contain a sequence of contained sections.
  • a section contains a name and data.
  • relocations are offset into a section, refer a symbol and have a type
  • symbols are offset into a section and have a name
  • debug data have an offset into a section and contain data.
  • sections cannot overlap
class ppci.binutils.objectfile.Image(name: str, address: int)

Memory image.

A memory image is a piece that can be loaded into memory.

add_section(section)

Add a section to this memory image

data

Get the data of this memory

size

Determine the size of this memory

class ppci.binutils.objectfile.ObjectFile(arch)

Container for sections with compiled code or data. Also contains symbols and relocation entries. Also contains debug information.

add_image(image)

Add an image

add_relocation(reloc)

Add a relocation entry

add_section(section)

Add a section

add_symbol(id, name, binding, value, section)

Define a new symbol

byte_size

Get the size in bytes of this object file

create_section(name)

Create and add a section with the given name.

del_symbol(name)

Remove a symbol with a given name

get_defined_symbols()

Get a list of defined symbols.

get_image(name)

Get a memory image

get_section(name, create=False)

Get or create a section with the given name

get_symbol(name)

Get a symbol

get_symbol_id_value(symbol_id)

Lookup a symbol and determine its value

get_undefined_symbols()

Get a list of undefined symbols.

has_section(name)

Check if the object file has a section with the given name

has_symbol(name)

Check if this object file has a symbol with name ‘name’

static load(input_file)

Load object file from file

save(output_file)

Save object file to a file like object

serialize()

Serialize the object into a dictionary structure suitable for json.

class ppci.binutils.objectfile.RelocationEntry(reloc_type, symbol_id, section, offset, addend)

A relocation entry.

While it mich be confusing to have Relocation here, and RelocationType in arch.encoding, this is cleaner, since the relocation here is a record indicating a relocation, while the relocation type is emitted by instructions and can actually apply the relocation.

This class is a record holding information on where to a apply which relocation. Not how to do the actual relocation.

class ppci.binutils.objectfile.Section(name)

A defined region of data in the object file

add_data(data)

Append data to the end of this section

class ppci.binutils.objectfile.Symbol(id, name, binding, value, section)

A symbol definition in an object file

undefined

Test if this symbol is undefined.

ppci.binutils.objectfile.asc2bin(data)

Decode ascii into binary

ppci.binutils.objectfile.bin2asc(data)

Encode binary data as ascii. If it is a large data set, then use a list of hex characters.

ppci.binutils.objectfile.chunks(data, size=30)

Split iterable thing into n-sized chunks

ppci.binutils.objectfile.deserialize(data)

Create an object file from dict-like data

ppci.binutils.objectfile.get_object(obj)

Try hard to load an object

ppci.binutils.objectfile.merge_memories(mem1, mem2, name)

Merge two memories into a new one

ppci.binutils.objectfile.print_object(obj)

Display an object in a user friendly manner

ppci.binutils.objectfile.serialize(x)

Serialize an object so it can be json-ified, or serialized