From 83398e5bb1a81e8f0965918a5c765352f41cf7f6 Mon Sep 17 00:00:00 2001
From: Matt Fischer <matt.fischer@garmin.com>
Date: Fri, 12 Apr 2013 17:35:47 -0500
Subject: [PATCH] Added basic QNX support

This change makes some small modifications to work around some
unique aspects of the QNX build environment.

* --enable-local and --enable-ptrace were added, to allow selective
  disabling of various parts of the build
* QNX-specific support was added for endianness checking, as well
  as the non-standard header names/locations that QNX uses for elf.h
* Support was added for the QNX compiler QCC, which has a couple subtle
  differences from GCC in its command line arguments
---
 configure.ac                   | 39 +++++++++++++++++++++++++++++++--------
 include/libunwind_i.h          | 20 +++++++++++++++++++-
 src/Makefile.am                |  7 +++++--
 src/dwarf/Gfind_unwind_table.c |  1 -
 src/elfxx.h                    |  1 -
 5 files changed, 55 insertions(+), 13 deletions(-)

diff --git a/configure.ac b/configure.ac
index 7d549aa..a175c77 100644
--- a/configure.ac
+++ b/configure.ac
@@ -37,7 +37,7 @@ dnl Checks for header files.
 AC_HEADER_STDC
 AC_CHECK_HEADERS(asm/ptrace_offsets.h endian.h sys/endian.h execinfo.h \
 		ia64intrin.h sys/uc_access.h unistd.h signal.h sys/types.h \
-		sys/procfs.h sys/ptrace.h byteswap.h)
+		sys/procfs.h sys/ptrace.h byteswap.h elf.h)
 
 dnl Checks for typedefs, structures, and compiler characteristics.
 AC_C_CONST
