./c0de/ruby/elfparse.txt

0.
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.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
 #!/usr/bin/env ruby
 
$Verbose = true
 
 
target = open(ARGV[0],'r')
 
puts ""
 
ei_mag = target.read(4)
printf("magic number:                   ")
   case ei_mag
     when "\x7f\x45\x4c\x46" :printf("%s :  alright :)",ei_mag)
     else                     printf("%s :  upps no magic!!",ei_mag)
   end
puts ""
 
ei_class = target.read(1)
printf("file's class:                    ")
   case ei_class
     when "\x1"              :print("32bit")
     when "\x2"              :print("64bit")
     else                     print("invalid")
   end
puts ""
 
ei_data = target.read(1)
printf("data encoding:                   ")
   case ei_data
     when "\x1" :print("little endian")
     when "\x2" :print("big endian")
     else        print("invalid")
   end
puts ""
 
ei_version = target.read(1)
printf("elf header version:              ")
   case ei_version
     when "\x1" :print("current")
     else        print("invalid")
   end
puts ""
 
 
ei_osabi = target.read(1)
printf("OS/ABI:                          ")
   case ei_osabi
       when "\x0"   :print("UNIX System V ABI")
       when "\x1"   :print("HP-UX operating system")
       when "\x255" :print("Standalone(embedded)application")
   end
puts ""
 
 
ei_abiversion = target.read(1)
printf("ABI Version:                     ")
   case ei_abiversion
       when "\x0"   :print("0")
       else          print("seems not valid")
   end
puts ""
 
ei_pad = target.read(7)
  printf("number of padding bytes:         %d",ei_pad.length)
puts ""
 
e_type = target.read(2)
e_type.reverse! if ei_data == "\x1"
print("object file type:         ")
   case e_type
      when "\x0\x0"   :print("       No file type")
      when "\x0\x1"   :print("       Relocatable file")
      when "\x0\x2"   :print("       Executable file")
      when "\x0\x3"   :print("       Shared object file")
      when "\x0\x4"   :print("       Core file")
      when "\xff\x00" :print("       Processor-specific (LOPROC)")
      when "\xff\xff" :print("       Processor-specific (HIPROC)")
   end
puts ""
 
e_machine = target.read(2)
e_machine.reverse! if ei_data == "\x1"
print("architecture:                ")
   case e_machine
     when "\x0\x0"  :print("    No machine")
     when "\x0\x1"  :print("    AT&T WE 32100")
     when "\x0\x2"  :print("    SPARC")
     when "\x0\x3"  :print("    Intel 80386")
     when "\x0\x4"  :print("    Motorola 68000")
     when "\x0\x5"  :print("    Motorola 88000")
     when "\x0\x7"  :print("    Intel 80860")
     when "\x0\x8"  :print("    MIPS RS3000")
   end
puts ""
 
e_version = target.read(4)
e_version.reverse! if ei_data == "\x1"
printf("object file version:             ")
   case e_version
     when "\x0\x0\x0\x1" :print("current")
     else                 print("invalid")
   end
puts ""
 
e_entry = target.read(4)
e_entry.reverse! if ei_data == "\x1"
printf("entry point:                     ")
print e_entry.unpack('H8')
puts ""
 
e_phoff = target.read(4)
e_phoff.reverse! if ei_data == "\x1"
printf("program headers start:           ")
print e_phoff.unpack('N')
print (' (bytes into file)')
puts ""
 
e_shoff = target.read(4)
e_shoff.reverse! if ei_data == "\x1"
printf("section headers start:           ")
print e_shoff.unpack('NNN')
print (' (bytes into file)')
puts ""
 
e_flags = target.read(4)
e_flags.reverse! if ei_data == "\x1"
printf("flags:                           ")
print e_flags.unpack('H2H2H2H2')
puts ""
 
e_ehsize = target.read(2)
e_ehsize.reverse! if ei_data == "\x1"
printf("This headers size:               ")
print e_ehsize.unpack('n')
puts ""
 
e_phentsize = target.read(2)
e_phentsize.reverse! if ei_data == "\x1"
printf("program headers size:            ")
print e_phentsize.unpack('n')
puts ""
 
e_phnum = target.read(2)
e_phnum.reverse! if ei_data == "\x1"
printf("number of program headers:       ")
print e_phnum.unpack('n')
puts ""
 
e_shentsize = target.read(2)
e_shentsize.reverse! if ei_data == "\x1"
printf("section headers size:            ")
print e_shentsize.unpack('n')
puts ""
 
e_shnum = target.read(2)
e_shnum.reverse! if ei_data == "\x1"
printf("number of section headers:       ")
print e_shnum.unpack('n')
puts ""
 
e_shstrndx = target.read(2)
e_shstrndx.reverse! if ei_data == "\x1"
printf("shd string table index:          ")
print e_shstrndx.unpack('n')
puts ""
puts "" 

Back