From e27467d0d2681cc379c37c3fb7ad22ab9766fb2d Mon Sep 17 00:00:00 2001 From: notaz Date: Fri, 18 Sep 2015 21:48:23 +0300 Subject: [PATCH] more 32bit fixes need 64bit numbers after all --- tools/cvt_data.c | 19 +++++++++++-------- tools/masm_tools.h | 23 +++++++++++++---------- tools/translate.c | 21 +++++++++++++-------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/tools/cvt_data.c b/tools/cvt_data.c index 09e7ebc..c71c2f8 100644 --- a/tools/cvt_data.c +++ b/tools/cvt_data.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include "my_assert.h" #include "my_str.h" @@ -388,6 +390,7 @@ int main(int argc, char *argv[]) char last_sym[32]; unsigned long val; unsigned long cnt; + uint64_t val64; const char *sym; enum dx_type type; char **pub_syms; @@ -562,7 +565,7 @@ int main(int argc, char *argv[]) if (header_mode) continue; - val = parse_number(words[1]); + val = parse_number(words[1], 0); fprintf(fout, "\t\t .align %d", align_value(val)); goto fin; } @@ -690,7 +693,7 @@ int main(int argc, char *argv[]) fprintf(fout, "%s", escape_string(word)); } else { - val = parse_number(words[w]); + val = parse_number(words[w], 0); if (val & ~0xff) aerr("bad string trailing byte?\n"); // unfortunately \xHH is unusable - gas interprets @@ -704,7 +707,7 @@ int main(int argc, char *argv[]) if (w == wordc - 2) { if (IS_START(words[w + 1], "dup(")) { - cnt = parse_number(words[w]); + cnt = parse_number(words[w], 0); p = words[w + 1] + 4; p2 = strchr(p, ')'); if (p2 == NULL) @@ -714,7 +717,7 @@ int main(int argc, char *argv[]) val = 0; if (!IS(word, "?")) - val = parse_number(word); + val = parse_number(word, 0); fprintf(fout, ".fill 0x%02lx,%d,0x%02lx", cnt, type_size(type), val); @@ -800,11 +803,11 @@ int main(int argc, char *argv[]) } } else { - val = parse_number(words[w]); - if (val < 10) - fprintf(fout, "%ld", val); + val64 = parse_number(words[w], 1); + if (val64 < 10) + fprintf(fout, "%d", (int)val64); else - fprintf(fout, "0x%lx", val); + fprintf(fout, "0x%" PRIx64, val64); } first = 0; diff --git a/tools/masm_tools.h b/tools/masm_tools.h index cee4d62..453a038 100644 --- a/tools/masm_tools.h +++ b/tools/masm_tools.h @@ -1,9 +1,12 @@ -static unsigned long parse_number(const char *number) +#include +#include + +static uint64_t parse_number(const char *number, int is64) { int len = strlen(number); const char *p = number; char *endp = NULL; - unsigned long ret; + uint64_t ret; int neg = 0; int bad; @@ -13,23 +16,23 @@ static unsigned long parse_number(const char *number) } if (len > 1 && *p == '0') p++; + + errno = 0; if (number[len - 1] == 'h') { - ret = strtoul(p, &endp, 16); + ret = strtouq(p, &endp, 16); bad = (*endp != 'h'); } else { - ret = strtoul(p, &endp, 10); + ret = strtouq(p, &endp, 10); bad = (*endp != 0); } - if (bad) - aerr("number parsing failed (%s)\n", number); -#if __SIZEOF_LONG__ > 4 + if (errno != 0 || bad) + aerr("number parsing failed (%s): %d\n", number, errno); // if this happens, callers must be fixed too - if (ret > 0xfffffffful) + if (!is64 && ret > 0xfffffffful) aerr("number too large? (%s)\n", number); -#endif if (neg) { - if (ret > 0x7fffffff) + if (!is64 && ret > 0x7fffffff) aerr("too large negative? (%s)\n", number); ret = -ret; } diff --git a/tools/translate.c b/tools/translate.c index e8b1ba9..324bc26 100644 --- a/tools/translate.c +++ b/tools/translate.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "my_assert.h" #include "my_str.h" @@ -491,7 +492,7 @@ static int parse_indmode(char *name, int *regmask, int need_c_cvt) } if ('0' <= w[0] && w[0] <= '9') { - number = parse_number(w); + number = parse_number(w, 0); printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number); continue; } @@ -587,8 +588,9 @@ static const char *parse_stack_el(const char *name, char *extra_reg, if (len < sizeof(buf) - 1) { strncpy(buf, s, len); buf[len] = 0; + errno = 0; val = strtol(buf, &endp, 16); - if (val == 0 || *endp != 0) { + if (val == 0 || *endp != 0 || errno != 0) { aerr("%s num parse fail for '%s'\n", __func__, buf); return NULL; } @@ -874,7 +876,7 @@ static int parse_operand(struct parsed_opr *opr, else if (('0' <= words[w][0] && words[w][0] <= '9') || words[w][0] == '-') { - number = parse_number(words[w]); + number = parse_number(words[w], 0); opr->type = OPT_CONST; opr->val = number; printf_number(opr->name, sizeof(opr->name), number); @@ -1750,8 +1752,9 @@ static struct parsed_equ *equ_find(struct parsed_op *po, const char *name, if (namelen <= 0) ferr(po, "equ parse failed for '%s'\n", name); + errno = 0; *extra_offs = strtol(p, &endp, 16); - if (*endp != 0) + if (*endp != 0 || errno != 0) ferr(po, "equ parse failed for '%s'\n", name); } @@ -1795,10 +1798,11 @@ static void parse_stack_access(struct parsed_op *po, p = name + 4; if (IS_START(p, "0x")) p += 2; + errno = 0; offset = strtoul(p, &endp, 16); if (name[3] == '-') offset = -offset; - if (*endp != 0) + if (*endp != 0 || errno != 0) ferr(po, "ebp- parse of '%s' failed\n", name); } else { @@ -1843,8 +1847,9 @@ static int parse_stack_esp_offset(struct parsed_op *po, // just plain offset? if (!IS_START(name, "esp+")) return -1; + errno = 0; offset = strtol(name + 4, &endp, 0); - if (endp == NULL || *endp != 0) + if (endp == NULL || *endp != 0 || errno != 0) return -1; *offset_out = offset; return 0; @@ -8991,7 +8996,7 @@ do_pending_endp: if (pd->type == OPT_OFFSET) pd->d[pd->count].u.label = strdup(words[i]); else - pd->d[pd->count].u.val = parse_number(words[i]); + pd->d[pd->count].u.val = parse_number(words[i], 0); pd->d[pd->count].bt_i = -1; pd->count++; } @@ -9170,7 +9175,7 @@ do_pending_endp: else aerr("bad lmod: '%s'\n", words[2]); - g_eqs[g_eqcnt].offset = parse_number(words[4]); + g_eqs[g_eqcnt].offset = parse_number(words[4], 0); g_eqcnt++; continue; } -- 2.39.5