@@ -110,6 +110,22 @@ AC_ARG_ENABLE(coredump,
 AC_MSG_CHECKING([if we should build libunwind-coredump])
 AC_MSG_RESULT([$enable_coredump])
 
+AC_ARG_ENABLE(ptrace,
+	AS_HELP_STRING([--enable-ptrace],[building libunwind-ptrace library]),,
+        [AC_CHECK_HEADER([sys/ptrace.h], [enable_ptrace=yes], [enable_ptrace=no])]
+)
+
+AC_MSG_CHECKING([if we should build libunwind-ptrace])
+AC_MSG_RESULT([$enable_ptrace])
+
+AC_ARG_ENABLE(local,
+	AS_HELP_STRING([--enable-local],[building local unwind support]),,
+        [AS_IF([test x$target_arch == x$host_arch], [enable_local=yes], [enable_local=no])]
+)
+
+AC_MSG_CHECKING([if we should build local unwind support])
+AC_MSG_RESULT([$enable_local])
+
 AC_MSG_CHECKING([for build architecture])
 AC_MSG_RESULT([$build_arch])
 AC_MSG_CHECKING([for host architecture])
@@ -120,7 +136,8 @@ AC_MSG_CHECKING([for target operating system])
 AC_MSG_RESULT([$target_os])
 
 AM_CONDITIONAL(BUILD_COREDUMP, test x$enable_coredump = xyes)
-AM_CONDITIONAL(REMOTE_ONLY, test x$target_arch != x$host_arch)
+AM_CONDITIONAL(BUILD_PTRACE, test x$enable_ptrace = xyes)
+AM_CONDITIONAL(REMOTE_ONLY, test x$enable_local = xno)
 AM_CONDITIONAL(ARCH_ARM, test x$target_arch = xarm)
 AM_CONDITIONAL(ARCH_IA64, test x$target_arch = xia64)
 AM_CONDITIONAL(ARCH_HPPA, test x$target_arch = xhppa)
@@ -160,14 +177,9 @@ if test x$target_arch = xppc64; then
         AC_SUBST([libdir])
 fi
 
-AC_MSG_CHECKING([whether to restrict build to remote support])
-if test x$target_arch != x$host_arch; then
+if test x$enable_local = xno; then
   CPPFLAGS="${CPPFLAGS} -DUNW_REMOTE_ONLY"
-  remote_only=yes
-else
-  remote_only=no
 fi
-AC_MSG_RESULT([$remote_only])
 
 AC_MSG_CHECKING([whether to enable debug support])
 AC_ARG_ENABLE(debug,
@@ -262,6 +274,10 @@ if test x$GCC = xyes -a x$intel_compiler != xyes; then
 fi
 AC_MSG_RESULT([$intel_compiler])
 
+AC_MSG_CHECKING([for QCC compiler])
+AS_CASE([$CC], [qcc*|QCC*], [qcc_compiler=yes], [qcc_compiler=no])
+AC_MSG_RESULT([$qcc_compiler])
+
 if test x$intel_compiler = xyes; then
   AC_MSG_CHECKING([if linker supports -static-libcxa])
   save_LDFLAGS="$LDFLAGS"
@@ -274,6 +290,12 @@ if test x$intel_compiler = xyes; then
   AC_MSG_RESULT([$have_static_libcxa])
 fi
 
+if test x$qcc_compiler = xyes; then
+    LDFLAGS_NOSTARTFILES="-XCClinker -Wc,-nostartfiles"
+else
+    LDFLAGS_NOSTARTFILES="-XCClinker -nostartfiles"
+fi
+
 AC_MSG_CHECKING([for __builtin___clear_cache])
 AC_LINK_IFELSE(
   [AC_LANG_PROGRAM([[]], [[__builtin___clear_cache(0, 0)]])],
@@ -331,6 +353,7 @@ AC_SUBST(target_os)
 AC_SUBST(arch)
 AC_SUBST(ARCH)
 AC_SUBST(LDFLAGS_STATIC_LIBCXA)
+AC_SUBST(LDFLAGS_NOSTARTFILES)
 AC_SUBST(LIBCRTS)
 AC_SUBST(PKG_MAJOR)
 AC_SUBST(PKG_MINOR)
diff --git a/include/libunwind_i.h b/include/libunwind_i.h
index 966a3e3..4fed2b6 100644
--- a/include/libunwind_i.h
+++ b/include/libunwind_i.h
@@ -56,7 +56,17 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 #include <string.h>
 #include <unistd.h>
 #include <sys/mman.h>
-#include <elf.h>
+
+#if defined(HAVE_ELF_H)
+# include <elf.h>
+#else
+# if defined(__QNX__)
+#  include <sys/elf.h>
+#  include <sys/elf_dyn.h>
+# else
+#  error Could not locate <elf.h>
+# endif
+#endif
 
 #if defined(HAVE_ENDIAN_H)
 # include <endian.h>
@@ -67,6 +77,14 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 # define __BIG_ENDIAN		4321
 # if defined(__hpux)
 #   define __BYTE_ORDER __BIG_ENDIAN
+# elif defined(__QNX__)
+#   if defined(__BIGENDIAN__)
+#     define __BYTE_ORDER __BIG_ENDIAN
+#   elif defined(__LITTLEENDIAN__)
+#     define __BYTE_ORDER __LITTLE_ENDIAN
+#   else
+#     error Host has unknown byte-order.
+#   endif
 # else
 #   error Host has unknown byte-order.
 # endif
diff --git a/src/Makefile.am b/src/Makefile.am
index adfbef3..7c202af 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,12 +4,15 @@ COREDUMP_SO_VERSION=0:0:0
 #
 # Don't link with start-files since we don't use any constructors/destructors:
 #
-COMMON_SO_LDFLAGS =	-XCClinker -nostartfiles
+COMMON_SO_LDFLAGS =	$(LDFLAGS_NOSTARTFILES)
 
 lib_LIBRARIES =
 lib_LTLIBRARIES =
 if !REMOTE_ONLY
-lib_LTLIBRARIES += libunwind.la libunwind-ptrace.la
+lib_LTLIBRARIES += libunwind.la
+if BUILD_PTRACE
+lib_LTLIBRARIES += libunwind-ptrace.la
+endif
 if BUILD_COREDUMP
 lib_LTLIBRARIES += libunwind-coredump.la
 endif
diff --git a/src/dwarf/Gfind_unwind_table.c b/src/dwarf/Gfind_unwind_table.c
index 961226a..eb01ae9 100644
--- a/src/dwarf/Gfind_unwind_table.c
+++ b/src/dwarf/Gfind_unwind_table.c
@@ -23,7 +23,6 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
-#include <elf.h>
 #include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
diff --git a/src/elfxx.h b/src/elfxx.h
index dea0463..558a235 100644
--- a/src/elfxx.h
+++ b/src/elfxx.h
@@ -24,7 +24,6 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
-#include <elf.h>
 #include <fcntl.h>
 #include <unistd.h>
 
-- 
1.8.0.msysgit.0

