/*@-compmempass@*/ yasm_expr * yasm_expr_create(yasm_expr_op op, yasm_expr__item *left, yasm_expr__item *right, unsigned long line) { yasm_expr *ptr, *sube; unsigned long z; ptr = yasm_xmalloc(sizeof(yasm_expr)); ptr->op = op; ptr->numterms = 0; ptr->terms[0].type = YASM_EXPR_NONE; ptr->terms[1].type = YASM_EXPR_NONE; if (left) { ptr->terms[0] = *left; /* structure copy */ z = (unsigned long)(left-itempool); if (z>=31) yasm_internal_error(N_("could not find expritem in pool")); itempool_used &= ~(1<<z); ptr->numterms++; /* Search downward until we find something *other* than an * IDENT, then bring it up to the current level. */ while (ptr->terms[0].type == YASM_EXPR_EXPR && ptr->terms[0].data.expn->op == YASM_EXPR_IDENT) { sube = ptr->terms[0].data.expn; ptr->terms[0] = sube->terms[0]; /* structure copy */ /*@-usereleased@*/ yasm_xfree(sube); /*@=usereleased@*/ } } else { yasm_internal_error(N_("Right side of expression must exist")); } if (right) { ptr->terms[1] = *right; /* structure copy */ z = (unsigned long)(right-itempool); if (z>=31) yasm_internal_error(N_("could not find expritem in pool")); itempool_used &= ~(1<<z); ptr->numterms++; /* Search downward until we find something *other* than an * IDENT, then bring it up to the current level. */ while (ptr->terms[1].type == YASM_EXPR_EXPR && ptr->terms[1].data.expn->op == YASM_EXPR_IDENT) { sube = ptr->terms[1].data.expn; ptr->terms[1] = sube->terms[0]; /* structure copy */ /*@-usereleased@*/ yasm_xfree(sube); /*@=usereleased@*/ } } ptr->line = line; return expr_level_op(ptr, 1, 1, 0); }
void yasm_vps_delete(yasm_valparamhead *headp) { yasm_valparam *cur, *next; cur = STAILQ_FIRST(headp); while (cur) { next = STAILQ_NEXT(cur, link); if (cur->val) yasm_xfree(cur->val); switch (cur->type) { case YASM_PARAM_ID: yasm_xfree(cur->param.id); break; case YASM_PARAM_STRING: yasm_xfree(cur->param.str); break; case YASM_PARAM_EXPR: yasm_expr_destroy(cur->param.e); break; } yasm_xfree(cur); cur = next; } STAILQ_INIT(headp); }
static void bc_incbin_destroy(void *contents) { bytecode_incbin *incbin = (bytecode_incbin *)contents; yasm_xfree(incbin->filename); yasm_expr_destroy(incbin->start); yasm_expr_destroy(incbin->maxlen); yasm_xfree(contents); }
static void x86_destroy(/*@only@*/ yasm_arch *arch) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch; unsigned int i; for (i=0; i<arch_x86->cpu_enables_size; i++) BitVector_Destroy(arch_x86->cpu_enables[i]); yasm_xfree(arch_x86->cpu_enables); yasm_xfree(arch); }
/* Free memory used by the list of arguments. */ static void cpp_destroy_args(yasm_preproc_cpp *pp) { cpp_arg_entry *arg; while ( (arg = TAILQ_FIRST(&pp->cpp_args)) ) { TAILQ_REMOVE(&pp->cpp_args, arg, entry); yasm_xfree(arg->param); yasm_xfree(arg); } }
void yasm_bc_destroy(yasm_bytecode *bc) { if (!bc) return; if (bc->callback) bc->callback->destroy(bc->contents); yasm_expr_destroy(bc->multiple); if (bc->symrecs) yasm_xfree(bc->symrecs); yasm_xfree(bc); }
void yasm_intnum_destroy(yasm_intnum *intn) { if (intn->type == INTNUM_BV) BitVector_Destroy(intn->val.bv); yasm_xfree(intn); }
void yasm_floatnum_print(const yasm_floatnum *flt, FILE *f) { unsigned char out[10]; unsigned char *str; int i; /* Internal format */ str = BitVector_to_Hex(flt->mantissa); fprintf(f, "%c %s *2^%04x\n", flt->sign?'-':'+', (char *)str, flt->exponent); yasm_xfree(str); /* 32-bit (single precision) format */ fprintf(f, "32-bit: %d: ", yasm_floatnum_get_sized(flt, out, 4, 32, 0, 0, 0)); for (i=0; i<4; i++) fprintf(f, "%02x ", out[i]); fprintf(f, "\n"); /* 64-bit (double precision) format */ fprintf(f, "64-bit: %d: ", yasm_floatnum_get_sized(flt, out, 8, 64, 0, 0, 0)); for (i=0; i<8; i++) fprintf(f, "%02x ", out[i]); fprintf(f, "\n"); /* 80-bit (extended precision) format */ fprintf(f, "80-bit: %d: ", yasm_floatnum_get_sized(flt, out, 10, 80, 0, 0, 0)); for (i=0; i<10; i++) fprintf(f, "%02x ", out[i]); fprintf(f, "\n"); }
void elf_reloc_entry_destroy(void *entry) { if (((elf_reloc_entry*)entry)->addend) yasm_intnum_destroy(((elf_reloc_entry*)entry)->addend); yasm_xfree(entry); }
static void lc3b_bc_insn_destroy(void *contents) { lc3b_insn *insn = (lc3b_insn *)contents; yasm_value_delete(&insn->imm); yasm_xfree(contents); }
static elf_symtab_entry * elf_objfmt_symtab_append(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym, elf_section_index sectidx, elf_symbol_binding bind, elf_symbol_type type, elf_symbol_vis vis, yasm_expr *size, elf_address *value, yasm_object *object) { elf_symtab_entry *entry = yasm_symrec_get_data(sym, &elf_symrec_data); if (!entry) { /*@only@*/ char *symname = yasm_symrec_get_global_name(sym, object); elf_strtab_entry *name = elf_strtab_append_str(objfmt_elf->strtab, symname); yasm_xfree(symname); entry = elf_symtab_entry_create(name, sym); yasm_symrec_add_data(sym, &elf_symrec_data, entry); } /* Only append to table if not already appended */ if (!elf_sym_in_table(entry)) elf_symtab_append_entry(objfmt_elf->elf_symtab, entry); elf_symtab_set_nonzero(entry, NULL, sectidx, bind, type, size, value); elf_sym_set_visibility(entry, vis); return entry; }
/* Bridge function to convert byte-oriented parser with line-oriented * preprocessor. */ static size_t preproc_input(yasm_parser_gas *parser_gas, /*@out@*/ YYCTYPE *buf, size_t max_size) { size_t tot=0; while (max_size > 0) { size_t n; if (!parser_gas->line) { parser_gas->line = yasm_preproc_get_line(parser_gas->preproc); if (!parser_gas->line) return tot; /* EOF */ parser_gas->linepos = parser_gas->line; parser_gas->lineleft = strlen(parser_gas->line) + 1; parser_gas->line[parser_gas->lineleft-1] = '\n'; } n = parser_gas->lineleft<max_size ? parser_gas->lineleft : max_size; strncpy((char *)buf+tot, parser_gas->linepos, n); if (n == parser_gas->lineleft) { yasm_xfree(parser_gas->line); parser_gas->line = NULL; } else { parser_gas->lineleft -= n; parser_gas->linepos += n; } tot += n; max_size -= n; } return tot; }
void yasm_linemap_destroy(yasm_linemap *linemap) { size_t i; for (i=0; i<linemap->source_info_size; i++) { if (linemap->source_info[i].source) yasm_xfree(linemap->source_info[i].source); } yasm_xfree(linemap->source_info); yasm_xfree(linemap->map_vector); if (linemap->filenames) HAMT_destroy(linemap->filenames, filename_delete_one); yasm_xfree(linemap); }
static int xdf_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d) { /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d; /*@null@*/ /*@only@*/ unsigned char *bigbuf; unsigned long size = REGULAR_OUTBUF_SIZE; int gap; assert(info != NULL); bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info, xdf_objfmt_output_value, NULL); /* Don't bother doing anything else if size ended up being 0. */ if (size == 0) { if (bigbuf) yasm_xfree(bigbuf); return 0; } info->xsd->size += size; /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { unsigned long left; yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, N_("uninitialized space: zeroing")); /* Write out in chunks */ memset(info->buf, 0, REGULAR_OUTBUF_SIZE); left = size; while (left > REGULAR_OUTBUF_SIZE) { fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); left -= REGULAR_OUTBUF_SIZE; } fwrite(info->buf, left, 1, info->f); } else { /* Output buf (or bigbuf if non-NULL) to file */ fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f); } /* If bigbuf was allocated, free it */ if (bigbuf) yasm_xfree(bigbuf); return 0; }
static void cpp_preproc_destroy(yasm_preproc *preproc) { yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc; if (pp->f) { #ifdef HAVE_POPEN if (pclose(pp->f) != 0) yasm__fatal( N_("Preprocessor exited with failure") ); #endif } cpp_destroy_args(pp); yasm_xfree(pp->filename); yasm_xfree(pp); }
/*@-globstate@*/ void yasm_floatnum_cleanup(void) { int i; /* Un-offset POT_TableP */ POT_TableP--; for (i=0; i<14; i++) { BitVector_Destroy(POT_TableN[i].f.mantissa); BitVector_Destroy(POT_TableP[i].f.mantissa); } BitVector_Destroy(POT_TableP[14].f.mantissa); yasm_xfree(POT_TableN); yasm_xfree(POT_TableP); }
static /*@only@*/ yasm_arch * x86_create(const char *machine, const char *parser, /*@out@*/ yasm_arch_create_error *error) { yasm_arch_x86 *arch_x86; unsigned int amd64_machine, address_size; *error = YASM_ARCH_CREATE_OK; if (yasm__strcasecmp(machine, "x86") == 0) { amd64_machine = 0; address_size = 32; } else if (yasm__strcasecmp(machine, "amd64") == 0) { amd64_machine = 1; address_size = 64; } else if (yasm__strcasecmp(machine, "x32") == 0) { amd64_machine = 1; address_size = 32; } else { *error = YASM_ARCH_CREATE_BAD_MACHINE; return NULL; } arch_x86 = yasm_xmalloc(sizeof(yasm_arch_x86)); arch_x86->arch.module = &yasm_x86_LTX_arch; /* default to all instructions/features enabled */ arch_x86->active_cpu = 0; arch_x86->cpu_enables_size = 1; arch_x86->cpu_enables = yasm_xmalloc(sizeof(wordptr)); arch_x86->cpu_enables[0] = BitVector_Create(64, FALSE); BitVector_Fill(arch_x86->cpu_enables[0]); arch_x86->amd64_machine = amd64_machine; arch_x86->mode_bits = 0; arch_x86->address_size = address_size; arch_x86->force_strict = 0; arch_x86->default_rel = 0; arch_x86->gas_intel_mode = 0; arch_x86->nop = X86_NOP_BASIC; if (yasm__strcasecmp(parser, "nasm") == 0) arch_x86->parser = X86_PARSER_NASM; else if (yasm__strcasecmp(parser, "tasm") == 0) arch_x86->parser = X86_PARSER_TASM; else if (yasm__strcasecmp(parser, "gas") == 0 || yasm__strcasecmp(parser, "gnu") == 0) arch_x86->parser = X86_PARSER_GAS; else { yasm_xfree(arch_x86); *error = YASM_ARCH_CREATE_BAD_PARSER; return NULL; } return (yasm_arch *)arch_x86; }
void elf_strtab_destroy(elf_strtab_head *strtab) { elf_strtab_entry *s1, *s2; if (strtab == NULL) yasm_internal_error("strtab is null"); if (STAILQ_EMPTY(strtab)) yasm_internal_error("strtab is missing initial dummy entry"); s1 = STAILQ_FIRST(strtab); while (s1 != NULL) { s2 = STAILQ_NEXT(s1, qlink); yasm_xfree(s1->str); yasm_xfree(s1); s1 = s2; } yasm_xfree(strtab); }
static char * cpp_preproc_get_line(yasm_preproc *preproc) { yasm_preproc_cpp *pp = (yasm_preproc_cpp *)preproc; int bufsize = BSIZE; char *buf, *p; if (! (pp->flags & CPP_HAS_BEEN_INVOKED) ) { pp->flags |= CPP_HAS_BEEN_INVOKED; cpp_invoke(pp); } /* Once the preprocessor has been run, we're just dealing with a normal file. */ /* Loop to ensure entire line is read (don't want to limit line length). */ buf = yasm_xmalloc((size_t)bufsize); p = buf; for (;;) { if (!fgets(p, bufsize-(p-buf), pp->f)) { if (ferror(pp->f)) { yasm_error_set(YASM_ERROR_IO, N_("error when reading from file")); yasm_errwarn_propagate(pp->errwarns, yasm_linemap_get_current(pp->cur_lm)); } break; } p += strlen(p); if (p > buf && p[-1] == '\n') break; if ((p-buf) >= bufsize) { /* Increase size of buffer */ char *oldbuf = buf; bufsize *= 2; buf = yasm_xrealloc(buf, (size_t)bufsize); p = buf + (p-oldbuf); } } if (p == buf) { /* No data; must be at EOF */ yasm_xfree(buf); return NULL; } /* Strip the line ending */ buf[strcspn(buf, "\r\n")] = '\0'; return buf; }
static void bc_align_destroy(void *contents) { bytecode_align *align = (bytecode_align *)contents; if (align->boundary) yasm_expr_destroy(align->boundary); if (align->fill) yasm_expr_destroy(align->fill); if (align->maxskip) yasm_expr_destroy(align->maxskip); yasm_xfree(contents); }
static int run_output_test(Test_Entry *test) { char *valstr = yasm__xstrdup(test->input); yasm_intnum *intn = yasm_intnum_create_hex(valstr); unsigned long size, i; unsigned char out[100]; int bad; yasm_xfree(valstr); if (test->negate) yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); size = yasm_intnum_size_leb128(intn, test->sign); if (size != test->outsize) { yasm_intnum_destroy(intn); sprintf(failmsg, "%ssigned %s%s size() bad size: expected %lu, got %lu!", test->sign?"":"un", test->negate?"-":"", test->input, test->outsize, size); return 1; } for (i=0; i<sizeof(out); i++) out[i] = 0xFF; size = yasm_intnum_get_leb128(intn, out, test->sign); if (size != test->outsize) { yasm_intnum_destroy(intn); sprintf(failmsg, "%ssigned %s%s get() bad size: expected %lu, got %lu!", test->sign?"":"un", test->negate?"-":"", test->input, test->outsize, size); return 1; } bad = 0; for (i=0; i<test->outsize && !bad; i++) { if (out[i] != test->result[i]) bad = 1; } if (bad) { yasm_intnum_destroy(intn); sprintf(failmsg, "%ssigned %s%s get() bad output!", test->sign?"":"un", test->negate?"-":"", test->input); return 1; } yasm_intnum_destroy(intn); return 0; }
static yasm_objfmt * xdf_objfmt_create(yasm_object *object) { yasm_objfmt_xdf *objfmt_xdf = yasm_xmalloc(sizeof(yasm_objfmt_xdf)); /* Only support x86 arch */ if (yasm__strcasecmp(yasm_arch_keyword(object->arch), "x86") != 0) { yasm_xfree(objfmt_xdf); return NULL; } /* Support x86 and amd64 machines of x86 arch */ if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), "x86") && yasm__strcasecmp(yasm_arch_get_machine(object->arch), "amd64")) { yasm_xfree(objfmt_xdf); return NULL; } objfmt_xdf->parse_scnum = 0; /* section numbering starts at 0 */ objfmt_xdf->objfmt.module = &yasm_xdf_LTX_objfmt; return (yasm_objfmt *)objfmt_xdf; }
static int xdf_objfmt_output_str(yasm_symrec *sym, /*@null@*/ void *d) { /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d; yasm_sym_vis vis = yasm_symrec_get_visibility(sym); assert(info != NULL); if (info->all_syms || vis != YASM_SYM_LOCAL) { /*@only@*/ char *name = yasm_symrec_get_global_name(sym, info->object); size_t len = strlen(name); fwrite(name, len+1, 1, info->f); yasm_xfree(name); } return 0; }
void yasm_intnum_print(const yasm_intnum *intn, FILE *f) { unsigned char *s; switch (intn->type) { case INTNUM_L: fprintf(f, "0x%lx", intn->val.l); break; case INTNUM_BV: s = BitVector_to_Hex(intn->val.bv); fprintf(f, "0x%s", (char *)s); yasm_xfree(s); break; } }
void elf_strtab_entry_set_str(elf_strtab_entry *entry, const char *str) { elf_strtab_entry *last; if (entry->str) yasm_xfree(entry->str); entry->str = yasm__xstrdup(str); /* Update all following indices since string length probably changes */ last = entry; entry = STAILQ_NEXT(last, qlink); while (entry) { entry->index = last->index + (unsigned long)strlen(last->str) + 1; last = entry; entry = STAILQ_NEXT(last, qlink); } }
/* Invoke the c preprocessor to generate dependency info. */ static void cpp_generate_deps(yasm_preproc_cpp *pp) { char *cmdline; cmdline = cpp_build_cmdline(pp, "-M"); #ifdef HAVE_POPEN pp->f_deps = popen(cmdline, "r"); if (!pp->f_deps) yasm__fatal( N_("Failed to execute preprocessor") ); #else yasm__fatal( N_("Cannot execute preprocessor, no popen available") ); #endif yasm_xfree(cmdline); }
static YYCTYPE * fill(yasm_parser_gas *parser_gas, YYCTYPE *cursor) { yasm_scanner *s = &parser_gas->s; int first = 0; if(!s->eof){ size_t cnt = s->tok - s->bot; if(cnt){ memmove(s->bot, s->tok, (size_t)(s->lim - s->tok)); s->tok = s->bot; s->ptr -= cnt; cursor -= cnt; s->lim -= cnt; } if (!s->bot) first = 1; if((s->top - s->lim) < BSIZE){ YYCTYPE *buf = yasm_xmalloc((size_t)(s->lim - s->bot) + BSIZE); memcpy(buf, s->tok, (size_t)(s->lim - s->tok)); s->tok = buf; s->ptr = &buf[s->ptr - s->bot]; cursor = &buf[cursor - s->bot]; s->lim = &buf[s->lim - s->bot]; s->top = &s->lim[BSIZE]; if (s->bot) yasm_xfree(s->bot); s->bot = buf; } if((cnt = preproc_input(parser_gas, s->lim, BSIZE)) == 0) { s->eof = &s->lim[cnt]; *s->eof++ = '\n'; } s->lim += cnt; if (first && parser_gas->save_input) { int i; YYCTYPE *saveline; parser_gas->save_last ^= 1; saveline = parser_gas->save_line[parser_gas->save_last]; /* save next line into cur_line */ for (i=0; i<79 && &s->tok[i] < s->lim && s->tok[i] != '\n'; i++) saveline[i] = s->tok[i]; saveline[i] = '\0'; } } return cursor; }
int yasm_dir_helper_string(void *obj, yasm_valparam *vp, unsigned long line, void *data, uintptr_t arg) { /*@dependent@*/ /*@null@*/ const char *local; char **s = (char **)data; if (*s) yasm_xfree(*s); if (!(local = yasm_vp_string(vp))) { yasm_error_set(YASM_ERROR_VALUE, N_("argument to `%s' is not a string or identifier"), vp->val); return -1; } *s = yasm__xstrdup(local); return 0; }
static char * raw_preproc_get_line(yasm_preproc *preproc) { yasm_preproc_raw *preproc_raw = (yasm_preproc_raw *)preproc; int bufsize = BSIZE; char *buf = yasm_xmalloc((size_t)bufsize); char *p; /* Loop to ensure entire line is read (don't want to limit line length). */ p = buf; for (;;) { if (!fgets(p, bufsize-(p-buf), preproc_raw->in)) { if (ferror(preproc_raw->in)) { yasm_error_set(YASM_ERROR_IO, N_("error when reading from file")); yasm_errwarn_propagate(preproc_raw->errwarns, yasm_linemap_get_current(preproc_raw->cur_lm)); } break; } p += strlen(p); if (p > buf && p[-1] == '\n') break; if ((p-buf)+1 >= bufsize) { /* Increase size of buffer */ char *oldbuf = buf; bufsize *= 2; buf = yasm_xrealloc(buf, (size_t)bufsize); p = buf + (p-oldbuf); } } if (p == buf) { /* No data; must be at EOF */ yasm_xfree(buf); return NULL; } /* Strip the line ending */ buf[strcspn(buf, "\r\n")] = '\0'; return buf; }
static int run_input_test(Test_Entry *test) { char *valstr = yasm__xstrdup(test->input); yasm_intnum *intn = yasm_intnum_create_hex(valstr); yasm_intnum *testn; unsigned long size; yasm_xfree(valstr); if (test->negate) yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); testn = yasm_intnum_create_leb128(test->result, test->sign, &size); if (size != test->outsize) { yasm_intnum_destroy(testn); yasm_intnum_destroy(intn); sprintf(failmsg, "%ssigned %s%s create() bad size: expected %lu, got %lu!", test->sign?"":"un", test->negate?"-":"", test->input, test->outsize, size); return 1; } yasm_intnum_calc(intn, YASM_EXPR_EQ, testn); if (!yasm_intnum_is_pos1(intn)) { yasm_intnum_destroy(testn); yasm_intnum_destroy(intn); sprintf(failmsg, "%ssigned %s%s create() bad output!", test->sign?"":"un", test->negate?"-":"", test->input); return 1; } yasm_intnum_destroy(testn); yasm_intnum_destroy(intn); return 0; }