2014-04-14 20:52:16 +02:00
|
|
|
require 'asm/assembler'
|
2014-04-14 17:09:56 +02:00
|
|
|
|
|
|
|
module Asm
|
|
|
|
module Arm
|
|
|
|
|
2014-04-14 20:52:16 +02:00
|
|
|
# Relocation constants
|
|
|
|
# Note that in this assembler, a relocation simply means any
|
|
|
|
# reference to a label that can only be determined at assembly time
|
|
|
|
# or later (as in the normal meaning)
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-14 20:52:16 +02:00
|
|
|
R_ARM_PC24 = 0x01
|
|
|
|
R_ARM_ABS32 = 0x02
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-14 20:52:16 +02:00
|
|
|
# Unofficial (cant be used for extern relocations)
|
|
|
|
R_ARM_PC12 = 0xF0
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-14 20:52:16 +02:00
|
|
|
# TODO actually find the closest somehow
|
|
|
|
def self.closest_addrtable(as)
|
|
|
|
as.objects.find do |obj|
|
|
|
|
obj.is_a?(Asm::Arm::AddrTableObject)
|
|
|
|
end || (raise Asm::AssemblyError.new('could not find addrtable to use', nil))
|
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-14 20:52:16 +02:00
|
|
|
def self.write_resolved_relocation(io, addr, type)
|
|
|
|
case type
|
|
|
|
when R_ARM_PC24
|
|
|
|
diff = addr - io.tell - 8
|
2014-04-20 01:28:57 +02:00
|
|
|
if (diff.abs > (1 << 25))
|
|
|
|
raise Asm::AssemblyError.new('offset too large for R_ARM_PC24 relocation', nil)
|
|
|
|
end
|
2014-04-14 20:52:16 +02:00
|
|
|
packed = [diff >> 2].pack('l')
|
|
|
|
io << packed[0,3]
|
|
|
|
when R_ARM_ABS32
|
|
|
|
packed = [addr].pack('l')
|
|
|
|
io << packed
|
|
|
|
when R_ARM_PC12
|
|
|
|
diff = addr - io.tell - 8
|
|
|
|
if (diff.abs > 2047)
|
2014-04-20 01:28:57 +02:00
|
|
|
raise Asm::AssemblyError.new('offset too large for R_ARM_PC12 relocation', nil)
|
2014-04-14 20:52:16 +02:00
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-14 20:52:16 +02:00
|
|
|
val = diff.abs
|
|
|
|
sign = (diff>0)?1:0
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-14 20:52:16 +02:00
|
|
|
curr = io.read_uint32
|
|
|
|
io.seek(-4, IO::SEEK_CUR)
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-14 20:52:16 +02:00
|
|
|
io.write_uint32 (curr & ~0b00000000100000000000111111111111) |
|
|
|
|
val | (sign << 23)
|
|
|
|
else
|
|
|
|
raise 'unknown relocation type'
|
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|