diff --git a/lib/vaporware/compiler/assembler/elf/section/symtab.rb b/lib/vaporware/compiler/assembler/elf/section/symtab.rb index b316e5d..ef53e11 100644 --- a/lib/vaporware/compiler/assembler/elf/section/symtab.rb +++ b/lib/vaporware/compiler/assembler/elf/section/symtab.rb @@ -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) @@ -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 diff --git a/sig/vaporware/compiler/assembler/elf/section/symtab.rbs b/sig/vaporware/compiler/assembler/elf/section/symtab.rbs index 1d4c584..369ff34 100644 --- a/sig/vaporware/compiler/assembler/elf/section/symtab.rbs +++ b/sig/vaporware/compiler/assembler/elf/section/symtab.rbs @@ -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] @@ -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 diff --git a/test/vaporware/compiler/assembler/elf/section/test_symtab.rb b/test/vaporware/compiler/assembler/elf/section/test_symtab.rb index 01bb13b..ba296b3 100644 --- a/test/vaporware/compiler/assembler/elf/section/test_symtab.rb +++ b/test/vaporware/compiler/assembler/elf/section/test_symtab.rb @@ -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