From 0cbfe8bc78300319f291cbcae6791a2c86ef8779 Mon Sep 17 00:00:00 2001
From: Segey Reznikov <diver@gelios.net>
Date: Wed, 18 Jan 2017 00:26:11 +0000
Subject: Haiku fixes


diff --git a/Config.kmk b/Config.kmk
index 3ff6865..252ee26 100644
--- a/Config.kmk
+++ b/Config.kmk
@@ -2740,8 +2740,8 @@ if   $(KBUILD_TARGET) == "linux" \
  VBOX_GCC_WARN_PEDANTIC += $(VBOX_GCC_WARN_MASOCHISTIC)
 endif
 
-VBOX_GCC_PEDANTIC_CXX ?= -pedantic $(VBOX_GCC_WARN_PEDANTIC) $(VBOX_GCC_Wno-return-type-c-linkage) $(VBOX_GCC_Wno-overloaded-virtual)
-VBOX_GCC_PEDANTIC_C   ?= -pedantic $(VBOX_GCC_WARN_PEDANTIC) -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Werror-implicit-function-declaration
+VBOX_GCC_PEDANTIC_CXX ?=  $(VBOX_GCC_WARN_PEDANTIC) $(VBOX_GCC_Wno-return-type-c-linkage) $(VBOX_GCC_Wno-overloaded-virtual)
+VBOX_GCC_PEDANTIC_C   ?=  $(VBOX_GCC_WARN_PEDANTIC) -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Werror-implicit-function-declaration
 
 VBOX_GCC_NON_PEDANTIC_CXX ?= -Wno-sign-compare
 VBOX_GCC_NON_PEDANTIC_C   ?= -Wno-sign-compare -Werror-implicit-function-declaration
@@ -4376,8 +4376,8 @@ TEMPLATE_VBOXR0DRV_LDTOOL              = $(VBOX_GCC_TOOL)
 TEMPLATE_VBOXR0DRV_DEFS                = _KERNEL_MODE=1 _STRICT_STDC IN_RING0 IN_RT_R0
 TEMPLATE_VBOXR0DRV_INCS                = $(VBOX_HAIKU_SYS_INCS)
 #TODO: sort this out
-TEMPLATE_VBOXR0DRV_LDFLAGS             = -shared -no-undefined -dc -dy -lroot -rpath-link /boot/system/develop/lib/x86 --no-add-needed /boot/system/develop/lib/_KERNEL_ --no-add-needed /boot/system/develop/lib/haiku_version_glue.o
-TEMPLATE_VBOXR0DRV_CFLAGS              = -fno-PIC \
+TEMPLATE_VBOXR0DRV_LDFLAGS             = -shared -no-undefined -dc -dy -lstdc++ -lsupc++ -lroot -rpath-link /system/develop/lib  /system/develop/lib/_KERNEL_  /system/develop/lib/haiku_version_glue.o
+TEMPLATE_VBOXR0DRV_CFLAGS              = -fPIC \
 	$(VBOX_GCC_WARN) -Wstrict-prototypes $(VBOX_GCC_Wno-pointer-sign) -Wno-sign-compare \
 	$(VBOX_GCC_fno-stack-protector) $(VBOX_GCC_R0_OPT) $(VBOX_GCC_R0_FP) -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration
 TEMPLATE_VBOXR0DRV_CFLAGS.x86          = -mno-sse -mno-mmx -mno-sse2 -mno-3dnow
@@ -4385,7 +4385,7 @@ TEMPLATE_VBOXR0DRV_CFLAGS.x86          = -m32 -mno-sse -mno-mmx -mno-sse2 -mno-3
 TEMPLATE_VBOXR0DRV_CFLAGS.amd64        = -m64 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow \
 	-fno-reorder-blocks -ffreestanding -fno-asynchronous-unwind-tables -funit-at-a-time \
 	-Wno-sign-compare
-TEMPLATE_VBOXR0DRV_CXXFLAGS           = -fno-PIC -Wpointer-arith \
+TEMPLATE_VBOXR0DRV_CXXFLAGS           = -fPIC -Wpointer-arith \
 	-Wshadow -Wuninitialized -Wunused-function -Wunused-label -Wunused-value -Wunused-variable \
 	-Wformat \
 	-O2 -nodefaultlibs -fno-omit-frame-pointer -fno-strict-aliasing -fno-common -fno-exceptions -fno-rtti
