orgasm 0.0.1a2 → 0.0.1a3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/bin/{disorgasm → ejaculate} +0 -0
  2. data/bin/swallow +0 -0
  3. data/lib/orgasm.rb +8 -2
  4. data/lib/orgasm/arch/i386.rb +27 -0
  5. data/lib/orgasm/{style.rb → arch/i386/base.rb} +4 -0
  6. data/lib/orgasm/{common → arch/i386/base}/address.rb +12 -19
  7. data/lib/orgasm/{common/unknown.rb → arch/i386/base/immediate.rb} +6 -10
  8. data/lib/orgasm/arch/i386/base/instruction.rb +41 -0
  9. data/lib/orgasm/arch/i386/base/register.rb +40 -0
  10. data/lib/orgasm/arch/i386/disassembler.rb +26 -154
  11. data/lib/orgasm/arch/i386/generator.rb +44 -0
  12. data/lib/orgasm/arch/i386/instructions.rb +150 -0
  13. data/lib/orgasm/arch/i386/instructions/dsl.rb +159 -0
  14. data/lib/orgasm/arch/i386/instructions/dsl/special.rb +75 -0
  15. data/lib/orgasm/arch/i386/instructions/instructions.rb +50 -0
  16. data/lib/orgasm/arch/i386/styles.rb +70 -0
  17. data/lib/orgasm/architecture.rb +103 -0
  18. data/lib/orgasm/assembler.rb +5 -16
  19. data/lib/orgasm/base.rb +50 -0
  20. data/lib/orgasm/{common/constant.rb → base/address.rb} +7 -6
  21. data/lib/orgasm/{common/register.rb → base/constant.rb} +11 -8
  22. data/lib/orgasm/base/instruction.rb +41 -0
  23. data/lib/orgasm/{common/instruction.rb → base/register.rb} +8 -8
  24. data/lib/orgasm/base/unknown.rb +36 -0
  25. data/lib/orgasm/disassembler.rb +25 -22
  26. data/lib/orgasm/disassembler/decoder.rb +26 -20
  27. data/lib/orgasm/{common/extensions.rb → extensions.rb} +12 -0
  28. data/lib/orgasm/generator.rb +46 -0
  29. data/lib/orgasm/generator/dsl.rb +60 -0
  30. data/lib/orgasm/piece.rb +49 -0
  31. data/lib/orgasm/styles.rb +64 -0
  32. data/lib/orgasm/styles/style.rb +55 -0
  33. data/lib/orgasm/version.rb +1 -1
  34. metadata +54 -14
  35. data/lib/orgasm/common.rb +0 -36
