This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objdumptoc.rb
139 lines (111 loc) · 2.51 KB
/
objdumptoc.rb
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env ruby
class Parser
SIZE1 = 28
SIZE2 = 28 + 4 + 32
SIZE3 = 28 + 4 + 32 + 4
attr_accessor :file, :block, :block_size
def initialize(filename)
unless filename.empty?
self.file = File.new(filename)
else
self.file = STDIN
end
self.block = Array.new
self.block_size = 0
end
def block_begin(line)
# Get the block name from label
temp = line.scan(/\w+/)
block_name = temp[1].delete('<>:')
self.block << Array.new
self.block[-1] << "char #{block_name}[]="
end
def block_end
# Insert the block size
self.block[-1][0] = block[-1][0].ljust(SIZE1)
self.block[-1][0] << '/* '
self.block[-1][0] << "#{block_size} bytes"
self.block[-1][0] = block[-1][0].ljust(SIZE2)
self.block[-1][0] << ' */'
# Reset the block size
self.block_size = 0
self.block[-1] << ';'
self.block[-1] << ''
end
def block_do(line)
temp = line.split("\t")
temp[1].strip!
temp[1] = temp[1].scan(/\w+/)
self.block[-1] << ' "'
temp[1].each do |byte|
self.block[-1][-1] << "\\x#{byte}"
self.block_size += 1
end
self.block[-1][-1] << '"'
self.block[-1][-1] = block[-1][-1].ljust(SIZE1)
self.block[-1][-1] << '/* '
# For file format aixcoff-rs6000
if temp.length == 4
temp[2] << ' '
temp[2] << temp[3]
temp.pop
end
if temp.length == 3
temp[2].strip!
temp[2] = temp[2].scan(/[$%()+,\-\.<>\w]+/)
if temp[2].length == 2
self.block[-1][-1] << temp[2][0].ljust(8)
self.block[-1][-1] << temp[2][1]
elsif temp[2].length == 3
self.block[-1][-1] << temp[2][0].ljust(8)
self.block[-1][-1] << temp[2][1]
self.block[-1][-1] << ' '
self.block[-1][-1] << temp[2][2]
else
self.block[-1][-1] << temp[2].to_s
end
end
self.block[-1][-1] = block[-1][-1].ljust(SIZE2)
self.block[-1][-1] << ' */'
end
def parse_line(line)
if line =~ /\w+ <[\.\w]+>:/
# End a previous block
unless block_size == 0
block_end
end
block_begin(line)
elsif line =~ /\w+:\t/
block_do(line)
end
end
def parse_file(file)
while (line = file.gets)
parse_line(line)
end
# End the last block
unless block_size == 0
block_end
end
end
def parse
parse_file(file)
end
def dump_all
block.each do |block|
block.each do |line|
print "#{line}\n"
end
end
end
end
unless STDIN.tty?
p = ::Parser.new('')
p.parse
p.dump_all
else
print "Tested with:\n"
print "\tGNU objdump 2.9-aix51-020209\n"
print "\tGNU objdump 2.15.92.0.2 20040927\n"
print "Usage: objdump -dM suffix <file(s)> | ruby objdumptoc.rb\n"
end