Tweak the host version of elfutils for MacOS.

Change-Id: I806cdda2782b00832c7d851664fdba64cce92c0e
diff --git a/host-darwin-fixup/AndroidFixup.h b/host-darwin-fixup/AndroidFixup.h
index 1342843..2235835 100644
--- a/host-darwin-fixup/AndroidFixup.h
+++ b/host-darwin-fixup/AndroidFixup.h
@@ -72,4 +72,26 @@
         return NULL;
 }
 
+/* workaround for canonicalize_file_name */
+#define canonicalize_file_name(path) realpath(path, NULL)
+
+/* workaround for open64 */
+#define open64(path, flags)     open(path, flags)
+
+/* rawmemchr */
+static inline void *rawmemchr(const void *s, int c)
+{
+    const unsigned char *ptr = s;
+    while (1) {
+        if (*ptr == c) return (void *) ptr;
+        ptr++;
+    }
+}
+
+#define strndup(str, size) strdup(str)
+
+static void tdestroy(void *root, void (*free_node)(void *nodep))
+{
+}
+
 #endif /* ANDROID_FIXUP_H */
diff --git a/host-darwin-fixup/getline.c b/host-darwin-fixup/getline.c
new file mode 100644
index 0000000..041a5ed
--- /dev/null
+++ b/host-darwin-fixup/getline.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+ssize_t getline(char **lineptr, size_t *n, FILE *stream)
+{
+    char *ptr;
+
+    ptr = fgetln(stream, n);
+
+    if (ptr == NULL) {
+        return -1;
+    }
+
+    /* Free the original ptr */
+    if (*lineptr != NULL) free(*lineptr);
+
+    /* Add one more space for '\0' */
+    size_t len = n[0] + 1;
+
+    /* Update the length */
+    n[0] = len;
+
+    /* Allocate a new buffer */
+    *lineptr = malloc(len);
+
+    /* Copy over the string */
+    memcpy(*lineptr, ptr, len-1);
+
+    /* Write the NULL character */
+    (*lineptr)[len-1] = '\0';
+
+    /* Return the length of the new buffer */
+    return len;
+}
diff --git a/libdw/Android.mk b/libdw/Android.mk
index 44f0223..9f4708d 100755
--- a/libdw/Android.mk
+++ b/libdw/Android.mk
@@ -128,12 +128,17 @@
 
 ifeq ($(HOST_OS),darwin)
 	LOCAL_CFLAGS += -fnested-functions
+	LOCAL_SRC_FILES += \
+		../host-darwin-fixup/getline.c
 endif
 
 LOCAL_C_INCLUDES += $(LOCAL_PATH)/../host-$(HOST_OS)-fixup
 
 LOCAL_CFLAGS += -DHAVE_CONFIG_H -std=gnu99 -D_GNU_SOURCE -DIS_LIBDW
 
+# to fix machine-dependent issues
+LOCAL_CFLAGS += -include $(LOCAL_PATH)/../host-$(HOST_OS)-fixup/AndroidFixup.h
+
 LOCAL_MODULE_TAGS := optional
 
 LOCAL_MODULE:= libdw
diff --git a/libdwfl/Android.mk b/libdwfl/Android.mk
index 71a7ddb..3effccc 100755
--- a/libdwfl/Android.mk
+++ b/libdwfl/Android.mk
@@ -63,6 +63,9 @@
 	LOCAL_CFLAGS += -fnested-functions
 endif
 
+# to fix machine-dependent issues
+LOCAL_CFLAGS += -include $(LOCAL_PATH)/../host-$(HOST_OS)-fixup/AndroidFixup.h
+
 LOCAL_MODULE_TAGS := optional
 
 LOCAL_MODULE:= libdwfl
diff --git a/libdwfl/dwfl_build_id_find_elf.c b/libdwfl/dwfl_build_id_find_elf.c
index 21c7719..28bf5fd 100644
--- a/libdwfl/dwfl_build_id_find_elf.c
+++ b/libdwfl/dwfl_build_id_find_elf.c
@@ -85,7 +85,7 @@
   const Dwfl_Callbacks *const cb = mod->dwfl->callbacks;
 
 /* ANDROID_CHANGE_BEGIN */
-#ifdef __BIONIC__
+#if defined(__BIONIC__) || defined(__APPLE__)
   char *path = strdup ((cb->debuginfo_path ? *cb->debuginfo_path : NULL)
 			?: DEFAULT_DEBUGINFO_PATH);
 #else
diff --git a/libdwfl/find-debuginfo.c b/libdwfl/find-debuginfo.c
index 9e06304..c58997d 100644
--- a/libdwfl/find-debuginfo.c
+++ b/libdwfl/find-debuginfo.c
@@ -145,7 +145,7 @@
 
   const Dwfl_Callbacks *const cb = mod->dwfl->callbacks;
 /* ANDROID_CHANGE_BEGIN */
-#ifdef __BIONIC__
+#if defined(__BIONIC__) || defined(__APPLE__)
   char *path = strdup ((cb->debuginfo_path ? *cb->debuginfo_path : NULL)
 			?: DEFAULT_DEBUGINFO_PATH);
 #else
@@ -163,7 +163,7 @@
     }
 
   /* ANDROID_CHANGE_BEGIN */
-#ifdef __BIONIC__
+#if defined(__BIONIC__) || defined(__APPLE__)
   char *file_dirname = (file_basename == file_name ? NULL
 			: strndup (file_name, file_basename - 1 - file_name));
 #else