My favorites | Sign in
Project Logo
          
Repository:
Checkout | Browse | Changes | Clones |
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# This file is part of Metasm, the Ruby assembly manipulation suite
# Copyright (C) 2006-2009 Yoann GUILLOT
#
# Licence is LGPL, see LICENCE in the top-level directory


module Metasm
# root directory for metasm files
# used by some scripts, eg to find samples/dasm-plugin directory
Metasmdir = File.dirname(__FILE__)

# constant defined in the same file as another
Const_autorequire_equiv = {
'X86' => 'Ia32', 'PPC' => 'PowerPC',
'X64' => 'X86_64',
'UniversalBinary' => 'MachO', 'COFFArchive' => 'COFF',
'PTrace32' => 'LinOS', 'GNUExports' => 'LinOS',
'LoadedELF' => 'ELF', 'LoadedPE' => 'PE',
'LinuxRemoteString' => 'LinOS',
'WinAPI' => 'WinOS', 'WindowsExports' => 'WinOS',
'WindowsRemoteString' => 'WinOS', 'WinDbgAPI' => 'WinOS',
'WinDebugger' => 'WinOS',
'VirtualFile' => 'OS', 'VirtualString' => 'OS',
'GdbRemoteString' => 'GdbClient', 'GdbRemoteDebugger' => 'GdbClient',
}

Const_autorequire = {
'CPU' => ['encode', 'decode', 'render', 'main', 'exe_format/main', 'os/main'],
'Ia32' => 'ia32', 'MIPS' => 'mips', 'PowerPC' => 'ppc',
'X86_64' => 'x86_64',
'C' => ['parse_c', 'compile_c'],
'MZ' => 'exe_format/mz', 'PE' => 'exe_format/pe',
'ELF' => ['exe_format/elf_encode', 'exe_format/elf_decode'],
'COFF' => ['exe_format/coff_encode', 'exe_format/coff_decode'],
'Shellcode' => 'exe_format/shellcode', 'AutoExe' => 'exe_format/autoexe',
'AOut' => 'exe_format/a_out', 'MachO' => 'exe_format/macho',
'NDS' => 'exe_format/nds', 'XCoff' => 'exe_format/xcoff',
'Bflt' => 'exe_format/bflt',
'Gui' => 'gui',
'LinOS' => 'os/linux', 'WinOS' => 'os/windows',
'GdbClient' => 'os/remote',
'Decompiler' => 'decompile',
'DynLdr' => 'dynldr',
}

def self.const_missing(c)
cst = Const_autorequire_equiv[c.to_s] || c.to_s

files = Const_autorequire[cst]
return if not files
files = [files] if files.kind_of? ::String

files.each { |f| require ::File.join('metasm', f) }

const_get c
end

def self.require(f)
# temporarily put the current file directory in the ruby include path
if not $:.include? Metasmdir
incdir = Metasmdir
$: << incdir
end

super(f)

$:.delete incdir if incdir
end
end

# handle subclasses, nested modules etc (e.g. Metasm::PE, to avoid Metasm::PE::Ia32: const not found)
class Module
alias premetasm_const_missing const_missing
def const_missing(c)
# Object.const_missing => Module#const_missing and not the other way around
# XXX should use Module.nesting, but ruby sucks arse
# e.g. module Metasm ; module Bla ; class << self ; Ia32 ; end ; end ; end -> fail
if (name =~ /^Metasm(::|$)/ or ancestors.include? Metasm) and cst = Metasm.const_missing(c)
cst
else
premetasm_const_missing(c)
end
end
end

# load core files by default (too many classes to check for otherwise)
Metasm::CPU.class

# remove an 1.9 warning, couldn't find a compatible way...
if {}.respond_to? :key
puts "using ruby1.9 workaround for Hash.index" if $DEBUG
class Hash ; alias index key end
end
Show details Hide details

Change log

54423f8bb4 by jj on Dec 17, 2009   Diff
move samples/dynldr to metasm/, add
autorequire
Go to: 
Sign in to write a code review

Older revisions

504dc791be by jj on Dec 11, 2009   Diff
exe_fmt: add CParser hooks, to handle
__attribute__((export)) and define
__ELF__ or __PE__
8e377bb4e3 by jj on Dec 10, 2009   Diff
Exefmt: add #compile_c and
#assemble(src) instance methods
d0f5bd96b4 by jj on Dec 10, 2009   Diff
move Metasmdir definition inside
module Metasm
All revisions of this file

File info

Size: 2976 bytes, 93 lines
Hosted by Google Code