Thread: [Wisp-cvs] wisp/users/dig .cvsignore,NONE,1.1 makehello.py,NONE,1.1 linkie.py,1.1,1.2
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-02-05 22:44:00
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv23760 Modified Files: linkie.py Added Files: .cvsignore makehello.py Log Message: imported makehello.py --- NEW FILE: .cvsignore --- *.pyc *.pyo --- NEW FILE: makehello.py --- #### makehello.py - test linkie.py by creating a greeter # # Copyleft © 2002 by Andres Soolo (di...@us...) # This file is licensed under the GNU GPL v2. If you # don't know what that means, please do read the GPL. # #### @(#) $Id: makehello.py,v 1.1 2003/02/05 22:43:57 digg Exp $ from linkie import Linkie code = Linkie('<') # ia32 code.place_symbol('_start') code.emit_byte(0xBA); code.emit_tetra(14) # mov edx, 14 code.emit_byte(0xB9); code.emit_tetra_sum(['message']) # mov ecx, message code.emit_byte(0xBB); code.emit_tetra(1) # mov ebx, 1 code.emit_byte(0xB8); code.emit_tetra(4) # mov eax, 4 code.emit_byte(0xCD); code.emit_byte(0x80) # int 0x80 code.emit_byte(0xBB); code.emit_tetra(0) # mov ebx, 0 code.emit_byte(0xB8); code.emit_tetra(1) # mov eax, 1 code.emit_byte(0xCD); code.emit_byte(0x80) # int 0x80 data = Linkie('<') # ia32 data.place_symbol('message') data.emit_string('Hello, world!\n') code.dump() print data.dump() print (code + data).dump() Index: linkie.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/linkie.py,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- linkie.py 5 Feb 2003 20:46:25 -0000 1.1 +++ linkie.py 5 Feb 2003 22:43:57 -0000 1.2 @@ -8,10 +8,11 @@ from struct import pack, unpack from array import array +from types import * class Linkie: def __init__ (this, byte_order): - this._symbols = {} # symbol -> address + this._symbols = [] # symbol -> address this._alignment = 1 # minimal required alignment constraint this._binary = array('c') this._skipped = 0 # uninitialized space after bits @@ -69,6 +70,8 @@ datum, = unpack(tpl, this._binary[ofs : ofs + 4]) this._binary[ofs : ofs + 4] = array('c', pack(tpl, datum + value)) + def notify_linker (this, offset, type, arg): + this._linker_notes.append((offset, type, arg)) def _emit_sum (this, size, addends, delta = 0): if this._skipped <> 0: raise "Events out of order", this for a in addends: @@ -78,7 +81,7 @@ else: # forwards this._unresolved_locals.append((a, size, len(this._binary))) elif type(a) == StringType: # global reference - this.notify_linker(size, a) + this.notify_linker(this.memsz(), size, a) else: raise 'Invalid addend', a return delta def emit_byte_sum (this, addends, delta = 0): @@ -175,6 +178,11 @@ place_symbol.""" if this._unresolved_locals: raise 'Incomplete linkie', this return this._symbols[:] + def get_notes (this): + """get_notes() -> list of (offset, type, argument) tuples + Returns a list of the linker notes from the linkie.""" + if this._unresolved_locals: raise 'Incomplete linkie', this + return this._linker_notes[:] def copy (this): if this._unresolved_locals: raise 'Incomplete linkie', this @@ -183,3 +191,42 @@ that._binary = this._binary[:] that._skipped = this._skipped that._symbols = this._symbols[:] + that._linker_notes = this._linker_notes[:] + return that + + def __add__ (this, that): + if this._unresolved_locals: raise 'Incomplete linkie', this + if that._unresolved_locals: raise 'Incomplete linkie', that + this = this.copy() + if that.filesz(): this.deskip() + this.align(that._alignment) + delta = this.memsz() + this._binary.extend(that._binary) + this._skipped = that._skipped + for sym, val in that._symbols: + this._symbols.append((sym, val + delta)) + for ofs, typ, arg in that._linker_notes: + this._linker_notes.append((ofs + delta, typ, arg)) + return this + + def dump (this): + rsymbols = {}; rnotes = {} + othersymbols = [] + for sym, val in this._symbols: + if 0 <= val < this.memsz(): + if rsymbols.has_key(val): rsymbols[val].append(sym) + else: rsymbols[val] = [sym] + else: othersymbols.append((sym, val)) + buf = []; bufstart = 0 + for i in range(this.memsz()): + if len(buf) >= 16 or rsymbols.has_key(i): + if buf: print ' %08x:' % bufstart, ' '.join(buf) + buf = []; bufstart = i + if rsymbols.has_key(i): + rsymbols[i].sort() + for s in rsymbols[i]: print s + ':' + if i < len(this._binary): + buf.append('%02x' % ord(this._binary[i])) + else: + buf.append('oo') + if buf: print ' %08x:' % bufstart, ' '.join(buf) |