|
| 1 | +module Caotral |
| 2 | + class Linker |
| 3 | + class ELF |
| 4 | + class Header |
| 5 | + include Caotral::Assembler::ELF::Utils |
| 6 | + attr_reader :entry, :phoffset, :shoffset, :shnum, :shstrndx |
| 7 | + IDENT = [0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00].freeze |
| 8 | + IDENT_STR = IDENT.pack("C*").freeze |
| 9 | + ELF_FILE_TYPE = { NONE: 0, REL: 1, EXEC: 2, DYN: 3, CORE: 4 }.freeze |
| 10 | + |
| 11 | + def initialize(endian: :little, type: :rel, arc: :amd64) |
| 12 | + @ident = IDENT |
| 13 | + @type = num2bytes(ELF_FILE_TYPE[elf(type)], 2) |
| 14 | + @arch = arch(arc) |
| 15 | + @version = num2bytes(1, 4) |
| 16 | + @entry = num2bytes(0x00, 8) |
| 17 | + @phoffset = num2bytes(0x00, 8) |
| 18 | + @shoffset = num2bytes(0x00, 8) |
| 19 | + @flags = num2bytes(0x00, 4) |
| 20 | + @ehsize = num2bytes(0x40, 2) |
| 21 | + @phsize = num2bytes(0x00, 2) |
| 22 | + @phnum = num2bytes(0x00, 2) |
| 23 | + @shentsize = num2bytes(0x40, 2) |
| 24 | + @shnum = num2bytes(0x08, 2) |
| 25 | + @shstrndx = num2bytes(0x07, 2) |
| 26 | + end |
| 27 | + |
| 28 | + def build = bytes.flatten.pack("C*") |
| 29 | + |
| 30 | + def set!(type: nil, entry: nil, phoffset: nil, shoffset: nil, shnum: nil, shstrndx: nil, phsize: nil, phnum: nil, ehsize: nil) |
| 31 | + @type = num2bytes(type, 2) if check(type, 2) |
| 32 | + @entry = num2bytes(entry, 8) if check(entry, 8) |
| 33 | + @phoffset = num2bytes(phoffset, 8) if check(phoffset, 8) |
| 34 | + @phsize = num2bytes(phsize, 2) if check(phsize, 2) |
| 35 | + @phnum = num2bytes(phnum, 2) if check(phnum, 2) |
| 36 | + @ehsize = num2bytes(ehsize, 2) if check(ehsize, 2) |
| 37 | + @shoffset = num2bytes(shoffset, 8) if check(shoffset, 8) |
| 38 | + @shnum = num2bytes(shnum, 2) if check(shnum, 2) |
| 39 | + @shstrndx = num2bytes(shstrndx, 2) if check(shstrndx, 2) |
| 40 | + self |
| 41 | + end |
| 42 | + |
| 43 | + def ehsize = get(:ehsize) |
| 44 | + def phsize = get(:phsize) |
| 45 | + def phnum = get(:phnum) |
| 46 | + def shentsize = get(:shentsize) |
| 47 | + def shnum = get(:shnum) |
| 48 | + def shstrndx = get(:shstrndx) |
| 49 | + |
| 50 | + LONG_TYPES = %w[entry phoffset shoffset].freeze |
| 51 | + INT_TYPES = %w[type version].freeze |
| 52 | + SHORT_TYPES = %w[ehsize phsize phnum shentsize shnum shstrndx].freeze |
| 53 | + CHAR_TYPES = %w[arch flags].freeze |
| 54 | + private_constant :LONG_TYPES, :INT_TYPES, :SHORT_TYPES, :CHAR_TYPES |
| 55 | + |
| 56 | + private |
| 57 | + def bytes = [ |
| 58 | + @ident, @type, @arch, @version, @entry, @phoffset, |
| 59 | + @shoffset, @flags, @ehsize, @phsize, @phnum, @shentsize, |
| 60 | + @shnum, @shstrndx |
| 61 | + ] |
| 62 | + |
| 63 | + def arch(machine) |
| 64 | + case machine.to_s |
| 65 | + in "amd64" | "x86_64" | "x64" |
| 66 | + [0x3e, 0x00] |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def elf(type) |
| 71 | + case type.to_s |
| 72 | + in "relocatable" | "rel" |
| 73 | + :REL |
| 74 | + in "exe" | "ex" | "exec" |
| 75 | + :EXEC |
| 76 | + in "shared" | "share" | "dynamic" | "dyn" |
| 77 | + :DYN |
| 78 | + else |
| 79 | + :NONE |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + def get(type) |
| 84 | + val = instance_variable_get(:"@#{type.to_s}").pack("C*") |
| 85 | + case type.to_s.downcase |
| 86 | + when *LONG_TYPES; val.unpack("Q<") |
| 87 | + when *INT_TYPES; val.unpack("L<") |
| 88 | + when *SHORT_TYPES; val.unpack("S<") |
| 89 | + when *CHAR_TYPES; val.unpack("C<") |
| 90 | + else |
| 91 | + raise "not specified: #{type}" |
| 92 | + end.first |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments