diff --git a/libyasm/inttree.c b/libyasm/inttree.c index d6816afd..963276d3 100644 --- a/libyasm/inttree.c +++ b/libyasm/inttree.c @@ -58,8 +58,13 @@ ITN_create(long low, long high, void *data) { IntervalTreeNode *itn = yasm_xmalloc(sizeof(IntervalTreeNode)); itn->data = data; - itn->low = low; - itn->high = high; + if (low < high) { + itn->low = low; + itn->high = high; + } else { + itn->low = high; + itn->high = low; + } itn->maxHigh = high; return itn; } diff --git a/libyasm/section.c b/libyasm/section.c index b751087d..5a401fad 100644 --- a/libyasm/section.c +++ b/libyasm/section.c @@ -966,6 +966,10 @@ optimize_term_expand(IntervalTreeNode *node, void *d) yasm_span *span = term->span; unsigned long len_diff = optd->len_diff; + /* Don't expand inactive spans */ + if (!span->active) + return; + /* Update term length */ if (term->precbc2) { if (term->precbc->bc_index > term->precbc2->bc_index) @@ -1193,7 +1197,7 @@ yasm_object_optimize(yasm_object *object, yasm_arch *arch, /* Step 2 */ while (!STAILQ_EMPTY(&optd.Q)) { - unsigned long orig_len, len_diff; + unsigned long orig_len; span = STAILQ_FIRST(&optd.Q); STAILQ_REMOVE_HEAD(&optd.Q, linkq); orig_len = span->bc->len; @@ -1215,8 +1219,8 @@ yasm_object_optimize(yasm_object *object, yasm_arch *arch, span->active = 0; if (orig_len > span->bc->len) yasm_internal_error(N_("length decreased during an expansion")); - len_diff = span->bc->len - orig_len; - if (len_diff == 0) + optd.len_diff = span->bc->len - orig_len; + if (optd.len_diff == 0) continue; /* didn't increase in size; unusual! */ IT_enumerate(optd.itree, (long)span->bc->bc_index, (long)span->bc->bc_index, &optd, optimize_term_expand); diff --git a/modules/arch/x86/x86bc.c b/modules/arch/x86/x86bc.c index 1f5be62b..6bdc594f 100644 --- a/modules/arch/x86/x86bc.c +++ b/modules/arch/x86/x86bc.c @@ -726,6 +726,9 @@ x86_bc_jmp_expand(yasm_bytecode *bc, int span, long old_val, long new_val, return -1; } + if (jmp->op_sel == JMP_NEAR) + yasm_internal_error(N_("trying to expand an already-near jump")); + /* Upgrade to a near jump */ jmp->op_sel = JMP_NEAR; bc->len -= jmp->shortop.len + 1;