Skip to content

Commit

Permalink
Merge pull request #6 from rlane/elf3
Browse files Browse the repository at this point in the history
elf: relocate function calls
  • Loading branch information
rlane committed Sep 23, 2015
2 parents 142f501 + adbb711 commit 4cdabc3
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 46 deletions.
7 changes: 6 additions & 1 deletion test_framework/expand-testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ def writefile(name, contents):

if 'asm' in data:
writefile('asm', data['asm'])
else:
elif code:
writefile('asm', ubpf.disassembler.disassemble(code))

if 'pyelf' in data:
from test_elf import generate_elf
elf = generate_elf(data['pyelf'])
writefile('elf', elf)

if __name__ == "__main__":
main()
81 changes: 66 additions & 15 deletions test_framework/test_elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from elftools.construct import Container
from elftools.elf.constants import SH_FLAGS
import testdata
import ubpf.assembler
VM = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "vm", "test")

def template():
Expand Down Expand Up @@ -37,7 +38,7 @@ def add(name, value):
e_phentsize=0,
e_phnum=0,
e_shentsize=64,
e_shnum=4,
e_shnum=5,
e_shstrndx=2))

add('first_shdr', Container(
Expand All @@ -57,8 +58,8 @@ def add(name, value):
sh_type='SHT_PROGBITS',
sh_flags=SH_FLAGS.SHF_ALLOC|SH_FLAGS.SHF_EXECINSTR,
sh_addr=0,
sh_offset=320,
sh_size=16,
sh_offset=384,
sh_size=24,
sh_link=0,
sh_info=0,
sh_addralign=8,
Expand All @@ -69,8 +70,8 @@ def add(name, value):
sh_type='SHT_STRTAB',
sh_flags=0,
sh_addr=0,
sh_offset=336,
sh_size=23,
sh_offset=408,
sh_size=34,
sh_link=0,
sh_info=0,
sh_addralign=1,
Expand All @@ -81,22 +82,62 @@ def add(name, value):
sh_type='SHT_SYMTAB',
sh_flags=0,
sh_addr=0,
sh_offset=359,
sh_size=0,
sh_link=0,
sh_offset=442,
sh_size=48,
sh_link=2,
sh_info=0,
sh_addralign=8,
sh_entsize=24))

add("text", "\xb7\x00\x00\x00\x2a\x00\x00\x00\x95\x00\x00\x00\x00\x00\x00\x00")
add("strtab", "\0.text\0.strtab\0.symtab\0")
add("symtab", "")
add('rel_shdr', Container(
sh_name=23,
sh_type='SHT_REL',
sh_flags=0,
sh_addr=0,
sh_offset=490,
sh_size=16,
sh_link=3,
sh_info=1,
sh_addralign=8,
sh_entsize=16))

# return sqrti(42*42)
asm = """
mov r1, 1764
call 0xffffffff
exit
"""

text = ubpf.assembler.assemble(asm)

add("text", text)
add("strtab", "\0.text\0.strtab\0.symtab\0.rel\0sqrti\0")
add("first_sym", Container(
st_name=0,
st_value=0,
st_size=0,
st_info=Container(bind='STB_WEAK', type='STT_FUNC'),
st_other=Container(visibility='STV_DEFAULT'),
st_shndx=0))
add("sqrti_sym", Container(
st_name=28,
st_value=0,
st_size=0,
st_info=Container(bind='STB_WEAK', type='STT_FUNC'),
st_other=Container(visibility='STV_DEFAULT'),
st_shndx=0))
add("sqrti_rel", Container(
r_info=(1 << 32) | 2,
r_info_sym=0,
r_info_type=0,
r_offset=8))

return parts

def serialize(parts):
s = elftools.elf.structs.ELFStructs(elfclass=64)
tmp = []
offset = 0

for name in parts['order']:
part = parts[name]
Expand All @@ -105,10 +146,22 @@ def serialize(parts):
serializer = s.Elf_Ehdr.build
elif name.endswith('shdr'):
serializer = s.Elf_Shdr.build
tmp.append(serializer(part))
elif name.endswith('rel'):
serializer = s.Elf_Rel.build
elif name.endswith('sym'):
serializer = s.Elf_Sym.build
data = serializer(part)
tmp.append(data)
#sys.stderr.write("Wrote %s size %d at offset %d\n" % (name, len(data), offset))
offset += len(data)