@@ -4566,9 +4566,9 @@ TEMPLATE_VBOXR3EXE_LIBS                = network iconv stdc++ supc++
 TEMPLATE_VBOXR3EXE_LIBPATH            += \
 	/boot/common/lib
 # Haiku uses PIC by default...
-TEMPLATE_VBOXR3EXE_CFLAGS             += -fno-pic
-TEMPLATE_VBOXR3EXE_CXXFLAGS           += -fno-pic
-TEMPLATE_VBOXR3EXE_LDFLAGS            += -fno-pic
+TEMPLATE_VBOXR3EXE_CFLAGS             += -fPIC
+TEMPLATE_VBOXR3EXE_CXXFLAGS           += -fPIC
+TEMPLATE_VBOXR3EXE_LDFLAGS            += -fPIC
  else if1of ($(KBUILD_TARGET), freebsd openbsd)
 TEMPLATE_VBOXR3EXE_TOOL                = GXX3
 TEMPLATE_VBOXR3EXE_LIBS                = pthread
@@ -4626,9 +4626,9 @@ ifn1of ($(KBUILD_TARGET), darwin win os2)
 endif
 ifeq ($(KBUILD_TARGET),haiku)
  # Haiku uses PIC by default...
- TEMPLATE_VBOXR3_CFLAGS               = $(TEMPLATE_VBOXR3EXE_CFLAGS) -fno-pic
- TEMPLATE_VBOXR3_CXXFLAGS             = $(TEMPLATE_VBOXR3EXE_CXXFLAGS) -fno-pic
- TEMPLATE_VBOXR3_LDFLAGS              = $(TEMPLATE_VBOXR3EXE_LDFLAGS) -fno-pic
+ TEMPLATE_VBOXR3_CFLAGS               = $(TEMPLATE_VBOXR3EXE_CFLAGS) -fPIC
+ TEMPLATE_VBOXR3_CXXFLAGS             = $(TEMPLATE_VBOXR3EXE_CXXFLAGS) -fPIC
+ TEMPLATE_VBOXR3_LDFLAGS              = $(TEMPLATE_VBOXR3EXE_LDFLAGS) -fPIC
 endif
 
 #
diff --git a/src/VBox/Additions/common/VBoxGuest/VBoxDev-haiku.c b/src/VBox/Additions/common/VBoxGuest/VBoxDev-haiku.c
index b56e972..375f845 100644
--- a/src/VBox/Additions/common/VBoxGuest/VBoxDev-haiku.c
+++ b/src/VBox/Additions/common/VBoxGuest/VBoxDev-haiku.c
@@ -104,7 +104,7 @@ static status_t vgdrvHaikuOpen(const char *name, uint32 flags, void **cookie)
         return B_OK;
     }
 
-    LogRel((DRIVER_NAME ":vgdrvHaikuOpen: failed. rc=%d\n", rc));
+    Log((DRIVER_NAME ":vgdrvHaikuOpen: failed. rc=%d\n", rc));
     return RTErrConvertToErrno(rc);
 }
 
