From 8cecb5b2a1ef59568ba1be321cb709e10eb90b5d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 3 Oct 2005 06:49:15 +0000 Subject: [PATCH] * bytecode.c (bc_align_tobytes): Handle cases where some code fills don't exist (this happens in LC3b). * lc3barch.c (lc3b_get_fill): NOP pattern is actually all 0's. svn path=/trunk/yasm/; revision=1264 --- libyasm/bytecode.c | 25 ++++++++++++++++++++----- modules/arch/lc3b/lc3barch.c | 24 ++++++++++++------------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index bf424d36..d1ea9f15 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -786,7 +786,7 @@ bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /*@unused@*/ yasm_output_reloc_func output_reloc) { bytecode_align *align = (bytecode_align *)bc->contents; - unsigned long len, i; + unsigned long len; unsigned long boundary = yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, NULL)); @@ -814,11 +814,26 @@ bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, memset(*bufp, (int)v, len); *bufp += len; } else if (align->code_fill) { - while (len > 15) { - memcpy(*bufp, align->code_fill[15], 15); - *bufp += 15; - len -= 15; + unsigned long maxlen = 15; + while (!align->code_fill[maxlen] && maxlen>0) + maxlen--; + if (maxlen == 0) { + yasm__error(bc->line, N_("could not find any code alignment size")); + return 1; + } + + /* Fill with maximum code fill as much as possible */ + while (len > maxlen) { + memcpy(*bufp, align->code_fill[maxlen], maxlen); + *bufp += maxlen; + len -= maxlen; + } + + if (!align->code_fill[len]) { + yasm__error(bc->line, N_("invalid alignment size %d"), len); + return 1; } + /* Handle rest of code fill */ memcpy(*bufp, align->code_fill[len], len); *bufp += len; } else { diff --git a/modules/arch/lc3b/lc3barch.c b/modules/arch/lc3b/lc3barch.c index f8f26c53..6aca91e2 100644 --- a/modules/arch/lc3b/lc3barch.c +++ b/modules/arch/lc3b/lc3barch.c @@ -92,27 +92,27 @@ lc3b_parse_directive(/*@unused@*/ yasm_arch *arch, static const unsigned char ** lc3b_get_fill(const yasm_arch *arch) { - /* NOP pattern is AND r0, r0, r0 repeated */ + /* NOP pattern is all 0's per LC-3b Assembler 3.50 output */ static const char *fill[16] = { NULL, /* unused */ NULL, /* 1 - illegal; all opcodes are 2 bytes long */ - "\x50\x00", /* 4 */ + "\x00\x00", /* 4 */ NULL, /* 3 - illegal */ - "\x50\x00\x50\x00", /* 4 */ + "\x00\x00\x00\x00", /* 4 */ NULL, /* 5 - illegal */ - "\x50\x00\x50\x00\x50\x00", /* 6 */ + "\x00\x00\x00\x00\x00\x00", /* 6 */ NULL, /* 7 - illegal */ - "\x50\x00\x50\x00\x50\x00" /* 8 */ - "\x50\x00", + "\x00\x00\x00\x00\x00\x00" /* 8 */ + "\x00\x00", NULL, /* 9 - illegal */ - "\x50\x00\x50\x00\x50\x00" /* 10 */ - "\x50\x00\x50\x00", + "\x00\x00\x00\x00\x00\x00" /* 10 */ + "\x00\x00\x00\x00", NULL, /* 11 - illegal */ - "\x50\x00\x50\x00\x50\x00" /* 12 */ - "\x50\x00\x50\x00\x50\x00", + "\x00\x00\x00\x00\x00\x00" /* 12 */ + "\x00\x00\x00\x00\x00\x00", NULL, /* 13 - illegal */ - "\x50\x00\x50\x00\x50\x00" /* 14 */ - "\x50\x00\x50\x00\x50\x00\x50\x00", + "\x00\x00\x00\x00\x00\x00" /* 14 */ + "\x00\x00\x00\x00\x00\x00\x00\x00", NULL /* 15 - illegal */ }; return (const unsigned char **)fill;