int main(int argc, char *argv[]) { Database *db; LinkedList *ll; db = db_create("project_seven"); //CREATE DATABASE project_seven; tbl_delete(db, "animals"); //DROP TABLE IF EXISTS animals; func_delete(db, "num_animals"); //DROP FUNCTION IF EXISTS num_animals; tbl_create(db, "animals"); //CREATE TABLE animals /* Capabilities of this function changed to make implementation easier. The purpose of this function is to demonstrate memory management */ func_create(db, "num_animals", num_animals); //CREATE FUNCTION num_animals() /*Creates rows in table*/ ll = ll_create(); ll_append(ll, "Cow"); tbl_insert(db, "animals", ll); //INSERT INTO animals (animal) VALUES ("Cow"); ll = ll_create(); ll_append(ll, "Dog"); tbl_insert(db, "animals", ll); //INSERT INTO animals (animal) VALUES ("Dog"); /*Printing tables, and rows created*/ db_print(db); //SHOW TABLES; tbl_print(db, "animals"); //SELECT * FROM animals; /*Deallocating memory*/ tbl_clear(db, "animals"); //DELETE FROM animals; tbl_print(db, "animals"); //SELECT * FROM animals; /*Allocating so that we can deallocate again*/ ll = ll_create(); ll_append(ll, "Pig"); tbl_insert(db, "animals", ll); //INSERT INTO animals (animal) VALUES ("Pig"); ll = ll_create(); ll_append(ll, "Cat"); tbl_insert(db, "animals", ll); //INSERT INTO animals (animal) VALUES ("Cat"); tbl_print(db, "animals"); //SELECT * FROM animals; /*Deallocating memory*/ tbl_clear(db, "animals"); //TRUNCATE TABLE animals; tbl_print(db, "animals"); //SELECT * FROM animals; tbl_delete(db, "animals"); //DROP TABLE IF EXISTS animals; func_delete(db, "num_animals"); //DROP FUNCTION IF EXISTS num_animals; db_print(db); //SHOW TABLES; db_delete(db); //DROP DATABASE IF EXISTS project_seven; return 0; }
/* * Sysv formatting helper functions. */ static void sysv_header(const char *name, Elf_Arhdr *arhdr) { text_size_total = 0; if (arhdr != NULL) (void) printf("%s (ex %s):\n", arhdr->ar_name, name); else (void) printf("%s :\n", name); tbl_new(3); tbl_append(); tbl_print("section", 0); tbl_print("size", 1); tbl_print("addr", 2); }
/* * Handles program headers except for PT_NOTE, when sysv output stlye is * choosen, prints out the segment name and length. For berkely output * style only PT_LOAD segments are handled, and text, * data, bss size is calculated for them. */ static void handle_phdr(Elf *elf, GElf_Ehdr *elfhdr, GElf_Phdr *phdr, uint32_t idx, const char *name) { uint64_t addr, size; int split; char buf[BUF_SIZE]; if (elf == NULL || elfhdr == NULL || phdr == NULL) return; size = addr = 0; split = (phdr->p_memsz > 0) && (phdr->p_filesz > 0) && (phdr->p_memsz > phdr->p_filesz); if (style == STYLE_SYSV) { (void) snprintf(buf, BUF_SIZE, "%s%d%s", name, idx, (split ? "a" : "")); tbl_append(); tbl_print(buf, 0); tbl_print_num(phdr->p_filesz, radix, 1); tbl_print_num(phdr->p_vaddr, radix, 2); text_size_total += phdr->p_filesz; if (split) { size = phdr->p_memsz - phdr->p_filesz; addr = phdr->p_vaddr + phdr->p_filesz; (void) snprintf(buf, BUF_SIZE, "%s%d%s", name, idx, "b"); text_size_total += phdr->p_memsz - phdr->p_filesz; tbl_append(); tbl_print(buf, 0); tbl_print_num(size, radix, 1); tbl_print_num(addr, radix, 2); } } else { if (phdr->p_type != PT_LOAD) return; if ((phdr->p_flags & PF_W) && !(phdr->p_flags & PF_X)) { data_size += phdr->p_filesz; if (split) data_size += phdr->p_memsz - phdr->p_filesz; } else { text_size += phdr->p_filesz; if (split) text_size += phdr->p_memsz - phdr->p_filesz; } } }
static void berkeley_footer(const char *name, const char *ar_name, const char *msg) { char buf[BUF_SIZE]; total_size = text_size + data_size + bss_size; if (show_totals) { text_size_total += text_size; bss_size_total += bss_size; data_size_total += data_size; } tbl_append(); tbl_print_num(text_size, radix, 0); tbl_print_num(data_size, radix, 1); tbl_print_num(bss_size, radix, 2); if (radix == RADIX_OCTAL) tbl_print_num(total_size, RADIX_OCTAL, 3); else tbl_print_num(total_size, RADIX_DECIMAL, 3); tbl_print_num(total_size, RADIX_HEX, 4); if (ar_name != NULL && name != NULL) (void) snprintf(buf, BUF_SIZE, "%s (%s %s)", ar_name, msg, name); else if (ar_name != NULL && name == NULL) (void) snprintf(buf, BUF_SIZE, "%s (%s)", ar_name, msg); else (void) snprintf(buf, BUF_SIZE, "%s", name); tbl_print(buf, 5); }
static inline int find_index_bin(int n, const ord_t **T, const ord_t m[n], const int from_idx, const int to_idx, enum tbl_ordering tbl_ord) { int start = from_idx, count = to_idx - from_idx, i = 0, step = 0; int iter = 0; while (count > 0) { ++iter; step = count / 2; i = start + step; if ((tbl_ord == BY_ORD && mad_mono_ord(n,T[i]) < mad_mono_ord(n,m)) || mad_mono_rcmp(n,T[i],m) < 0) { start = ++i; count -= step + 1; } else count = step; } if ( start < to_idx && mad_mono_equ(n,T[start], m)) return start; // error tbl_print(n, to_idx, (ord_t **)T); printf("monomial not found in table_by_%s: ", tbl_ord == BY_ORD ? "ord" : "var"); mad_mono_print(n,m); printf(" Searched from %d to %d\n", from_idx, to_idx); assert(NULL); return -1; }
static void tbl_print_num(uint64_t num, enum radix_style rad, int col) { char buf[BUF_SIZE]; (void) snprintf(buf, BUF_SIZE, (rad == RADIX_DECIMAL ? "%ju" : ((rad == RADIX_OCTAL) ? "0%jo" : "0x%jx")), (uintmax_t) num); tbl_print(buf, col); }
static void sysv_footer(void) { tbl_append(); tbl_print("Total", 0); tbl_print_num(text_size_total, radix, 1); tbl_flush(); putchar('\n'); }
static inline D* desc_build(int nmv, const ord_t map_ords[nmv], str_t var_nam_[nmv], int nv, const ord_t ords[nv], ord_t ko) { assert(map_ords && ords); D *d = desc_init(nmv,map_ords, nv, ko); set_var_ords(d, ords); set_var_names(d, var_nam_); make_monos(d); tbl_by_ord(d); tbl_by_var(d); // requires To tbl_set_H(d); tbl_set_L(d); build_dispatch(d); // set temps for(int i=0; i < desc_max_temps; i++) { d-> t[i] = mad_tpsa_newd (d,d->mo); d->ct[i] = mad_ctpsa_newd(d,d->mo); } // TODO: add the size of the temps to d->size #ifdef DEBUG printf("nc = %d ---- Total desc size: %d bytes\n", d->nc, d->size); #endif int err = tbl_check(d); if (err != 0) { printf("\nA= "); mad_mono_print(d->nv, d->var_ords); printf("\nH=\n"); tbl_print_H(d); printf("\nTv=\n"); tbl_print(d->nv, d->nc, d->Tv); printf("\nTo=\n"); tbl_print(d->nv, d->nc, d->To); printf("\nCheking table consistency ... %d\n", err); assert(NULL); } return d; }
code_ptr* create_code_var(char* name, symbol* params, symbol* vars) { char* reg; code_ptr* c; check_variable(name, params, vars); // First look for params reg = tbl_find_reg(name, params); if (reg == NULL) { // 'name' not found @params, look @vars reg = tbl_find_reg(name, vars); if (reg == NULL) { printf("THIS SHOULD NEVER HAPPEN: create_code_var(%s)\n", name); tbl_print(params); tbl_print(vars); exit(3); } } c = create_code(TT_VARIABLE, (code_ptr *)NULL, (code_ptr *)NULL); c->name = strdup(name); c->reg = reg; return c; }
static inline void tbl_by_ord(D *d) { assert(d && d->var_ords && d->ord2idx && d->monos); d->To = mad_malloc(d->nc * sizeof *(d->To)); assert(d->To); d->size += d->nc * sizeof *(d->To); ord_t *monos = d->monos; for (int i = 0; i < d->nc; ++i, monos += d->nv) d->To[i] = monos; #ifdef DEBUG tbl_print(d->nv, d->nc, d->To); #endif }
static void sysv_calc(Elf *elf, GElf_Ehdr *elfhdr, GElf_Shdr *shdr) { char *section_name; section_name = elf_strptr(elf, elfhdr->e_shstrndx, (size_t) shdr->sh_name); if ((shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_STRTAB || shdr->sh_type == SHT_RELA || shdr->sh_type == SHT_REL) && shdr->sh_addr == 0) return; tbl_append(); tbl_print(section_name, 0); tbl_print_num(shdr->sh_size, radix, 1); tbl_print_num(shdr->sh_addr, radix, 2); text_size_total += shdr->sh_size; }
/** Tv depends on To, as it needs to search its monomials there. But H matrix wants Tv to be built according to sorted d->var_ords. So we pretend we build it with sorted d->var_ords, whereas actually we just generate nxt_mono_by_var differently, such that the variable with the highest order varies the fastest. And H will access it based on the sorted order */ static inline void tbl_by_var(D *d) { assert(d && d->var_ords && d->sort_var && d->monos && d->ord2idx); d->Tv = mad_malloc(d->nc * sizeof *d->Tv); d->tv2to = mad_malloc(d->nc * sizeof *d->tv2to); d->to2tv = mad_malloc(d->nc * sizeof *d->to2tv); d->size += d->nc * sizeof *d->Tv; d->size += 2 * d->nc * sizeof *d->tv2to; // tv2to + to2tv int mi = 0, nv = d->nv; ord_t m[nv]; mad_mono_fill(nv, m, 0); do { int o = mad_mono_ord(nv, m); int idx = find_index_bin(nv, (const ord_t**)d->To, m, d->ord2idx[o], d->ord2idx[o+1], BY_ORD); d->tv2to[mi] = idx; d->to2tv[idx] = mi; d->Tv[mi] = d->To[idx]; ++mi; } while (mad_desc_mono_nxtbyvar(d,nv,m)); assert(mi == d->nc); #ifdef DEBUG printf("sort: "); for (int i = 0; i < d->nv; ++i) printf("%d ", d->sort_var[i]); printf("\n"); tbl_print(nv, d->nc, d->Tv); printf("tv2to=[ "); for (int i = 0; i < MIN(d->nc,50); ++i) printf("%d ", d->tv2to[i]); printf("%s]\n", d->nc > 50 ? " ... " : ""); printf("to2tv=[ "); for (int i = 0; i < MIN(d->nc,50); ++i) printf("%d ", d->to2tv[i]); printf("%s]\n", d->nc > 50 ? " ... " : ""); #endif }
/* * berkeley style output formatting helper functions. */ static void berkeley_header(void) { static int printed; text_size = data_size = bss_size = 0; if (!printed) { tbl_new(6); tbl_append(); tbl_print("text", 0); tbl_print("data", 1); tbl_print("bss", 2); if (radix == RADIX_OCTAL) tbl_print("oct", 3); else tbl_print("dec", 3); tbl_print("hex", 4); tbl_print("filename", 5); printed = 1; } }
symbol* gen_para_regs(symbol* parameters, symbol* vars) { char *registers[6] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"}; int i = 0; symbol *para_start = parameters; while (parameters != (symbol *)NULL) { if (i > 5) { not_supported("more than 6 function parameters"); } parameters->reg = strdup(registers[i++]); parameters = parameters->next; } #ifdef MY_DEBUG printf("#-- ASSIGNING PARAMETER REGISTERS\n"); tbl_print(para_start); printf("\n"); #endif reg_init(para_start); var_init(vars); return para_start; }
/* * Parse individual note entries inside a PT_NOTE segment. */ static void handle_core_note(Elf *elf, GElf_Ehdr *elfhdr, GElf_Phdr *phdr, char **cmd_line) { size_t max_size; uint64_t raw_size; GElf_Off offset; static pid_t pid; uintptr_t ver; Elf32_Nhdr *nhdr, nhdr_l; static int reg_pseudo = 0, reg2_pseudo = 0 /*, regxfp_pseudo = 0*/; char buf[BUF_SIZE], *data, *name; if (elf == NULL || elfhdr == NULL || phdr == NULL) return; data = elf_rawfile(elf, &max_size); offset = phdr->p_offset; while (data != NULL && offset < phdr->p_offset + phdr->p_filesz) { nhdr = (Elf32_Nhdr *)(uintptr_t)((char*)data + offset); memset(&nhdr_l, 0, sizeof(Elf32_Nhdr)); if (!xlatetom(elf, elfhdr, &nhdr->n_type, &nhdr_l.n_type, ELF_T_WORD, sizeof(Elf32_Word)) || !xlatetom(elf, elfhdr, &nhdr->n_descsz, &nhdr_l.n_descsz, ELF_T_WORD, sizeof(Elf32_Word)) || !xlatetom(elf, elfhdr, &nhdr->n_namesz, &nhdr_l.n_namesz, ELF_T_WORD, sizeof(Elf32_Word))) break; name = (char *)((char *)nhdr + sizeof(Elf32_Nhdr)); switch (nhdr_l.n_type) { case NT_PRSTATUS: { raw_size = 0; if (elfhdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD && nhdr_l.n_namesz == 0x8 && !strcmp(name,"FreeBSD")) { if (elfhdr->e_ident[EI_CLASS] == ELFCLASS32) { raw_size = (uint64_t)*((uint32_t *) (uintptr_t)(name + ELF_ALIGN((int32_t) nhdr_l.n_namesz, 4) + 8)); ver = (uintptr_t)NOTE_OFFSET_32(nhdr, nhdr_l.n_namesz,0); if (*((int *)ver) == 1) pid = PID32(nhdr, nhdr_l.n_namesz, 24); } else { raw_size = *((uint64_t *)(uintptr_t) (name + ELF_ALIGN((int32_t) nhdr_l.n_namesz, 8) + 16)); ver = (uintptr_t)NOTE_OFFSET_64(nhdr, nhdr_l.n_namesz,0); if (*((int *)ver) == 1) pid = PID64(nhdr, nhdr_l.n_namesz, 40); } xlatetom(elf, elfhdr, &raw_size, &raw_size, ELF_T_WORD, sizeof(uint64_t)); xlatetom(elf, elfhdr, &pid, &pid, ELF_T_WORD, sizeof(pid_t)); } if (raw_size != 0 && style == STYLE_SYSV) { (void) snprintf(buf, BUF_SIZE, "%s/%d", ".reg", pid); tbl_append(); tbl_print(buf, 0); tbl_print_num(raw_size, radix, 1); tbl_print_num(0, radix, 2); if (!reg_pseudo) { tbl_append(); tbl_print(".reg", 0); tbl_print_num(raw_size, radix, 1); tbl_print_num(0, radix, 2); reg_pseudo = 1; text_size_total += raw_size; } text_size_total += raw_size; } } break; case NT_FPREGSET: /* same as NT_PRFPREG */ if (style == STYLE_SYSV) { (void) snprintf(buf, BUF_SIZE, "%s/%d", ".reg2", pid); tbl_append(); tbl_print(buf, 0); tbl_print_num(nhdr_l.n_descsz, radix, 1); tbl_print_num(0, radix, 2); if (!reg2_pseudo) { tbl_append(); tbl_print(".reg2", 0); tbl_print_num(nhdr_l.n_descsz, radix, 1); tbl_print_num(0, radix, 2); reg2_pseudo = 1; text_size_total += nhdr_l.n_descsz; } text_size_total += nhdr_l.n_descsz; } break; #if 0 case NT_AUXV: if (style == STYLE_SYSV) { tbl_append(); tbl_print(".auxv", 0); tbl_print_num(nhdr_l.n_descsz, radix, 1); tbl_print_num(0, radix, 2); text_size_total += nhdr_l.n_descsz; } break; case NT_PRXFPREG: if (style == STYLE_SYSV) { (void) snprintf(buf, BUF_SIZE, "%s/%d", ".reg-xfp", pid); tbl_append(); tbl_print(buf, 0); tbl_print_num(nhdr_l.n_descsz, radix, 1); tbl_print_num(0, radix, 2); if (!regxfp_pseudo) { tbl_append(); tbl_print(".reg-xfp", 0); tbl_print_num(nhdr_l.n_descsz, radix, 1); tbl_print_num(0, radix, 2); regxfp_pseudo = 1; text_size_total += nhdr_l.n_descsz; } text_size_total += nhdr_l.n_descsz; } break; case NT_PSINFO: #endif case NT_PRPSINFO: { /* FreeBSD 64-bit */ if (nhdr_l.n_descsz == 0x78 && !strcmp(name,"FreeBSD")) { *cmd_line = strdup(NOTE_OFFSET_64(nhdr, nhdr_l.n_namesz, 33)); /* FreeBSD 32-bit */ } else if (nhdr_l.n_descsz == 0x6c && !strcmp(name,"FreeBSD")) { *cmd_line = strdup(NOTE_OFFSET_32(nhdr, nhdr_l.n_namesz, 25)); } /* Strip any trailing spaces */ if (*cmd_line != NULL) { char *s; s = *cmd_line + strlen(*cmd_line); while (s > *cmd_line) { if (*(s-1) != 0x20) break; s--; } *s = 0; } break; } #if 0 case NT_PSTATUS: case NT_LWPSTATUS: #endif default: break; } NEXT_NOTE(elfhdr, nhdr_l.n_descsz, nhdr_l.n_namesz, offset); } }