@@ -176,7 +176,7 @@ static status_t vgdrvHaikuIOCtl(void *cookie, uint32 op, void *data, size_t len)
 {
     PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
     int rc;
-    Log(("vgdrvHaikuIOCtl: cookie=%p op=0x%08x data=%p len=%lu)\n", cookie, op, data, len));
+    LogFlow(("vgdrvHaikuIOCtl: cookie=%p op=0x%08x data=%p len=%lu)\n", cookie, op, data, len));
 
     /*
      * Validate the input.
@@ -211,7 +211,7 @@ static status_t vgdrvHaikuIOCtl(void *cookie, uint32 op, void *data, size_t len)
         pvBuf = RTMemTmpAlloc(len);
         if (RT_UNLIKELY(!pvBuf))
         {
-            LogRel((DRIVER_NAME ":vgdrvHaikuIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", len));
+            Log((DRIVER_NAME ":vgdrvHaikuIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", len));
             return ENOMEM;
         }
 
@@ -220,13 +220,13 @@ static status_t vgdrvHaikuIOCtl(void *cookie, uint32 op, void *data, size_t len)
         if (RT_UNLIKELY(rc < 0))
         {
             RTMemTmpFree(pvBuf);
-            LogRel((DRIVER_NAME ":vgdrvHaikuIOCtl: user_memcpy failed; pvBuf=%p data=%p op=%d. rc=%d\n", pvBuf, data, op, rc));
+            Log((DRIVER_NAME ":vgdrvHaikuIOCtl: user_memcpy failed; pvBuf=%p data=%p op=%d. rc=%d\n", pvBuf, data, op, rc));
             return EFAULT;
         }
         if (RT_UNLIKELY(!VALID_PTR(pvBuf)))
         {
             RTMemTmpFree(pvBuf);
-            LogRel((DRIVER_NAME ":vgdrvHaikuIOCtl: pvBuf invalid pointer %p\n", pvBuf));
+            Log((DRIVER_NAME ":vgdrvHaikuIOCtl: pvBuf invalid pointer %p\n", pvBuf));
             return EINVAL;
         }
     }
diff --git a/src/VBox/Additions/common/VBoxGuest/VBoxGuest-haiku-stubs.c b/src/VBox/Additions/common/VBoxGuest/VBoxGuest-haiku-stubs.c
index b013db2..c454e32 100644
--- a/src/VBox/Additions/common/VBoxGuest/VBoxGuest-haiku-stubs.c
+++ b/src/VBox/Additions/common/VBoxGuest/VBoxGuest-haiku-stubs.c
@@ -338,10 +338,6 @@ RTDECL(PRTLOGGER) RTLogRelGetDefaultInstance(void)
 {
     return g_VBoxGuest->_RTLogRelGetDefaultInstance();
 }
-RTDECL(PRTLOGGER) RTLogRelGetDefaultInstance(uint32_t fFlags, uint32_t iGroup)
-{
-    return g_VBoxGuest->_RTLogRelGetDefaultInstanceEx(fFlags, iGroup);
-}
 RTDECL(int) RTErrConvertToErrno(int iErr)
 {
     return g_VBoxGuest->_RTErrConvertToErrno(iErr);
diff --git a/src/VBox/Additions/common/VBoxGuest/VBoxGuest-haiku.c b/src/VBox/Additions/common/VBoxGuest/VBoxGuest-haiku.c
index 72bc988..37e0912 100644
--- a/src/VBox/Additions/common/VBoxGuest/VBoxGuest-haiku.c
+++ b/src/VBox/Additions/common/VBoxGuest/VBoxGuest-haiku.c
@@ -186,9 +186,6 @@ static struct vboxguest_module_info g_VBoxGuest =
     VGDrvCommonIoCtl,
     VGDrvCommonCreateUserSession,
     VGDrvCommonCloseSession,
-    VBoxGuestIDCOpen,
-    VBoxGuestIDCClose,
-    VBoxGuestIDCCall,
     RTAssertMsg1Weak,
     RTAssertMsg2Weak,
     RTAssertMsg2WeakV,
diff --git a/src/VBox/Additions/common/VBoxGuestLib/Init.cpp b/src/VBox/Additions/common/VBoxGuestLib/Init.cpp
index e8c985e..041ac23 100644
--- a/src/VBox/Additions/common/VBoxGuestLib/Init.cpp
+++ b/src/VBox/Additions/common/VBoxGuestLib/Init.cpp
@@ -167,7 +167,7 @@ int vbglInitCommon (void)
     }
     else
     {
-        LogRel(("vbglInitCommon: VbglPhysHeapInit failed. rc=%Rrc\n", rc));
+        Log(("vbglInitCommon: VbglPhysHeapInit failed. rc=%Rrc\n", rc));
         g_vbgldata.status = VbglStatusNotInitialized;
     }
 
diff --git a/src/VBox/Additions/common/VBoxGuestLib/PhysHeap.cpp b/src/VBox/Additions/common/VBoxGuestLib/PhysHeap.cpp
index 7ba42a4..8722647 100644
--- a/src/VBox/Additions/common/VBoxGuestLib/PhysHeap.cpp
+++ b/src/VBox/Additions/common/VBoxGuestLib/PhysHeap.cpp
@@ -314,7 +314,7 @@ static VBGLPHYSHEAPBLOCK *vbglPhysHeapChunkAlloc (uint32_t cbSize)
 
     if (!pChunk)
     {
-        LogRel(("vbglPhysHeapChunkAlloc: failed to alloc %u contiguous bytes.\n", cbSize));
+        Log(("vbglPhysHeapChunkAlloc: failed to alloc %u contiguous bytes.\n", cbSize));
         return NULL;
     }
 
diff --git a/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.c b/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.c
index 26fcb2b..873b2a8 100644
--- a/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.c
+++ b/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR0LibSharedFolders.c
@@ -75,7 +75,7 @@ DECLVBGL(int) VbglR0SfConnect(PVBGLSFCLIENT pClient)
     RT_ZERO(data);
     data.result   = VINF_SUCCESS;
     data.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
-#if defined(RT_OS_LINUX)
+#if defined(RT_OS_LINUX) || defined(RT_OS_HAIKU)
     strcpy(data.Loc.u.host.achName, "VBoxSharedFolders");
 #else
     RTStrCopy(data.Loc.u.host.achName, sizeof(data.Loc.u.host.achName), "VBoxSharedFolders");
diff --git a/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp b/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp
index 9510688..12ceb20 100644
--- a/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp
+++ b/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp
@@ -426,7 +426,7 @@ int vbglR3DoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
 # elif defined(RT_OS_HAIKU)
     /* The ioctl hook in Haiku does take the len parameter when specified,
      * so just use it. */
-    int rc = ioctl((int)g_File, iFunction, pvData, cbData);
+    int rc = ioctl(RTFileToNative(g_File), iFunction, pvData, cbData);
 # else
 #  error Port me!
 # endif
diff --git a/src/VBox/Additions/haiku/SharedFolders/lock.h b/src/VBox/Additions/haiku/SharedFolders/lock.h
index 07c9e61..6badd17 100644
--- a/src/VBox/Additions/haiku/SharedFolders/lock.h
+++ b/src/VBox/Additions/haiku/SharedFolders/lock.h
@@ -64,7 +64,7 @@ typedef struct rw_lock {
         const char*                             name;
         struct rw_lock_waiter*  waiters;
         thread_id                               holder;
-        vint32                                  count;
+        int32                                   count;
         int32                                   owner_count;
         int16                                   active_readers;
                                                                 // Only > 0 while a writer is waiting: number
diff --git a/src/VBox/Additions/haiku/SharedFolders/vnode_cache.cpp b/src/VBox/Additions/haiku/SharedFolders/vnode_cache.cpp
index 92bee8f..30fbc98 100644
--- a/src/VBox/Additions/haiku/SharedFolders/vnode_cache.cpp
+++ b/src/VBox/Additions/haiku/SharedFolders/vnode_cache.cpp
@@ -46,6 +46,8 @@
 #include "vboxsf.h"
 #include "OpenHashTable.h"
 
+void* __dso_handle = NULL;
+
 struct HashTableDefinition
 {
     typedef uint32       KeyType;
@@ -77,7 +79,7 @@ static ino_t g_nextVnid = 1;
 mutex g_vnodeCacheLock;
 
 
-extern "C" status_t vboxsf_new_vnode(PVBSFMAP map, PSHFLSTRING path, PSHFLSTRING name, vboxsf_vnode** p)
+extern "C" status_t vboxsf_new_vnode(PVBGLSFMAP map, PSHFLSTRING path, PSHFLSTRING name, vboxsf_vnode** p)
 {
     vboxsf_vnode* vn = (vboxsf_vnode*)malloc(sizeof(vboxsf_vnode));
     if (vn == NULL)
diff --git a/src/VBox/Additions/haiku/VBoxTray/VBoxClipboard.cpp b/src/VBox/Additions/haiku/VBoxTray/VBoxClipboard.cpp
index 13467b2..733ea5a 100644
--- a/src/VBox/Additions/haiku/VBoxTray/VBoxClipboard.cpp
+++ b/src/VBox/Additions/haiku/VBoxTray/VBoxClipboard.cpp
@@ -255,7 +255,7 @@ void VBoxClipboardService::MessageReceived(BMessage *message)
             if (formats & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
             {
                 const char *text;
-                int32 textLen;
+                ssize_t textLen;
                 if (clip->FindData("text/plain", B_MIME_TYPE, (const void **)&text, &textLen) == B_OK)
                 {
                     // usually doesn't include the \0 so be safe
@@ -314,7 +314,7 @@ void VBoxClipboardService::MessageReceived(BMessage *message)
         {
             printf("B_CLIPBOARD_CHANGED\n");
             const void *data;
-            int32 dataLen;
+            ssize_t dataLen;
             if (!be_clipboard->Lock())
                 break;
 
diff --git a/src/VBox/Additions/haiku/VBoxTray/VBoxGuestApplication.cpp b/src/VBox/Additions/haiku/VBoxTray/VBoxGuestApplication.cpp
index 37c229a..f59489c 100644
--- a/src/VBox/Additions/haiku/VBoxTray/VBoxGuestApplication.cpp
+++ b/src/VBox/Additions/haiku/VBoxTray/VBoxGuestApplication.cpp
@@ -51,7 +51,6 @@
 #include <iprt/log.h>
 
 #include <errno.h>
-#include <Alert.h>
 #include <Debug.h>
 #include <Invoker.h>
 #include <String.h>
diff --git a/src/VBox/Additions/haiku/VBoxVideo/driver/driver.cpp b/src/VBox/Additions/haiku/VBoxVideo/driver/driver.cpp
index b15afdb..4b3797e 100644
--- a/src/VBox/Additions/haiku/VBoxVideo/driver/driver.cpp
+++ b/src/VBox/Additions/haiku/VBoxVideo/driver/driver.cpp
@@ -171,10 +171,10 @@ status_t init_hardware()
         if (err == B_OK)
             return B_OK;
 
-        LogRel((DRIVER_NAME ":_init_hardware() get_module(%s) failed. err=%08lx\n", B_PCI_MODULE_NAME));
+        Log((DRIVER_NAME ":_init_hardware() get_module(%s) failed. err=%08lx\n", B_PCI_MODULE_NAME));
     }
     else
-        LogRel((DRIVER_NAME ":_init_hardware() get_module(%s) failed. err=%08lx\n", VBOXGUEST_MODULE_NAME, err));
+        Log((DRIVER_NAME ":_init_hardware() get_module(%s) failed. err=%08lx\n", VBOXGUEST_MODULE_NAME, err));
     return B_ERROR;
 }
 
diff --git a/src/VBox/Runtime/r0drv/haiku/alloc-r0drv-haiku.c b/src/VBox/Runtime/r0drv/haiku/alloc-r0drv-haiku.c
index 561eab6..d6d1c11 100644
--- a/src/VBox/Runtime/r0drv/haiku/alloc-r0drv-haiku.c
+++ b/src/VBox/Runtime/r0drv/haiku/alloc-r0drv-haiku.c
@@ -86,7 +86,7 @@ RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb) RT_NO_THROW_DEF
      * Allocate the memory and ensure that the API is still providing
      * memory that's always below 4GB.
      */
-    cb = RT_ALIGN_Z(cb, PAGE_SIZE);
+    cb = RT_ALIGN_Z(cb, B_PAGE_SIZE);
     void *pv;
     area_id area = create_area("VirtualBox Contig Alloc", &pv, B_ANY_KERNEL_ADDRESS, cb, B_32_BIT_CONTIGUOUS,
                                B_READ_AREA | B_WRITE_AREA);
diff --git a/src/VBox/Runtime/r3/posix/process-creation-posix.cpp b/src/VBox/Runtime/r3/posix/process-creation-posix.cpp
index 7fc3a43..2d3b957 100644
--- a/src/VBox/Runtime/r3/posix/process-creation-posix.cpp
+++ b/src/VBox/Runtime/r3/posix/process-creation-posix.cpp
@@ -71,6 +71,11 @@
 # include <libcontract.h>
 #endif
 
+#ifdef RT_OS_HAIKU
+#define _BSD_SOURCE
+# include <bsd/paths.h>
+#endif
+
 #ifndef RT_OS_SOLARIS
 # include <paths.h>
 #else
diff --git a/src/libs/kStuff/kStuff/include/k/kDefs.h b/src/libs/kStuff/kStuff/include/k/kDefs.h
index f805cc3..3207d50 100644
--- a/src/libs/kStuff/kStuff/include/k/kDefs.h
+++ b/src/libs/kStuff/kStuff/include/k/kDefs.h
@@ -60,8 +60,10 @@
 #define K_OS_SOLARIS    9
 /** Windows. */
 #define K_OS_WINDOWS    10
+/** Haiku. */
+#define K_OS_HAIKU    11
 /** The max K_OS_* value (exclusive). */
-#define K_OS_MAX        11
+#define K_OS_MAX        12
 /** @} */
 
 /** @def K_OS
@@ -94,6 +96,8 @@
 #  define K_OS      K_OS_SOLARIS
 # elif defined(_WIN32) || defined(_WIN64)
 #  define K_OS      K_OS_WINDOWS
+# elif defined(__HAIKU__)
+#  define K_OS      K_OS_HAIKU
 # else
 #  error "Port Me"
 # endif
-- 
2.12.2