return ''.join(tmp)

def generate_elf(pyelf):
parts = template()
exec(pyelf, parts)
return serialize(parts)

def check_datafile(filename):
"""
"""
Expand All @@ -120,9 +173,7 @@ def check_datafile(filename):
if not os.path.exists(VM):
raise SkipTest("VM not found")

parts = template()
exec(data['pyelf'], parts)
elf = serialize(parts)
elf = generate_elf(data['pyelf'])

cmd = [VM]

Expand Down
4 changes: 4 additions & 0 deletions tests/elf/bad-rel-offset.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- pyelf
sqrti_rel.r_offset = 500
-- error
Failed to load code: bad relocation offset
4 changes: 4 additions & 0 deletions tests/elf/bad-rel-strtab-index.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- pyelf
symtab_shdr.sh_link = 30
-- error
Failed to load code: bad string table section index
4 changes: 4 additions & 0 deletions tests/elf/bad-rel-symbol-index.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- pyelf
sqrti_rel.r_info = (10 << 32) | 2
-- error
Failed to load code: bad symbol index
4 changes: 4 additions & 0 deletions tests/elf/bad-rel-symbol-name.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- pyelf
sqrti_sym.st_name = 100
-- error
Failed to load code: bad symbol name
4 changes: 4 additions & 0 deletions tests/elf/bad-rel-symbol-table-section-index.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- pyelf
rel_shdr.sh_link = 30
-- error
Failed to load code: bad symbol table section index
4 changes: 4 additions & 0 deletions tests/elf/bad-rel-type.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- pyelf
sqrti_rel.r_info = (1 << 32) | 3
-- error
Failed to load code: bad relocation type
2 changes: 1 addition & 1 deletion tests/elf/bad-section-header-size.data
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- pyelf
ehdr.e_shnum = 1024
ehdr.e_shentsize = 1024
-- error
Failed to load code: bad section header offset or size
4 changes: 0 additions & 4 deletions tests/elf/rel-section.data

This file was deleted.

4 changes: 4 additions & 0 deletions tests/elf/rel-sym-not-found.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- pyelf
sqrti_sym.st_name += 1
-- error
Failed to load code: function 'qrti' not found
4 changes: 4 additions & 0 deletions tests/elf/too-many-sections.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- pyelf
ehdr.e_shnum = 1024
-- error
Failed to load code: too many sections
17 changes: 11 additions & 6 deletions vm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

DYNASM := ../dynasm

CFLAGS := -Wall -Werror -Iinc -I$(DYNASM) -O2 -g
LDLIBS := -lm

ifeq ($(COVERAGE),1)
COVERAGE_CFLAGS := -fprofile-arcs -ftest-coverage
COVERAGE_LDFLAGS := -fprofile-arcs
CFLAGS += -fprofile-arcs -ftest-coverage
LDFLAGS += -fprofile-arcs
endif

DYNASM := ../dynasm

CFLAGS := -Wall -Werror -Iinc -I$(DYNASM) -O2 -g $(COVERAGE_CFLAGS)
LDFLAGS := $(COVERAGE_LDFLAGS)
ifeq ($(ASAN),1)
CFLAGS += -fsanitize=address
LDFLAGS += -fsanitize=address
endif

all: libubpf.a test

Expand Down
8 changes: 8 additions & 0 deletions vm/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <getopt.h>
#include <errno.h>
#include <elf.h>
#include <math.h>
#include "ubpf.h"

void ubpf_set_register_offset(int x);
Expand Down Expand Up @@ -214,10 +215,17 @@ trash_registers(void)
);
}

static uint32_t
sqrti(uint32_t x)
{
return sqrt(x);
}

static void
register_functions(struct ubpf_vm *vm)
{
ubpf_register(vm, 0, "gather_bytes", gather_bytes);
ubpf_register(vm, 1, "memfrob", memfrob);
ubpf_register(vm, 2, "trash_registers", trash_registers);
ubpf_register(vm, 3, "sqrti", sqrti);
}
1 change: 1 addition & 0 deletions vm/ubpf_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ struct ubpf_vm {
};

char *ubpf_error(const char *fmt, ...);
unsigned int ubpf_lookup_registered_function(struct ubpf_vm *vm, const char *name);

#endif
Loading

0 comments on commit 4cdabc3

Please sign in to comment.