diff options
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | evaluate.c | 2 | ||||
| -rw-r--r-- | obfuscate.c | 80 | ||||
| -rw-r--r-- | symbol.c | 2 |
4 files changed, 87 insertions, 2 deletions
@@ -1,7 +1,7 @@ CC=gcc CFLAGS=-g -Wall -PROGRAMS=test-lexing test-parsing +PROGRAMS=test-lexing test-parsing obfuscate HEADERS=token.h parse.h lib.h symbol.h scope.h expression.h @@ -16,6 +16,9 @@ test-lexing: test-lexing.o $(COMMON) test-parsing: test-parsing.o $(COMMON) gcc -o $@ $< $(COMMON) +obfuscate: obfuscate.o $(COMMON) + gcc -o $@ $< $(COMMON) + evaluate.o: $(HEADERS) expression.o: $(HEADERS) lib.o: $(HEADERS) @@ -1137,7 +1137,7 @@ long long get_expression_value(struct expression *expr) case EXPR_SYMBOL: { struct symbol *sym = expr->symbol; if (!sym || !sym->ctype.base_type || sym->ctype.base_type->type != SYM_ENUM) { - warn(expr->pos, "undefined identifier '%s' in constant expression", expr->symbol_name); + warn(expr->pos, "undefined identifier '%s' in constant expression", show_ident(expr->symbol_name)); return 0; } return sym->value; diff --git a/obfuscate.c b/obfuscate.c new file mode 100644 index 00000000..a16d7238 --- /dev/null +++ b/obfuscate.c @@ -0,0 +1,80 @@ +/* + * Example trivial client program that uses the sparse library + * to tokenize, pre-process and parse a C file, and prints out + * the results. + * + * Copyright (C) 2003 Transmeta Corp, all rights reserved. + */ +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <ctype.h> +#include <unistd.h> +#include <fcntl.h> + +#include "lib.h" +#include "token.h" +#include "parse.h" +#include "symbol.h" +#include "expression.h" + +static void emit_blob(struct symbol *sym) +{ + int bit_size = sym->bit_size; + + if (bit_size & 7) + warn(sym->pos, "emitting symbol of size %d\n", bit_size); + printf("unsigned char %s[%d]\n", show_ident(sym->ident), bit_size >> 3); +} + +void emit_symbol(struct symbol *sym, void *_parent, int flags) +{ + struct symbol *ctype; + + evaluate_symbol(sym); + ctype = sym->ctype.base_type; + if (!ctype) + return; + switch (ctype->type) { + case SYM_PTR: + case SYM_ARRAY: + case SYM_STRUCT: + case SYM_UNION: + case SYM_BASETYPE: + emit_blob(sym); + return; + default: + return; + } +} + +int main(int argc, char **argv) +{ + int fd; + char *filename = argv[1]; + struct token *token; + struct symbol_list *list = NULL; + + // Initialize symbol stream first, so that we can add defines etc + init_symbols(); + + fd = open(filename, O_RDONLY); + if (fd < 0) + die("No such file: %s", argv[1]); + + // Tokenize the input stream + token = tokenize(filename, fd, NULL); + close(fd); + + // Pre-process the stream + token = preprocess(token); + + // Parse the resulting C code + translation_unit(token, &list); + + // Do type evaluation and simplify + symbol_iterate(list, emit_symbol, NULL); + + return 0; +} @@ -307,8 +307,10 @@ struct sym_init { { "__typeof", NULL, MOD_TYPEOF }, { "__typeof__", NULL, MOD_TYPEOF }, +#if 0 { "attribute", NULL, MOD_ATTRIBUTE }, { "__attribute", NULL, MOD_ATTRIBUTE }, +#endif { "__attribute__", NULL, MOD_ATTRIBUTE }, { "struct", NULL, MOD_STRUCTOF }, |