@@ -0,0 +1,159 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'orgasm/arch/i386/instructions/dsl/special'
21
+
22
+ module Orgasm; module I386
23
+
24
+ class DSL
25
+ def initialize (&block)
26
+ @instructions = Hash.new {|hash, key| hash[key] = []}
27
+
28
+ instance_eval &block
29
+ end
30
+
31
+ [:digit, # a digit between 0 ad 7 indicate that the ModR/M byte of the instruction
32
+ # uses only the r/m (register or memory) operand.
33
+ # The reg field contains the digit that provides an extension to the instruction's
34
+ # opcode.
35
+
36
+ :r, # indicates that the ModR/M byte of the instruction contains both a register operand
37
+ # and an r/m operand.
38
+
39
+ :cb, # 1 byte
40
+ :cw, # 2 bytes
41
+ :cd, # 4 bytes
42
+ :cp, # 6 bytes
43
+ # value following the opcode that is used to spcifya code ofset ad possibly a new
44
+ # value for the code segment register
45
+
46
+ :ib, # 1 byte
47
+ :iw, # 2 bytes
48
+ :id, # 4 bytes
49
+ # immediate operand to the instruction that follows the opcode, ModR/M bytes or
50
+ # scale-indexing bytes. The opcode determines if the operand is a signed value.
51
+
52
+ :rb, # a registercode, from 0 through 7, added to the hexadecimal byte gien at the left
53
+ :rw, # of the plus sign to form a single opcode byte.
54
+ :rd,
55
+
56
+ :i, # a number used in floating-point instructions when one of te operands is ST(i) from
57
+ # the FPU register stack. The number i (which can range from 0 to 7) is added to the
58
+ # hexadecial byte given at the left of the plus sign to form a singl opcode byte.
59
+
60
+ :rel8, # a relative address in the range from 128 byes before the end of the instruction to
61
+ # 127 bytes after the end of the instruction
62
+
63
+ :rel16, # a relative address withn the same code segment as the instruction assembled. Applies to
64
+ # instructions with an operand-size attribute of 16 bits
65
+
66
+ :rel32, # a relative address withn the same code segment as the instruction assembled. Applies to
67
+ # instructions with an operand-size attribute of 32 bits
68
+
69
+ :ptr16, # a far pointer, typically in a code segment different from that of the istuction.
70
+ # The notation of 16:16 indicates that the value of the pointer has two parts. The value
71
+ # to the left of he colon is a 16-bit selctor or value destined for the code segmet register.
72
+ # The valueto the right corresponds to the offset within the destination segmet. The ptr16:16
73
+ # symbol is used when the instruction's operand-size attribute is 16 bits; the ptr16:32 symbol
74
+ # is used wen the operand-size attribute is 32 bits.
75
+
76
+ :r8, # one of the byte gneral-purpose registers: AL, CL, DL, BL, AH, CH, DH, BH
77
+ :r16, # one of the word general-purpose registers: AX, CX, DX, BX, SP, BP, SI, DI
78
+ :r32, # one of the double-word general purpose registers: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI
79
+
80
+ :imm8, # an immediate byte value. The imm8 symbol is a signed number between -128 and +127 inclusive.
81
+ # For instructins in which imm8 is combind with a word or doublewod operand, the immediate
82
+ # value is sign-extended to for a word or doubleword. The upper byte of the word is filled
83
+ # with the topmost bit of the immediate value
84
+
85
+ :imm16, # an immediate word value used forinstructions hose operand-size attribute is 16 bits.
86
+ # This is a number between -32768 and +32767 inclusive.
87
+
88
+ :imm32, # an immediate doubleword value used for instructions whose operand-size attribute is 32 bits.
89
+ # It allows the use of a number between +2147483647 and -2147483648 inclusive.
90
+
91
+ :m, # a 16 or 32 bit operand in memory.
92
+
93
+ :m8, # a byte operand in memory, usually expressed as a variable or array name, but pointed to by
94
+ # the S:(E)SI or ES:(E)DI registers. This nomeclature is used only with the string instructions
95
+ # and the XLAT instruction.
96
+
97
+ :m16, # a word operand in memory, usually exressed as a variable or array name, but pointed to by
98
+ # the DS()SI or ES(E)DI registers. This nomenclature is used only with te string instructions.
99
+
100
+ :m32, # a doubleword operand in memory, usually expressed as a variable or array name, but pointed
101
+ # to by the DS:(E)SI or ES:(E)DI registers. This nomenclature is ued only with the string
102
+ # instructions
103
+
104
+ :m64, # a memory quadword operand in memory. This nomenclaure is used only with the CMPXCHG8B instruction.
105
+
106
+ :m128, # a mmory double quadword operand in memory. This nomenclature is used only wh the Streaming
107
+ # SIMD Extensions.
108
+
109
+ :rm8, # a byte operand thtis either the contents of a byte general-purpose register (AL, BL, CL, DL,
110
+ # AH, BH, CH and DH), or a byte from memory
111
+
112
+ :rm16, # a word general-purpose register or memoy operand used for instructions whose operan-size attribute
113
+ # is 16 bits. The word gneral-purpose regsters are: AX, bx, CX, DX, SP, BP, SI and DI.
114
+ # The contents of memory are found at the address provided by the effective address computation.
115
+
116
+ :rm32, # a doubleword general-purpose register or memory operand used for instructions whose operand-size
117
+ # attribute is 32 bits. The doubleword general-purpose registers are: EAX, EBX, ECX, EDX, ESP,
118
+ # EBP ESI and EDI. The contents of memory are found at the address provided by the effective
119
+ # address computation
120
+
121
+ :mm, # an MMX™ technology register
122
+ :mm0,
123
+ :mm1,
124
+ :mm2,
125
+ :mm3,
126
+ :mm4,
127
+ :mm5,
128
+ :mm7,
129
+
130
+ :xmm, # a SIMD floating-point register.
131
+ :xmm0,
132
+ :xmm1,
133
+ :xmm2,
134
+ :xmm3,
135
+ :xmm4,
136
+ :xmm5,
137
+ :xmm7,
138
+
139
+ :al, :cl, :dl, :bl, :ah, :ch, :dh, :bh,
140
+ :ax, :cx, :dx, :bx, :sp, :bp, :si, :di,
141
+ :eax, :ecx, :edx, :ebx, :esp, :ebp, :esi, :edi
142
+ ].each {|name|
143
+ value = Special.new(name)
144
+
145
+ define_method name do
146
+ value
147
+ end
148
+ }
149
+
150
+ def method_missing (id, *args)
151
+ @instructions[id.upcase].insert(-1, *args)
152
+ end
153
+
154
+ def to_hash
155
+ @instructions
156
+ end
157
+ end
158
+
159
+ end; end
@@ -0,0 +1,75 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Orgasm; module I386
21
+
22
+ class Special
23
+ class Operator
24
+ attr_reader :first, :second
25
+
26
+ def initialize (first, second)
27
+ @first = first
28
+ @second = second
29
+ end
30
+ end
31
+
32
+ class Or < Operator
33
+ def to_s
34
+ "#{first}|#{second}"
35
+ end
36
+ end
37
+
38
+ class And < Operator
39
+ def to_s
40
+ "#{first}&#{second}"
41
+ end
42
+ end
43
+
44
+ class Offset < Operator
45
+ def to_s
46
+ "#{first}:#{second}"
47
+ end
48
+ end
49
+
50
+ def initialize (value)
51
+ @value = value
52
+ end
53
+
54
+ def | (value)
55
+ Or.new(self, value)
56
+ end
57
+
58
+ def & (value)
59
+ And.new(self, value)
60
+ end
61
+
62
+ def ^ (value)
63
+ Offset.new(self, value)
64
+ end
65
+
66
+ def to_sym
67
+ @value
68
+ end
69
+
70
+ def to_s
71
+ @value.to_s
72
+ end
73
+ end
74
+
75
+ end; end
@@ -0,0 +1,50 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Orgasm; module I386
21
+
22
+ class Instructions < Hash
23
+ def registers
24
+ [:al, :cl, :dl, :bl, :ah, :ch, :dh, :bh,
25
+ :ax, :cx, :dx, :bx, :sp, :bp, :si, :di,
26
+ :eax, :ecx, :edx, :ebx, :esp, :ebp, :esi, :edi]
27
+ end
28
+
29
+ def register? (value)
30
+ return unless case value.to_s.downcase
31
+ when /^e[abcd]x$/,
32
+ /^e[bs]p$/,
33
+ /^e[sd]i$/,
34
+ /^[abcd]x$/,
35
+ /^[sb]p$/,
36
+ /^[sd]i$/,
37
+ /^[abcd][lh]$/ then true
38
+
39
+ else false
40
+ end
41
+
42
+ case value.to_s.downcase
43
+ when /^e/ then 32
44
+ when /[xpi]$/ then 16
45
+ when /[lh]$/ then 8
46
+ end
47
+ end
48
+ end
49
+
50
+ end; end
@@ -0,0 +1,70 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ style 'Intel' do |style|
21
+ style.for Register do
22
+ name.to_s.downcase
23
+ end
24
+
25
+ style.for I386::Address do
26
+ offset? ? "[#{start}#{'%+d' % to_i}]" : "0x#{to_i.to_s(16)}"
27
+ end
28
+
29
+ style.for I386::Immediate do
30
+ to_i.to_s
31
+ end
32
+
33
+ style.for I386::Instruction do
34
+ "#{name.to_s.downcase}#{
35
+ case parameters.length
36
+ when 1 then " #{destination}"
37
+ when 2 then " #{destination}, #{source}"
38
+ end
39
+ }"
40
+ end
41
+
42
+ style.for Unknown do
43
+ "???(#{to_i})"
44
+ end
45
+ end
46
+
47
+ style 'AT&T' do |style|
48
+ style.for Register do
49
+ "%#{name.to_s.downcase}"
50
+ end
51
+
52
+ style.for Address do
53
+ offset? ? "#{to_i}(#{start})" : "0x#{to_i.to_s(16)}"
54
+ end
55
+
56
+ style.for I386::Immediate do
57
+ "$#{to_i.to_s}"
58
+ end
59
+
60
+ style.for I386::Instruction do
61
+ "#{name.to_s.downcase}#{
62
+ { b: 8, w: 16, l: 32 }.key((parameters.last.size rescue parameters.first.size rescue nil))
63
+ } #{parameters.reverse.join(', ')}"
64
+ end
65
+
66
+ style.for Unknown do
67
+ "???(#{to_i})"
68
+ end
69
+
70
+ end
@@ -0,0 +1,103 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Orgasm
21
+
22
+ class Architecture
23
+ @@archs = {}
24
+
25
+ class << self
26
+ def for (name, &block)
27
+ (@@archs[name.downcase.to_sym] ||= Architecture.new(name)).do(&block)
28
+ end
29
+
30
+ alias is for
31
+ alias in for
32
+
33
+ def [] (name)
34
+ @@archs[name.downcase.to_sym]
35
+ end
36
+
37
+ def method_missing (id, *)
38
+ return self[id] if self[id]
39
+
40
+ super
41
+ end
42
+ end
43
+
44
+ attr_reader :name
45
+
46
+ def initialize (name, &block)
47
+ @name = name
48
+
49
+ self.do(&block)
50
+ end
51
+
52
+ def instructions (path=nil, &block)
53
+ return @instructions unless path or block
54
+
55
+ @instructions = if path
56
+ path = $:.each {|dir|
57
+ dir = File.join(dir, "#{path}.rb")
58
+
59
+ break dir if File.readable?(dir)
60
+ }.tap {|o|
61
+ raise LoadError, "no such file to load -- #{path}" unless o.is_a?(String)
62
+ }
63
+
64
+ instance_eval File.read(path), path, 1
65
+ else
66
+ instance_eval &block
67
+ end
68
+ end
69
+
70
+ [:disassembler, :assembler, :generator, :styles].each {|name|
71
+ define_method name do |path=nil, &block|
72
+ return instance_variable_get("@#{name}") unless path or block
73
+
74
+ instance_variable_set("@#{name}", if path
75
+ io = File.open($:.each {|dir|
76
+ dir = File.join(dir, "#{path}.rb")
77
+
78
+ break dir if File.readable?(dir)
79
+ }.tap {|o|
80
+ raise LoadError, "no such file to load -- #{path}" unless o.is_a?(String)
81
+ }, 'r')
82
+
83
+ Orgasm.const_get(name.capitalize).new(self, io)
84
+ else
85
+ Orgasm.const_get(name.capitalize).new(self, &block)
86
+ end)
87
+ end
88
+ }
89
+
90
+ def do (string=nil, &block)
91
+ if block
92
+ instance_eval &block
93
+ elsif string
94
+ instance_eval string
95
+ end
96
+ end
97
+
98
+ def to_s
99
+ @name
100
+ end
101
+ end
102
+
103
+ end