@@ -7,11 +7,16 @@ class Header
77 IDENT = [ 0x7f , 0x45 , 0x4c , 0x46 , 0x02 , 0x01 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ] . freeze
88 IDENT_STR = IDENT . pack ( "C*" ) . freeze
99 ELF_FILE_TYPE = { NONE : 0 , REL : 1 , EXEC : 2 , DYN : 3 , CORE : 4 } . freeze
10+ ELF_TARGET_MACHINE = { AMD64 : 62 } . freeze
11+ TYPE = ELF_FILE_TYPE
12+ ITYPE = TYPE . invert . freeze
13+ MACHINE = ELF_TARGET_MACHINE
14+ IMACHINE = MACHINE . invert . freeze
1015
11- def initialize ( endian : :little , type : :rel , arc : :amd64 )
16+ def initialize ( endian : :little , type : :REL )
1217 @ident = IDENT
13- @type = num2bytes ( ELF_FILE_TYPE [ elf ( type ) ] , 2 )
14- @arch = arch ( arc )
18+ @type = num2bytes ( ELF_FILE_TYPE [ type ] , 2 )
19+ @arch = num2bytes ( 62 , 2 ) # target: e_machine EM_x86_64
1520 @version = num2bytes ( 1 , 4 )
1621 @entry = num2bytes ( 0x00 , 8 )
1722 @phoffset = num2bytes ( 0x00 , 8 )
@@ -27,8 +32,9 @@ def initialize(endian: :little, type: :rel, arc: :amd64)
2732
2833 def build = bytes . flatten . pack ( "C*" )
2934
30- def set! ( type : nil , entry : nil , phoffset : nil , shoffset : nil , shnum : nil , shstrndx : nil , phsize : nil , phnum : nil , ehsize : nil )
35+ def set! ( type : nil , arch : nil , entry : nil , phoffset : nil , shoffset : nil , shnum : nil , shstrndx : nil , phsize : nil , phnum : nil , ehsize : nil )
3136 @type = num2bytes ( type , 2 ) if check ( type , 2 )
37+ @arch = num2bytes ( arch , 2 ) if check ( arch , 2 )
3238 @entry = num2bytes ( entry , 8 ) if check ( entry , 8 )
3339 @phoffset = num2bytes ( phoffset , 8 ) if check ( phoffset , 8 )
3440 @phsize = num2bytes ( phsize , 2 ) if check ( phsize , 2 )
@@ -48,33 +54,14 @@ def shentsize = @shentsize.pack("C*").unpack1("S<")
4854 def shnum = @shnum . pack ( "C*" ) . unpack1 ( "S<" )
4955 def shstrndx = @shstrndx . pack ( "C*" ) . unpack1 ( "S<" )
5056 def shoffset = @shoffset . pack ( "C*" ) . unpack1 ( "Q<" )
57+ def type = ITYPE [ @type . pack ( "C*" ) . unpack1 ( "S<" ) ]
58+ def arch = IMACHINE [ @arch . pack ( "C*" ) . unpack1 ( "S<" ) ]
5159
52- private
53- def bytes = [
60+ private def bytes = [
5461 @ident , @type , @arch , @version , @entry , @phoffset ,
5562 @shoffset , @flags , @ehsize , @phsize , @phnum , @shentsize ,
5663 @shnum , @shstrndx
5764 ]
58-
59- def arch ( machine )
60- case machine . to_s
61- in "amd64" | "x86_64" | "x64"
62- [ 0x3e , 0x00 ]
63- end
64- end
65-
66- def elf ( type )
67- case type . to_s
68- in "relocatable" | "rel"
69- :REL
70- in "exe" | "ex" | "exec"
71- :EXEC
72- in "shared" | "share" | "dynamic" | "dyn"
73- :DYN
74- else
75- :NONE
76- end
77- end
7865 end
7966 end
8067 end
0 commit comments