Skip to content

Commit

Permalink
set name bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
katsyoshi committed May 4, 2024
1 parent b286897 commit c33fe2f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
14 changes: 13 additions & 1 deletion lib/vaporware/compiler/assembler/elf/section/symtab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize
end

def set!(name: nil, info: nil, other: nil, shndx: nil, value: nil, size: nil)
@name = num2bytes(name, 4) if check(name, 4)
@name = name2bytes(name, 4) if check(name, 4)
@info = num2bytes(info, 1) if check(info, 4)
@other = num2bytes(other, 1) if check(other, 1)
@shndx = num2bytes(shndx, 4) if check(shndx, 4)
Expand All @@ -20,4 +20,16 @@ def set!(name: nil, info: nil, other: nil, shndx: nil, value: nil, size: nil)

private
def bytes = [@name, @info, @other, @shndx, @value, @size]
def name2bytes(name, bytes)
case name
when String
name.bytes.reverse
when Array
name[0..bytes]
when Integer
num2bytes(name, bytes)
else
[0] * bytes
end
end
end
7 changes: 4 additions & 3 deletions sig/vaporware/compiler/assembler/elf/section/symtab.rbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Vaporware::Compiler::Assembler::ELF::Section::Symtab
@name: Array[Integer]
@name: Array[Integer]?
@info: Array[Integer]
@other: Array[Integer]
@shndx: Array[Integer]
Expand All @@ -14,6 +14,7 @@ class Vaporware::Compiler::Assembler::ELF::Section::Symtab
private def desc!: (String) -> void
private def bytes: () -> Array[Array[Integer]?]
private def align!: (Array[Integer], Integer) -> void
private def num2bytes: (Integer, Integer) -> Array[Integer]
private def check: (Integer | Array[Integer], Integer) -> bool
private def num2bytes: (Integer?, Integer) -> Array[Integer]
private def check: ((String | Integer | Array[Integer])?, Integer) -> bool
private def name2bytes: ((String | Integer | Array[Integer])?, Integer) -> Array[Integer]?
end
17 changes: 14 additions & 3 deletions test/vaporware/compiler/assembler/elf/section/test_symtab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@

class Vaporware::Compiler::Assembler::ELF::Section::TestSymtab < Test::Unit::TestCase
def setup = @symtab = Vaporware::Compiler::Assembler::ELF::Section::Symtab.new
def test_set_values
binary = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x10\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding("ASCII-8BIT")
assert_equal(@symtab.build.size, binary.size)

def test_default_values
binary = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding("ASCII-8BIT")
assert_equal(@symtab.build, binary)
end

def test_main_value
binary = "\x01\x00\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding("ASCII-8BIT")
@symtab.set!(name: [1, 0, 2, 1])
assert_equal(@symtab.build, binary)
@symtab.set!(name: 16908289)
assert_equal(@symtab.build, binary)
@symtab.set!(name: "\x01\x00\x02\x01".force_encoding("ASCII-8BIT"))
assert_equal(@symtab.build, binary)
end
end

0 comments on commit c33fe2f

Please sign in to comment.