diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
commit | b6c1cf6de79035f58b512f4400db458c8401379a (patch) | |
tree | 68979db37c85b499bc384e4ac337ed1424baab51 /tools/iself | |
download | build-b6c1cf6de79035f58b512f4400db458c8401379a.zip build-b6c1cf6de79035f58b512f4400db458c8401379a.tar.gz build-b6c1cf6de79035f58b512f4400db458c8401379a.tar.bz2 |
Initial Contribution
Diffstat (limited to 'tools/iself')
-rwxr-xr-x | tools/iself/Android.mk | 23 | ||||
-rw-r--r-- | tools/iself/debug.h | 90 | ||||
-rw-r--r-- | tools/iself/iself.c | 36 |
3 files changed, 149 insertions, 0 deletions
diff --git a/tools/iself/Android.mk b/tools/iself/Android.mk new file mode 100755 index 0000000..49fabff --- /dev/null +++ b/tools/iself/Android.mk @@ -0,0 +1,23 @@ +# Copyright 2005 The Android Open Source Project +# +# Android.mk for iself +# + +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_CFLAGS += -O2 -g +LOCAL_CFLAGS += -fno-function-sections -fno-data-sections -fno-inline +LOCAL_CFLAGS += -Wall -Wno-unused-function #-Werror +LOCAL_CFLAGS += -DDEBUG + +LOCAL_C_INCLUDES:= \ + $(LOCAL_PATH)/ + +LOCAL_SRC_FILES := \ + iself.c + +LOCAL_MODULE := iself + +include $(BUILD_HOST_EXECUTABLE) diff --git a/tools/iself/debug.h b/tools/iself/debug.h new file mode 100644 index 0000000..9bbf47f --- /dev/null +++ b/tools/iself/debug.h @@ -0,0 +1,90 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#include <stdlib.h> +#include <stdio.h> + +#define unlikely(expr) __builtin_expect (expr, 0) +#define likely(expr) __builtin_expect (expr, 1) + +#ifdef DEBUG + + #define FAILIF(cond, msg...) do { \ + if (unlikely(cond)) { \ + fprintf(stderr, "%s(%d): ", __FILE__, __LINE__); \ + fprintf(stderr, ##msg); \ + exit(1); \ + } \ +} while(0) + +/* Debug enabled */ + #define ASSERT(x) do { \ + if (unlikely(!(x))) { \ + fprintf(stderr, \ + "ASSERTION FAILURE %s:%d: [%s]\n", \ + __FILE__, __LINE__, #x); \ + exit(1); \ + } \ +} while(0) + +#else + + #define FAILIF(cond, msg...) do { \ + if (unlikely(cond)) { \ + fprintf(stderr, ##msg); \ + exit(1); \ + } \ +} while(0) + +/* No debug */ + #define ASSERT(x) do { } while(0) + +#endif/* DEBUG */ + +#define FAILIF_LIBELF(cond, function) \ + FAILIF(cond, "%s(): %s\n", #function, elf_errmsg(elf_errno())); + +static inline void *MALLOC(unsigned int size) { + void *m = malloc(size); + FAILIF(NULL == m, "malloc(%d) failed!\n", size); + return m; +} + +static inline void *CALLOC(unsigned int num_entries, unsigned int entry_size) { + void *m = calloc(num_entries, entry_size); + FAILIF(NULL == m, "calloc(%d, %d) failed!\n", num_entries, entry_size); + return m; +} + +static inline void *REALLOC(void *ptr, unsigned int size) { + void *m = realloc(ptr, size); + FAILIF(NULL == m, "realloc(%p, %d) failed!\n", ptr, size); + return m; +} + +static inline void FREE(void *ptr) { + free(ptr); +} + +static inline void FREEIF(void *ptr) { + if (ptr) FREE(ptr); +} + +#define PRINT(x...) do { \ + extern int quiet_flag; \ + if(likely(!quiet_flag)) \ + fprintf(stdout, ##x); \ +} while(0) + +#define ERROR PRINT + +#define INFO(x...) do { \ + extern int verbose_flag; \ + if(unlikely(verbose_flag)) \ + fprintf(stdout, ##x); \ +} while(0) + +/* Prints a hex and ASCII dump of the selected buffer to the selected stream. */ +int dump_hex_buffer(FILE *s, void *b, size_t l, size_t elsize); + +#endif/*DEBUG_H*/ diff --git a/tools/iself/iself.c b/tools/iself/iself.c new file mode 100644 index 0000000..e634a22 --- /dev/null +++ b/tools/iself/iself.c @@ -0,0 +1,36 @@ +#include <debug.h> +#include <unistd.h> + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> +#include <errno.h> + +int +main(int argc, char **argv) +{ + char *fname; + int fd; + char magic[4]; + + argc--, argv++; + FAILIF(argc != 1, "Expecting a file name!\n"); + fname = *argv; + + fd = open(fname, O_RDONLY); + FAILIF(fd < 0, "Error opening %s for reading: %s (%d)!\n", + fname, strerror(errno), errno); + + FAILIF(4 != read(fd, magic, 4), + "Could not read first 4 bytes from %s: %s (%d)!\n", + fname, strerror(errno), errno); + + if (magic[0] != 0x7f) return 1; + if (magic[1] != 'E') return 1; + if (magic[2] != 'L') return 1; + if (magic[3] != 'F') return 1; + + return 0; +} |