Remove compiler warnings when building the emulator.

This forces -Wall during the build. Note that this patch doesn't
remove all warnings, but most of the remaining ones are from upstream anyway.

Change-Id: I8808d8495e99866e156ce5780d2e3c305eab491f
diff --git a/Makefile.android b/Makefile.android
index 98968b9..70904f5 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -98,6 +98,11 @@
   endif
 endif
 
+# Enable warning, except those related to missing field initializers
+# (the QEMU coding style loves using these).
+#
+MY_CFLAGS += -Wall -Wno-missing-field-initializers
+
 include $(CLEAR_VARS)
 
 ###########################################################
diff --git a/android/charmap.c b/android/charmap.c
index 553ad67..1d200e6 100644
--- a/android/charmap.c
+++ b/android/charmap.c
@@ -394,7 +394,7 @@
 */
 static int
 kcm_get_ushort_hex_val(const char* token, unsigned short* val) {
-    int hex_val = hex2int(token, strlen(token));
+    int hex_val = hex2int((const uint8_t*)token, strlen(token));
     // Make sure token format was ok and value doesn't exceed unsigned short.
     if (-1 == hex_val || 0 != (hex_val & ~0xFFFF)) {
       return -1;
diff --git a/android/config.c b/android/config.c
index 36fab11..2c37b03 100644
--- a/android/config.c
+++ b/android/config.c
@@ -14,6 +14,7 @@
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "android/config.h"
 #include "android/utils/path.h"
@@ -193,7 +194,7 @@
             s = data - 1;
 
             if(value) {
-               /* if we're looking for a value, then take anything 
+               /* if we're looking for a value, then take anything
                 * until the end of line. note that sharp signs do
                 * not start comments then. the result will be stripped
                 * from trailing whitespace.
@@ -385,7 +386,12 @@
 
         w->p += avail;
         if (w->p == w->end) {
-            write( w->fd, w->buff, w->p - w->buff );
+            int ret;
+            do {
+                ret = write( w->fd, w->buff, w->p - w->buff );
+            } while (ret < 0 && errno == EINTR);
+            if (ret < 0)
+                break;
             w->p = w->buff;
         }
     }
@@ -394,8 +400,12 @@
 static void
 writer_done( Writer*  w )
 {
-    if (w->p > w->buff)
-        write( w->fd, w->buff, w->p - w->buff );
+    if (w->p > w->buff) {
+        int ret;
+        do {
+            ret = write( w->fd, w->buff, w->p - w->buff );
+        } while (ret < 0 && errno == EINTR);
+    }
     close( w->fd );
 }
 
diff --git a/android/skin/argb.h b/android/skin/argb.h
index b3f0a6d..436a9c8 100644
--- a/android/skin/argb.h
+++ b/android/skin/argb.h
@@ -134,7 +134,7 @@
 
 typedef uint32_t    argb_t;
 
-#define  ARGB_DECL_ZERO()   argb_t     _zero = 0
+#define  ARGB_DECL_ZERO()   /* nothing */
 #define  ARGB_DECL(x)       argb_t    x##_ag, x##_rb
 #define  ARGB_DECL2(x1,x2)  argb_t    x1##_ag, x1##_rb, x2##_ag, x2##_rb
 #define  ARGB_ZERO(x)       (x##_ag = x##_rb = 0)
diff --git a/android/skin/scaler.c b/android/skin/scaler.c
index 59e212f..907c5ca 100644
--- a/android/skin/scaler.c
+++ b/android/skin/scaler.c
@@ -75,7 +75,7 @@
 #define  ARGB_SCALE_GENERIC       scale_generic
 #define  ARGB_SCALE_05_TO_10      scale_05_to_10
 #define  ARGB_SCALE_UP_BILINEAR   scale_up_bilinear
-#define  ARGB_SCALE_UP_QUICK_4x4  scale_up_quick_4x4
+/* #define  ARGB_SCALE_UP_QUICK_4x4  scale_up_quick_4x4 UNUSED */
 
 #include "android/skin/argb.h"
 
diff --git a/android/skin/trackball.c b/android/skin/trackball.c
index b18923a..6fac1cb 100644
--- a/android/skin/trackball.c
+++ b/android/skin/trackball.c
@@ -12,6 +12,7 @@
 #include "android/skin/trackball.h"
 #include "android/skin/image.h"
 #include "android/utils/system.h"
+#include "user-events.h"
 #include <math.h>
 
 /***********************************************************************/
@@ -448,7 +449,7 @@
             break;
         }
 
-        kbd_mouse_event(ddx, ddy, 1, 0);
+        user_event_mouse(ddx, ddy, 1, 0);
     }
 
     rotator_reset( rot, dx, dy );
diff --git a/android/utils/ini.c b/android/utils/ini.c
index 95bb4e3..317d233 100644
--- a/android/utils/ini.c
+++ b/android/utils/ini.c
@@ -109,8 +109,8 @@
 }
 
 void
-iniFile_getPair( IniFile*   i, 
-                    int           index, 
+iniFile_getPair( IniFile*   i,
+                    int           index,
                     const char*  *pKey,
                     const char*  *pValue )
 {
@@ -251,6 +251,7 @@
     char*        text;
     long         size;
     IniFile*     ini = NULL;
+    size_t       len;
 
     if (fp == NULL) {
         D("could not open .ini file: %s: %s",
@@ -275,8 +276,8 @@
 
     /* read the file, add a sentinel at the end of it */
     AARRAY_NEW(text, size+1);
-    fread(text, 1, size, fp);
-    text[size] = 0;
+    len = fread(text, 1, size, fp);
+    text[len] = 0;
 
     ini = iniFile_newFromMemory(text, filepath);
     AFREE(text);
diff --git a/android/utils/mapfile.c b/android/utils/mapfile.c
index c8ba8e5..102dfd8 100644
--- a/android/utils/mapfile.c
+++ b/android/utils/mapfile.c
@@ -34,8 +34,8 @@
 {
 #ifdef WIN32
     DWORD win32_share;
-    DWORD win32_desired_access;
-    DWORD win32_disposition;
+    DWORD win32_desired_access = GENERIC_READ;
+    DWORD win32_disposition = OPEN_EXISTING;
     DWORD win32_flags;
 
     /* Convert to Win32 desired access. */
diff --git a/audio/audio.c b/audio/audio.c
index aa4102b..6f107dc 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -201,6 +201,7 @@
 #endif
 };
 
+#if 0
 /* http://www.df.lth.se/~john_e/gems/gem002d.html */
 /* http://www.multi-platforms.com/Tips/PopCount.htm */
 uint32_t popcount (uint32_t u)
@@ -217,6 +218,7 @@
 {
     return popcount ((u&-u)-1);
 }
+#endif
 
 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
 #error No its not
diff --git a/audio/ossaudio.c b/audio/ossaudio.c
index f946f79..ceb81ce 100644
--- a/audio/ossaudio.c
+++ b/audio/ossaudio.c
@@ -36,6 +36,23 @@
 #define AUDIO_CAP "oss"
 #include "audio_int.h"
 
+/* http://www.df.lth.se/~john_e/gems/gem002d.html */
+/* http://www.multi-platforms.com/Tips/PopCount.htm */
+uint32_t popcount (uint32_t u)
+{
+    u = ((u&0x55555555) + ((u>>1)&0x55555555));
+    u = ((u&0x33333333) + ((u>>2)&0x33333333));
+    u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
+    u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
+    u = ( u&0x0000ffff) + (u>>16);
+    return u;
+}
+
+inline uint32_t lsbindex (uint32_t u)
+{
+    return popcount ((u&-u)-1);
+}
+
 typedef struct OSSVoiceOut {
     HWVoiceOut hw;
     void *pcm_buf;
diff --git a/block/qcow2.c b/block/qcow2.c
index aa8ff35..5ca20b2 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -71,7 +71,7 @@
 }
 
 
-/* 
+/*
  * read qcow2 extension and fill bs
  * start reading from start_offset
  * finish reading upon magic of value 0 or when end_offset reached
@@ -651,6 +651,35 @@
     return res;
 }
 
+static int write_all(int fd, const void *buff, size_t bufsize)
+{
+    int ret = 0;
+    const char *ptr = buff;
+    while (bufsize > 0) {
+        ret = write(fd, ptr, bufsize);
+        if (ret < 0) {
+            if (errno != EINTR)
+                return -1;
+        } else {
+            bufsize -= ret;
+        }
+    }
+    return 0;
+}
+
+static int lseek_to(int fd, off_t offset)
+{
+    off_t ret;
+    do {
+        ret = lseek(fd, offset, SEEK_SET);
+    } while (ret == (off_t)-1 && errno == EINTR);
+
+    if (ret == (off_t)-1)
+        return -1;
+
+    return 0;
+}
+
 static int qcow_create2(const char *filename, int64_t total_size,
                         const char *backing_file, const char *backing_format,
                         int flags, size_t cluster_size)
@@ -663,7 +692,6 @@
     QCowCreateState s1, *s = &s1;
     QCowExtension ext_bf = {0, 0};
 
-
     memset(s, 0, sizeof(*s));
 
     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
@@ -744,7 +772,8 @@
         ref_clusters * s->cluster_size);
 
     /* write all the data */
-    write(fd, &header, sizeof(header));
+    if (write_all(fd, &header, sizeof(header)) < 0)
+        goto FAIL;
     if (backing_file) {
         if (backing_format_len) {
             char zero[16];
@@ -753,29 +782,42 @@
             memset(zero, 0, sizeof(zero));
             cpu_to_be32s(&ext_bf.magic);
             cpu_to_be32s(&ext_bf.len);
-            write(fd, &ext_bf, sizeof(ext_bf));
-            write(fd, backing_format, backing_format_len);
+            if (write_all(fd, &ext_bf, sizeof(ext_bf)) < 0 ||
+                write_all(fd, backing_format, backing_format_len) < 0)
+                goto FAIL;
             if (d>0) {
-                write(fd, zero, d);
+                if (write_all(fd, zero, d) < 0)
+                    goto FAIL;
             }
         }
-        write(fd, backing_file, backing_filename_len);
+        if (write_all(fd, backing_file, backing_filename_len) < 0)
+            goto FAIL;
     }
-    lseek(fd, s->l1_table_offset, SEEK_SET);
+    if (lseek_to(fd, s->l1_table_offset) < 0)
+        goto FAIL;
+
     tmp = 0;
     for(i = 0;i < l1_size; i++) {
-        write(fd, &tmp, sizeof(tmp));
+        if (write_all(fd, &tmp, sizeof(tmp)) < 0)
+            goto FAIL;
     }
-    lseek(fd, s->refcount_table_offset, SEEK_SET);
-    write(fd, s->refcount_table, s->cluster_size);
+    if (lseek_to(fd, s->refcount_table_offset) < 0 ||
+        write_all(fd, s->refcount_table, s->cluster_size) < 0)
+        goto FAIL;
 
-    lseek(fd, s->refcount_block_offset, SEEK_SET);
-    write(fd, s->refcount_block, ref_clusters * s->cluster_size);
+    if (lseek_to(fd, s->refcount_block_offset) < 0 ||
+        write_all(fd, s->refcount_block, ref_clusters * s->cluster_size) < 0)
+        goto FAIL;
 
     qemu_free(s->refcount_table);
     qemu_free(s->refcount_block);
     close(fd);
     return 0;
+FAIL:
+    qemu_free(s->refcount_table);
+    qemu_free(s->refcount_block);
+    close(fd);
+    return -errno;
 }
 
 static int qcow_create(const char *filename, QEMUOptionParameter *options)
diff --git a/block/raw-posix.c b/block/raw-posix.c
index 11effd7..0e9e343 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -570,7 +570,7 @@
     PosixAioState *s;
     int fds[2];
     struct qemu_paioinit ai;
-  
+
     if (posix_aio_state)
         return 0;
 
@@ -836,7 +836,7 @@
 
 static int raw_create(const char *filename, QEMUOptionParameter *options)
 {
-    int fd;
+    int fd, ret;
     int64_t total_size = 0;
 
     /* Read out options */
@@ -851,8 +851,12 @@
               0644);
     if (fd < 0)
         return -EIO;
-    ftruncate(fd, total_size * 512);
+    do {
+        ret = ftruncate(fd, total_size * 512);
+    } while (ret < 0 && errno == EINTR);
     close(fd);
+    if (ret != 0)
+        return -errno;
     return 0;
 }
 
diff --git a/distrib/sdl-1.2.12/src/video/x11/SDL_x11wm.c b/distrib/sdl-1.2.12/src/video/x11/SDL_x11wm.c
index fe7e9ac..76706b3 100644
--- a/distrib/sdl-1.2.12/src/video/x11/SDL_x11wm.c
+++ b/distrib/sdl-1.2.12/src/video/x11/SDL_x11wm.c
@@ -334,7 +334,6 @@
 
 static void  set_window_pos_nolock(_THIS, int x, int y)
 {
-    XWindowAttributes  attr;
     int          xNew, yNew;
     Window       child;
     int          xAdjust = X11_wmXAdjust;
@@ -394,7 +393,6 @@
     SDL_Lock_EventThread();
     {
         Window  child;
-        Status  ret;
 
         XTranslateCoordinates( SDL_Display, WMwindow, SDL_Root, 0, 0, px, py, &child );
     }
diff --git a/elff/dwarf_defs.h b/elff/dwarf_defs.h
index 567df6a..04573e2 100644
--- a/elff/dwarf_defs.h
+++ b/elff/dwarf_defs.h
@@ -717,9 +717,9 @@
    * hopping, that all Dwarf_Abbr_DIEs for the CU will fit into it.

    */

   DwarfAbbrDieArray()

-      : count_(0),

+      : array_(&small_array_[0]),

         array_size_(ELFF_ARRAY_SIZE(small_array_)),

-        array_(&small_array_[0]) {

+        count_(0) {

   }

 

   /* Destructs DwarfAbbrDieArray instance. */

diff --git a/elff/dwarf_die.cc b/elff/dwarf_die.cc
index 2085e31..0f58cbe 100644
--- a/elff/dwarf_die.cc
+++ b/elff/dwarf_die.cc
@@ -212,7 +212,7 @@
           Elf_Xword low, high;

           while (elf_file()->get_range<Elf_Xword>(off, &low, &high) &&

                  (low != 0 || high != 0)) {

-            printf("                                %08I64X - %08I64X\n",

+            printf("                                %08" FMT_I64 "X - %08" FMT_I64 "X\n",

                    low, high);

             off += 16;

           }

diff --git a/elff/dwarf_die.h b/elff/dwarf_die.h
index 21dea37..ca2eeeb 100644
--- a/elff/dwarf_die.h
+++ b/elff/dwarf_die.h
@@ -39,8 +39,8 @@
       : die_(die),

         parent_cu_(parent_cu),

         parent_die_(parent_die),

-        prev_sibling_(NULL),

-        last_child_(NULL) {

+        last_child_(NULL),

+        prev_sibling_(NULL) {

   }

 

   /* Destructs DIEObject intance. */

diff --git a/elff/dwarf_utils.cc b/elff/dwarf_utils.cc
index 56c05a3..1d84b66 100644
--- a/elff/dwarf_utils.cc
+++ b/elff/dwarf_utils.cc
@@ -275,12 +275,12 @@
       break;
 
     case DWARF_VALUE_U64:
-      printf("XWORD)  = %I64u (x%I64X)\n", attr_value->u64,
+      printf("XWORD)  = %" FMT_I64 "u (x%" FMT_I64 "X)\n", attr_value->u64,
                                           attr_value->u64);
       break;
 
     case DWARF_VALUE_S64:
-      printf("SXWORD) = %I64d (x%I64X)\n", attr_value->s64,
+      printf("SXWORD) = %" FMT_I64 "d (x%" FMT_I64 "X)\n", attr_value->s64,
                                           attr_value->s64);
       break;
 
@@ -293,11 +293,11 @@
       break;
 
     case DWARF_VALUE_PTR64:
-      printf("PTR64)  = x%08I64X\n", attr_value->ptr64);
+      printf("PTR64)  = x%08" FMT_I64 "X\n", attr_value->ptr64);
       break;
 
     case DWARF_VALUE_BLOCK:
-      printf("BLOCK)  = [%I64u]:", attr_value->block.block_size);
+      printf("BLOCK)  = [%u]:", attr_value->block.block_size);
       for (Elf_Xword i = 0; i < attr_value->block.block_size; i++) {
         Elf_Byte prnt = *((const Elf_Byte*)attr_value->block.block_ptr + i);
         printf(" x%02X", prnt);
diff --git a/elff/elf_alloc.h b/elff/elf_alloc.h
index d76dcdb..648a1e9 100644
--- a/elff/elf_alloc.h
+++ b/elff/elf_alloc.h
@@ -152,7 +152,7 @@
    * attempts to instantiate objects of derived classes using this version

    * of operator 'new'.

    */

-  void* operator new(size_t size) {

+  void* operator new(size_t size) throw() {

     return NULL;

   }

 };

diff --git a/elff/elf_defs.h b/elff/elf_defs.h
index 39fd1b1..1eca81b 100644
--- a/elff/elf_defs.h
+++ b/elff/elf_defs.h
@@ -132,4 +132,12 @@
   return get_byte(&tmp, 0) == 0xFF;

 }

 

+/* Use in printf() statements to dump 64-bit values

+ */

+#ifdef _WIN32

+#  define FMT_I64  "I64"

+#else

+#  define FMT_I64  "ll"

+#endif

+

 #endif  // ELFF_ELF_DEFS_H_

diff --git a/elff/elf_file.cc b/elff/elf_file.cc
index 39aa371..5a6392c 100644
--- a/elff/elf_file.cc
+++ b/elff/elf_file.cc
@@ -35,16 +35,16 @@
 //=============================================================================

 

 ElfFile::ElfFile()

-    : sec_table_(NULL),

-      elf_file_path_(NULL),

-      sec_count_(0),

-      cu_count_(0),

-      last_cu_(NULL),

-      allocator_(NULL),

-      fixed_base_address_(0),

-      is_exec_(0),

+    : fixed_base_address_(0),

       elf_handle_((MapFile*)-1),

-      sec_entry_size_(0) {

+      elf_file_path_(NULL),

+      allocator_(NULL),

+      sec_table_(NULL),

+      sec_count_(0),

+      sec_entry_size_(0),

+      last_cu_(NULL),

+      cu_count_(0),

+      is_exec_(0) {

 }

 

 ElfFile::~ElfFile() {

diff --git a/hw/goldfish_audio.c b/hw/goldfish_audio.c
index c8a6712..75f65bb 100644
--- a/hw/goldfish_audio.c
+++ b/hw/goldfish_audio.c
@@ -217,7 +217,7 @@
     qemu_put_buffer(f, b->data, b->length );
 }
 
-static int
+static void
 goldfish_audio_buff_get( struct goldfish_audio_buff*  b, QEMUFile*  f )
 {
     b->address = qemu_get_be32(f);
diff --git a/hw/goldfish_events_device.c b/hw/goldfish_events_device.c
index 3f5bf0b..1f932f3 100644
--- a/hw/goldfish_events_device.c
+++ b/hw/goldfish_events_device.c
@@ -15,6 +15,7 @@
 #include "android/globals.h"  /* for android_hw */
 #include "irq.h"
 #include "user-events.h"
+#include "console.h"
 
 #define MAX_EVENTS 256*4
 
diff --git a/hw/goldfish_memlog.c b/hw/goldfish_memlog.c
index f4be28a..6024f38 100644
--- a/hw/goldfish_memlog.c
+++ b/hw/goldfish_memlog.c
@@ -23,8 +23,8 @@
 
 static uint32_t memlog_read(void *opaque, target_phys_addr_t offset)
 {
-    struct goldfish_device *dev = opaque;
-
+    (void)opaque;
+    (void)offset;
     return 0;
 }
 
@@ -34,13 +34,19 @@
 {
     char buf[128];
     struct goldfish_device *dev = opaque;
+    int ret;
 
-    info[offset / 4] = val;
+    (void)dev;
+
+    if (offset < 8*4)
+        info[offset / 4] = val;
 
     if (offset == 0) {
             /* write PID and VADDR to logfile */
-        sprintf(buf,"%08x %08x\n", info[0], info[1]);
-        write(fd, buf, strlen(buf));
+        snprintf(buf, sizeof buf, "%08x %08x\n", info[0], info[1]);
+        do {
+            ret = write(fd, buf, strlen(buf));
+        } while (ret < 0 && errno == EINTR);
     }
 }
 
@@ -69,7 +75,9 @@
     dev->size = 0x1000;
     dev->irq_count = 0;
 
-    fd = open("mem.log", /* O_CREAT | */ O_TRUNC | O_WRONLY, 0644);
+    do {
+        fd = open("mem.log", /* O_CREAT | */ O_TRUNC | O_WRONLY, 0644);
+    } while (fd < 0 && errno == EINTR);
 
     goldfish_device_add(dev, memlog_readfn, memlog_writefn, dev);
 }
diff --git a/hw/goldfish_nand.c b/hw/goldfish_nand.c
index 1570095..e222cd1 100644
--- a/hw/goldfish_nand.c
+++ b/hw/goldfish_nand.c
@@ -52,7 +52,7 @@
 typedef struct {
     char*      devname;
     size_t     devname_len;
-    char*      data;
+    uint8_t*   data;
     int        fd;
     uint32_t   flags;
     uint32_t   page_size;
@@ -274,7 +274,7 @@
     case NAND_CMD_GET_DEV_NAME:
         if(size > dev->devname_len)
             size = dev->devname_len;
-        cpu_memory_rw_debug(cpu_single_env, s->data, dev->devname, size, 1);
+        cpu_memory_rw_debug(cpu_single_env, s->data, (uint8_t*)dev->devname, size, 1);
         return size;
     case NAND_CMD_READ:
         if(addr >= dev->size)
diff --git a/hw/goldfish_trace.c b/hw/goldfish_trace.c
index a7e589d..fc338c8 100644
--- a/hw/goldfish_trace.c
+++ b/hw/goldfish_trace.c
@@ -15,8 +15,11 @@
  */
 #include "qemu_file.h"
 #include "goldfish_trace.h"
+#include "sysemu.h"
+#include "trace.h"
 #ifdef CONFIG_MEMCHECK
 #include "memcheck/memcheck.h"
+#include "memcheck/memcheck_util.h"
 #endif  // CONFIG_MEMCHECK
 
 //#define DEBUG   1
@@ -46,6 +49,8 @@
 {
     trace_dev_state *s = (trace_dev_state *)opaque;
 
+    (void)s;
+
     switch (offset >> 2) {
     case TRACE_DEV_REG_SWITCH:  // context switch, switch to pid
         if (trace_filename != NULL) {
@@ -128,7 +133,7 @@
         cmdlen = value;
         break;
     case TRACE_DEV_REG_CMDLINE:         // execve, process cmdline
-        cpu_memory_rw_debug(cpu_single_env, value, exec_arg, cmdlen, 0);
+        cpu_memory_rw_debug(cpu_single_env, value, (uint8_t*)exec_arg, cmdlen, 0);
         if (trace_filename != NULL) {
             trace_execve(exec_arg, cmdlen);
         }
@@ -351,6 +356,8 @@
 {
     trace_dev_state *s = (trace_dev_state *)opaque;
 
+    (void)s;
+
     switch (offset >> 2) {
     case TRACE_DEV_REG_ENABLE:          // tracing enable
         return tracing;
@@ -378,7 +385,6 @@
 /* initialize the trace device */
 void trace_dev_init()
 {
-    int iomemtype;
     trace_dev_state *s;
 
     s = (trace_dev_state *)qemu_mallocz(sizeof(trace_dev_state));
diff --git a/hw/goldfish_tty.c b/hw/goldfish_tty.c
index dd50efc..904a07b 100644
--- a/hw/goldfish_tty.c
+++ b/hw/goldfish_tty.c
@@ -126,8 +126,8 @@
                             if (to_write > len)
                                 to_write = len;
 
-                            cpu_memory_rw_debug(cpu_single_env, buf, temp, to_write, 0);
-                            qemu_chr_write(s->cs, temp, to_write);
+                            cpu_memory_rw_debug(cpu_single_env, buf, (uint8_t*)temp, to_write, 0);
+                            qemu_chr_write(s->cs, (const uint8_t*)temp, to_write);
                             buf += to_write;
                             len -= to_write;
                         }
diff --git a/memcheck/memcheck.c b/memcheck/memcheck.c
index 8e0a1f8..3c8194a 100644
--- a/memcheck/memcheck.c
+++ b/memcheck/memcheck.c
@@ -138,7 +138,7 @@
                     }
                     memset(align, ' ', set_align);
                     align[set_align] = '\0';
-                    printf(align);
+                    printf("%s", align);
                     if (inl[index].inlined_in_file == NULL) {
                         printf("inlined to %s in unknown location\n",
                                inl[index].routine_name);
@@ -561,7 +561,7 @@
 memcheck_guest_print_str(target_ulong str) {
     char str_copy[4096];
     memcheck_get_guest_string(str_copy, str, sizeof(str_copy));
-    printf(str_copy);
+    printf("%s", str_copy);
 }
 
 /* Validates read operations, detected in __ldx_mmu routine.
diff --git a/memcheck/memcheck_proc_management.c b/memcheck/memcheck_proc_management.c
index 4120b9f..593ba32 100644
--- a/memcheck/memcheck_proc_management.c
+++ b/memcheck/memcheck_proc_management.c
@@ -25,6 +25,7 @@
 #include "memcheck.h"
 #include "memcheck_proc_management.h"
 #include "memcheck_logging.h"
+#include "memcheck_util.h"
 
 /* Current thread id.
  * This value is updated with each call to memcheck_switch, saving here
diff --git a/memcheck/memcheck_util.c b/memcheck/memcheck_util.c
index cc4182d..5449488 100644
--- a/memcheck/memcheck_util.c
+++ b/memcheck/memcheck_util.c
@@ -102,7 +102,7 @@
      * read / write guest's memory. */
     while (buffer_size) {
         *(uint8_t*)qemu_address = ldub_user(guest_address);
-        (uint32_t)qemu_address++;
+        qemu_address = (uint8_t*)qemu_address + 1;
         guest_address++;
         buffer_size--;
     }
@@ -116,7 +116,7 @@
     while (buffer_size) {
         stb_user(guest_address, *(uint8_t*)qemu_address);
         guest_address++;
-        (uint32_t)qemu_address++;
+        qemu_address = (uint8_t*)qemu_address + 1;
         buffer_size--;
     }
 }
diff --git a/memcheck/memcheck_util.h b/memcheck/memcheck_util.h
index d4f6c8a..b75ee53 100644
--- a/memcheck/memcheck_util.h
+++ b/memcheck/memcheck_util.h
@@ -24,7 +24,7 @@
 #endif  // CONFIG_MEMCHECK
 
 #include "memcheck_common.h"
-#include "elff_api.h"
+#include "elff/elff_api.h"
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/osdep.h b/osdep.h
index 1cdc7e2..b7e8d51 100644
--- a/osdep.h
+++ b/osdep.h
@@ -96,6 +96,8 @@
 int ffs(int i);
 
 int setenv(const char *name, const char *value, int overwrite);
+int asprintf(char **sptr, char *fmt, ...);
+int vasprintf(char **sptr, char *fmt, va_list args);
 
 typedef struct {
     long tv_sec;
diff --git a/path.c b/path.c
index 0d2bf14..4fbc210 100644
--- a/path.c
+++ b/path.c
@@ -3,6 +3,9 @@
 
    The assumption is that this area does not change.
 */
+#ifdef __linux__
+#define _GNU_SOURCE 1
+#endif
 #include <sys/types.h>
 #include <sys/param.h>
 #include <dirent.h>
diff --git a/qemu-char-android.c b/qemu-char-android.c
index 7753bf9..4074ea6 100644
--- a/qemu-char-android.c
+++ b/qemu-char-android.c
@@ -546,7 +546,7 @@
         }
     }
     return len1 - len;
-#endif	
+#endif
 }
 
 #else
@@ -2023,6 +2023,7 @@
     *size = j;
 }
 
+#if 0
 static int tcp_get_msgfd(CharDriverState *chr)
 {
     TCPCharDriver *s = chr->opaque;
@@ -2030,6 +2031,7 @@
     s->msgfd = -1;
     return fd;
 }
+#endif
 
 #ifndef _WIN32
 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
diff --git a/slirp-android/misc.c b/slirp-android/misc.c
index fb8ec5f..a6063a9 100644
--- a/slirp-android/misc.c
+++ b/slirp-android/misc.c
@@ -327,11 +327,14 @@
 		/* Ooops, failed, let's tell the user why */
 		  {
 			  char buff[256];
+                          int ret;
 
 			  snprintf(buff, sizeof(buff),
                                    "Error: execvp of %s failed: %s\n",
                                    argv[0], strerror(errno));
-			  write(2, buff, strlen(buff)+1);
+			  do {
+                                ret =write(2, buff, strlen(buff)+1);
+                          } while (ret < 0 && errno == EINTR);
 		  }
 		close(0); close(1); close(2); /* XXX */
 		exit(1);
diff --git a/sockets.c b/sockets.c
index 12f925f..14e9ad6 100644
--- a/sockets.c
+++ b/sockets.c
@@ -9,6 +9,11 @@
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 */
+#ifdef __linux__ /* Recent versions of glibc only define EAI_NODATA, which is an
+                    extension to the POSIX standard, if _GNU_SOURCE is defined. */
+#  define _GNU_SOURCE 1
+#endif
+
 #include "sockets.h"
 #include "qemu-common.h"
 #include <fcntl.h>
@@ -33,14 +38,7 @@
 #  include <sys/socket.h>
 #  include <netinet/in.h>
 #  include <netinet/tcp.h>
-#  ifdef __linux__ /* Recent versions of glibc only define EAI_NODATA, which is an
-                      extension to the POSIX standard, if __USE_GNU is defined. */
-#    define __USE_GNU
-#    include <netdb.h>
-#    undef __USE_GNU
-#  else /* !__linux__ */
-#    include <netdb.h>
-#  endif /* !__linux__ */
+#  include <netdb.h>
 #  if HAVE_UNIX_SOCKETS
 #    include <sys/un.h>
 #    ifndef UNIX_PATH_MAX
@@ -133,7 +131,7 @@
     int                  unix = EINVAL;  /* generic error code */
 
 	winsock_error = WSAGetLastError();
-	
+
     for ( ; werr->string != NULL; werr++ ) {
         if (werr->winsock == winsock_error) {
             unix = werr->unix;
@@ -508,7 +506,7 @@
 #endif
 
 static int
-sock_address_to_bsd( const SockAddress*  a, sockaddr_storage*  paddress, size_t  *psize )
+sock_address_to_bsd( const SockAddress*  a, sockaddr_storage*  paddress, socklen_t  *psize )
 {
     switch (a->family) {
     case SOCKET_INET:
@@ -569,12 +567,12 @@
 }
 
 static int
-sock_address_from_bsd( SockAddress*  a, const sockaddr_storage*  from, size_t  fromlen )
+sock_address_from_bsd( SockAddress*  a, const void*  from, size_t  fromlen )
 {
-    switch (from->sa->sa_family) {
+    switch (((struct sockaddr *)from)->sa_family) {
     case AF_INET:
         {
-            struct sockaddr_in*  src = from->in;
+           const struct sockaddr_in*  src = from;
 
             if (fromlen < sizeof(*src))
                 return _set_errno(EINVAL);
@@ -588,7 +586,7 @@
 #ifdef HAVE_IN6_SOCKETS
     case AF_INET6:
         {
-            struct sockaddr_in6*  src = from->in6;
+            const struct sockaddr_in6*  src = from;
 
             if (fromlen < sizeof(*src))
                 return _set_errno(EINVAL);
@@ -603,7 +601,7 @@
 #ifdef HAVE_UNIX_SOCKETS
     case AF_LOCAL:
         {
-            struct sockaddr_un*  src = from->un;
+            const struct sockaddr_un*  src = from;
             char*                end;
 
             if (fromlen < sizeof(*src))
@@ -633,7 +631,7 @@
 {
     struct addrinfo   hints[1];
     struct addrinfo*  res;
-    int               ret;
+    int                ret;
 
     memset(hints, 0, sizeof(hints));
     hints->ai_family   = preferIn6 ? AF_INET6 : AF_UNSPEC;
@@ -760,9 +758,9 @@
             break;
 
         switch (ret) {
-#ifdef EAI_ADDRFAMILY		
-        case EAI_ADDRFAMILY: 
-#endif		
+#ifdef EAI_ADDRFAMILY
+        case EAI_ADDRFAMILY:
+#endif
         case EAI_NODATA:
             _set_errno(ENOENT);
             break;
@@ -772,12 +770,12 @@
         case EAI_AGAIN:
             _set_errno(EAGAIN);
             break;
-#ifdef EAI_SYSTEM			
+#ifdef EAI_SYSTEM
         case EAI_SYSTEM:
             if (errno == EINTR)
                 continue;
             break;
-#endif			
+#endif
         default:
             _set_errno(EINVAL);
         }
@@ -1066,14 +1064,14 @@
 #else
         int  opt  = -1;
 #endif
-        size_t  optlen = sizeof(opt);
+        socklen_t  optlen = sizeof(opt);
         ret = getsockopt(fd, domain, option, (char*)&opt, &optlen);
         if (ret == 0)
             return (int)opt;
         if (errno != EINTR)
             return defaut;
     }
-#undef OPT_CAST	
+#undef OPT_CAST
 }
 
 
@@ -1151,7 +1149,7 @@
 	return 0;
 #else
     return socket_setoption(fd, IPPROTO_IPV6, IPV6_V6ONLY, 1);
-#endif	
+#endif
 }
 
 
@@ -1486,7 +1484,7 @@
     imr.imr_interface.s_addr = htonl(INADDR_ANY);
 
     if ( setsockopt( s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
-                     (const char *)&imr, 
+                     (const char *)&imr,
                      sizeof(struct ip_mreq)) < 0 )
     {
         return _fix_errno();
@@ -1503,7 +1501,7 @@
     imr.imr_interface.s_addr = htonl(INADDR_ANY);
 
     if ( setsockopt( s, IPPROTO_IP, IP_DROP_MEMBERSHIP,
-                     (const char *)&imr, 
+                     (const char *)&imr,
                      sizeof(struct ip_mreq)) < 0 )
     {
         return _fix_errno();
diff --git a/target-arm/op_helper.c b/target-arm/op_helper.c
index 05eb558..6d8db29 100644
--- a/target-arm/op_helper.c
+++ b/target-arm/op_helper.c
@@ -67,10 +67,13 @@
 
 #if !defined(CONFIG_USER_ONLY)
 
+//#define ALIGNED_ONLY  1
+
+#if ALIGNED_ONLY == 1
 static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr);
+#endif
 
 #define MMUSUFFIX _mmu
-//#define ALIGNED_ONLY  1
 
 #define SHIFT 0
 #include "softmmu_template.h"
@@ -84,6 +87,7 @@
 #define SHIFT 3
 #include "softmmu_template.h"
 
+#if ALIGNED_ONLY == 1
 static void do_unaligned_access (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
 {
     //printf("::UNALIGNED:: addr=%lx is_write=%d is_user=%d retaddr=%p\n", addr, is_write, is_user, retaddr);
@@ -96,6 +100,7 @@
         cpu_loop_exit();
     }
 }
+#endif
 
 /* try to fill the TLB and return an exception if error. If retaddr is
    NULL, it means that the function was called in C code (i.e. not
@@ -137,7 +142,7 @@
     if (buf == NULL) return;
 
     for (index = 0; index < max; index += 1) {
-        cpu_physical_memory_read(ptr + index, buf + index, 1);
+        cpu_physical_memory_read(ptr + index, (uint8_t*)buf + index, 1);
         if (buf[index] == 0)
             break;
     }
diff --git a/target-arm/translate.c b/target-arm/translate.c
index 275356d..d298f89 100644
--- a/target-arm/translate.c
+++ b/target-arm/translate.c
@@ -5798,7 +5798,7 @@
 {
     unsigned int cond, insn, val, op1, i, shift, rm, rs, rn, rd, sh;
 #ifdef CONFIG_TRACE
-    int  ticks;
+    int  ticks = 0;
 #endif
     TCGv tmp;
     TCGv tmp2;
diff --git a/telephony/remote_call.c b/telephony/remote_call.c
index 43c9099..d7b6b17 100644
--- a/telephony/remote_call.c
+++ b/telephony/remote_call.c
@@ -79,7 +79,7 @@
 {
     char*  end;
     long   num;
-    char*  temp = number;
+    const char*  temp = number;
     int    len;
 
     len = strlen(number);
diff --git a/trace.c b/trace.c
index a96f87f..9eb8f5f 100644
--- a/trace.c
+++ b/trace.c
@@ -24,6 +24,7 @@
 #include "exec-all.h"
 #include "trace.h"
 #include "varint.h"
+#include "android/utils/path.h"
 
 TraceBB trace_bb;
 TraceInsn trace_insn;
diff --git a/trace.h b/trace.h
index f5b0233..7bf4b82 100644
--- a/trace.h
+++ b/trace.h
@@ -149,4 +149,11 @@
 extern int trace_cache_miss;
 extern int trace_all_addr;
 
+// Trace process/thread operations
+extern void trace_switch(int pid);
+extern void trace_fork(int tgid, int pid);
+extern void trace_clone(int tgid, int pid);
+extern void trace_exit(int exitcode);
+extern void trace_name(char *name);
+
 #endif /* TRACE_H */
diff --git a/usb-linux.c b/usb-linux.c
index 67e4acd..d8610e8 100644
--- a/usb-linux.c
+++ b/usb-linux.c
@@ -107,9 +107,9 @@
 
 /*
  * Control transfer state.
- * Note that 'buffer' _must_ follow 'req' field because 
+ * Note that 'buffer' _must_ follow 'req' field because
  * we need contigious buffer when we submit control URB.
- */ 
+ */
 struct ctrl_struct {
     uint16_t len;
     uint16_t offset;
@@ -193,10 +193,10 @@
     return NULL;
 }
 
-/* 
+/*
  * Async URB state.
  * We always allocate one isoc descriptor even for bulk transfers
- * to simplify allocation and casts. 
+ * to simplify allocation and casts.
  */
 typedef struct AsyncURB
 {
@@ -262,7 +262,7 @@
 
         p = aurb->packet;
 
-	dprintf("husb: async completed. aurb %p status %d alen %d\n", 
+	dprintf("husb: async completed. aurb %p status %d alen %d\n",
                 aurb, aurb->urb.status, aurb->urb.actual_length);
 
 	if (p) {
@@ -332,7 +332,7 @@
         }
         config_descr_len = dev->descr[i];
 
-	printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration); 
+	printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
 
         if (configuration < 0 || configuration == dev->descr[i + 5]) {
             configuration = dev->descr[i + 5];
@@ -454,7 +454,7 @@
     if (is_halted(s, p->devep)) {
 	ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
         if (ret < 0) {
-            dprintf("husb: failed to clear halt. ep 0x%x errno %d\n", 
+            dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
                    urb->endpoint, errno);
             return USB_RET_NAK;
         }
@@ -502,7 +502,7 @@
 {
     if (errno == ETIMEDOUT)
         return USB_RET_NAK;
-    else 
+    else
         return USB_RET_STALL;
 }
 
@@ -518,12 +518,12 @@
     usb_host_release_interfaces(s);
 
     int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
- 
+
     dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
-    
+
     if (ret < 0)
         return ctrl_error();
- 
+
     usb_host_claim_interfaces(s, config);
     return 0;
 }
@@ -536,10 +536,10 @@
     si.interface  = iface;
     si.altsetting = alt;
     ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
-    
-    dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n", 
+
+    dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
     	iface, alt, ret, errno);
-    
+
     if (ret < 0)
         return ctrl_error();
 
@@ -553,7 +553,7 @@
     AsyncURB *aurb;
     int ret, value, index;
 
-    /* 
+    /*
      * Process certain standard device requests.
      * These are infrequent and are processed synchronously.
      */
@@ -561,7 +561,7 @@
     index = le16_to_cpu(s->ctrl.req.wIndex);
 
     dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
-        s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index, 
+        s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
         s->ctrl.len);
 
     if (s->ctrl.req.bRequestType == 0) {
@@ -584,12 +584,12 @@
     aurb->hdev   = s;
     aurb->packet = p;
 
-    /* 
+    /*
      * Setup ctrl transfer.
      *
      * s->ctrl is layed out such that data buffer immediately follows
      * 'req' struct which is exactly what usbdevfs expects.
-     */ 
+     */
     urb = &aurb->urb;
 
     urb->type     = USBDEVFS_URB_TYPE_CONTROL;
@@ -628,7 +628,7 @@
 
     if (p->len != 8)
         return USB_RET_STALL;
- 
+
     memcpy(&s->ctrl.req, p->data, 8);
     s->ctrl.len    = le16_to_cpu(s->ctrl.req.wLength);
     s->ctrl.offset = 0;
@@ -768,7 +768,7 @@
 
     case USB_TOKEN_OUT:
         return do_token_out(s, p);
- 
+
     default:
         return USB_RET_STALL;
     }
@@ -927,10 +927,10 @@
 
     dev->fd = fd;
 
-    /* 
-     * Initial configuration is -1 which makes us claim first 
+    /*
+     * Initial configuration is -1 which makes us claim first
      * available config. We used to start with 1, which does not
-     * always work. I've seen devices where first config starts 
+     * always work. I've seen devices where first config starts
      * with 2.
      */
     if (!usb_host_claim_interfaces(dev, -1))
@@ -1018,7 +1018,7 @@
     if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
                              devname) < 0)
         return -1;
- 
+
     s = hostdev_find(bus_num, addr);
     if (s) {
         usb_device_del_addr(0, s->dev.addr);
@@ -1027,7 +1027,7 @@
 
     return -1;
 }
- 
+
 static int get_tag_value(char *buf, int buf_size,
                          const char *str, const char *tag,
                          const char *stopchars)
@@ -1159,9 +1159,8 @@
              device_file);
     f = fopen(filename, "r");
     if (f) {
-        fgets(line, line_size, f);
+        ret = (fgets(line, line_size, f) != NULL);
         fclose(f);
-        ret = 1;
     } else {
         monitor_printf(mon, "husb: could not open %s\n", filename);
     }
@@ -1406,7 +1405,7 @@
     	p = strpbrk(p, ":.");
     	if (!p) break;
         p++;
- 
+
     	if (*p == '*')
             continue;
 
@@ -1426,7 +1425,7 @@
     return 0;
 }
 
-static int match_filter(const struct USBAutoFilter *f1, 
+static int match_filter(const struct USBAutoFilter *f1,
                         const struct USBAutoFilter *f2)
 {
     return f1->bus_num    == f2->bus_num &&
@@ -1444,13 +1443,13 @@
 
     f = qemu_mallocz(sizeof(*f));
 
-    *f = filter; 
+    *f = filter;
 
     if (!usb_auto_filter) {
         /*
          * First entry. Init and start the monitor.
          * Right now we're using timer to check for new devices.
-         * If this turns out to be too expensive we can move that into a 
+         * If this turns out to be too expensive we can move that into a
          * separate thread.
          */
 	usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
@@ -1657,7 +1656,7 @@
     if (val == -1)
         snprintf(str, size, "*");
     else
-        snprintf(str, size, "%d", val); 
+        snprintf(str, size, "%d", val);
 }
 
 static void hex2str(int val, char *str, size_t size)
diff --git a/vl-android.c b/vl-android.c
index 8b3241c..13f4dac 100644
--- a/vl-android.c
+++ b/vl-android.c
@@ -525,7 +525,7 @@
     va_end(ap);
     abort();
 }
- 
+
 /***************/
 /* ballooning */
 
@@ -2537,11 +2537,13 @@
     return 0;
 }
 
+#if 0
 static void qemu_event_increment(void)
 {
     SetEvent(qemu_event_handle);
 }
 #endif
+#endif
 
 static int cpu_can_run(CPUState *env)
 {
@@ -4948,7 +4950,10 @@
     if (pid_file && qemu_create_pidfile(pid_file) != 0) {
         if (daemonize) {
             uint8_t status = 1;
-            write(fds[1], &status, 1);
+            int ret;
+            do {
+                ret = write(fds[1], &status, 1);
+            } while (ret < 0 && errno == EINTR);
         } else
             fprintf(stderr, "Could not acquire pid file\n");
         exit(1);