Include some build fixes for OS X.

Apart from the obvious little issues, this also works around a
(seeming) libtool/linker:

a.c defines a symbol:

int kFoo;

b.c uses it:

extern int kFoo;

int f() {
  return kFoo;
}

compile them:

$ gcc -c a.c
$ gcc -c b.c

and create a dummy main in order to run it, main.c:

int f();

int main() {
  return f();
}

this works as expected:

$ gcc main.c a.o b.o

but, if we make an archive:

$ ar q lib.a a.o b.o

and use that:

$ gcc main.c lib.a
Undefined symbols for architecture x86_64
  "_kFoo", referenced from:
    _f in lib.a(b.o)

(It doesn't matter what order the .o files are put into the .a)

Linux and Windows don't seem to have this problem.

nm on a.o shows that the symbol is of type "C", which is a "common symbol"[1].
Basically the linker will merge multiple common symbol definitions together.

If ones makes a.c read:

int kFoo = 0;

Then one gets a type "D" symbol - a "data section symbol" and everything works
just fine.

This might actually be a libtool bug instead of an ld bug: Looking at `xxd
lib.a | less`, the __.SYMDEF SORTED index at the beginning of the archive
doesn't contain an entry for kFoo unless initialised.

Change-Id: I4cdad9ba46e9919221c3cbd79637508959359427
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index 86fb477..35c2262 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -1,6 +1,10 @@
 include_directories(. ../include)
 
-if (UNIX)
+if(APPLE)
+	set(PERLASM_STYLE macosx)
+	set(ASM_EXT S)
+	enable_language(ASM)
+else(UNIX)
 	set(PERLASM_STYLE elf)
 	set(ASM_EXT S)
 	enable_language(ASM)
diff --git a/crypto/base.h b/crypto/base.h
index 8a05899..a5ec844 100644
--- a/crypto/base.h
+++ b/crypto/base.h
@@ -75,6 +75,10 @@
 #error "Unknown target CPU"
 #endif
 
+#if defined(__APPLE__)
+#define OPENSSL_APPLE
+#endif
+
 #define OPENSSL_IS_BORINGSSL
 #define OPENSSL_VERSION_NUMBER 0x10002000
 
diff --git a/crypto/bn/bn_test.c b/crypto/bn/bn_test.c
index a49eada..440e1b6 100644
--- a/crypto/bn/bn_test.c
+++ b/crypto/bn/bn_test.c
@@ -695,17 +695,6 @@
 }
 
 static void print_word(BIO *bp, BN_ULONG w) {
-#ifdef OPENSSL_64_BIT
-  if (sizeof(w) > sizeof(unsigned long)) {
-    unsigned long h = (unsigned long)(w >> 32), l = (unsigned long)(w);
-
-    if (h)
-      BIO_printf(bp, "%lX%08lX", h, l);
-    else
-      BIO_printf(bp, "%lX", l);
-    return;
-  }
-#endif
   BIO_printf(bp, BN_HEX_FMT1, w);
 }
 
diff --git a/crypto/bn/internal.h b/crypto/bn/internal.h
index 3d6d75d..afc9232 100644
--- a/crypto/bn/internal.h
+++ b/crypto/bn/internal.h
@@ -125,6 +125,8 @@
 
 #include <openssl/base.h>
 
+#include <inttypes.h>
+
 #if defined(__cplusplus)
 extern "C" {
 #endif
@@ -156,11 +158,10 @@
 #define BN_MASK2h1	(0xffffffff80000000L)
 #define BN_TBIT		(0x8000000000000000L)
 #define BN_DEC_CONV	(10000000000000000000UL)
-#define BN_DEC_FMT1	"%lu"
-#define BN_DEC_FMT2	"%019lu"
+#define BN_DEC_FMT1	"%" PRIu64
+#define BN_DEC_FMT2	"%019" PRIu64
 #define BN_DEC_NUM	19
-#define BN_HEX_FMT1	"%lX"
-#define BN_HEX_FMT2	"%016lX"
+#define BN_HEX_FMT1	"%" PRIx64
 
 #elif defined(OPENSSL_32_BIT)
 
@@ -176,11 +177,10 @@
 #define BN_MASK2h	(0xffff0000L)
 #define BN_TBIT		(0x80000000L)
 #define BN_DEC_CONV	(1000000000L)
-#define BN_DEC_FMT1	"%u"
-#define BN_DEC_FMT2	"%09u"
+#define BN_DEC_FMT1	"%" PRIu32
+#define BN_DEC_FMT2	"%09" PRIu32
 #define BN_DEC_NUM	9
-#define BN_HEX_FMT1	"%X"
-#define BN_HEX_FMT2	"%08X"
+#define BN_HEX_FMT1	"%" PRIx32
 
 #else
 #error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT"
diff --git a/crypto/cpu-intel.c b/crypto/cpu-intel.c
index cda80ea..bc3148f 100644
--- a/crypto/cpu-intel.c
+++ b/crypto/cpu-intel.c
@@ -66,7 +66,14 @@
 #include <stdio.h>
 #include <inttypes.h>
 
-uint32_t OPENSSL_ia32cap_P[4];
+/* This value must be explicitly initialised to zero in order to work around a
+ * bug in libtool or the linker on OS X.
+ *
+ * If not initialised then it becomes a "common symbol". When put into an
+ * archive, linking on OS X will fail to resolve common symbols. By
+ * initialising it to zero, it becomes a "data symbol", which isn't so
+ * affected. */
+uint32_t OPENSSL_ia32cap_P[4] = {0};
 
 /* OPENSSL_ia32_cpuid is defined in cpu-x86_64-asm.pl. */
 extern uint64_t OPENSSL_ia32_cpuid(uint32_t*);
diff --git a/crypto/lhash/lhash_test.c b/crypto/lhash/lhash_test.c
index e53a6f6..578bb66 100644
--- a/crypto/lhash/lhash_test.c
+++ b/crypto/lhash/lhash_test.c
@@ -12,9 +12,14 @@
  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
 
-#define _POSIX_SOURCE
 #define _BSD_SOURCE
 
+#if !defined(__APPLE__)
+/* _POSIX_SOURCE is needed on Linux in order to get rand_r, but on OS X causes
+ * a build failure. */
+#define _POSIX_SOURCE
+#endif
+
 #include <openssl/lhash.h>
 
 #include <stdio.h>
diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt
index d9f6732..4b9c03b 100644
--- a/tool/CMakeLists.txt
+++ b/tool/CMakeLists.txt
@@ -10,4 +10,8 @@
 	tool.cc
 )
 
-target_link_libraries(bssl ssl crypto -lrt)
+if (APPLE)
+	target_link_libraries(bssl ssl crypto)
+else()
+	target_link_libraries(bssl ssl crypto -lrt)
+endif()
diff --git a/tool/client.cc b/tool/client.cc
index 6af34a3..5acc8b1 100644
--- a/tool/client.cc
+++ b/tool/client.cc
@@ -17,6 +17,7 @@
 #include <string>
 #include <vector>
 
+#include <errno.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 
diff --git a/tool/speed.cc b/tool/speed.cc
index 176e2e2..8dde777 100644
--- a/tool/speed.cc
+++ b/tool/speed.cc
@@ -28,6 +28,8 @@
 
 #if defined(OPENSSL_WINDOWS)
 #include <Windows.h>
+#elif defined(OPENSSL_APPLE)
+#include <sys/time.h>
 #endif
 
 extern "C" {
@@ -61,6 +63,17 @@
 
 #if defined(OPENSSL_WINDOWS)
 static uint64_t time_now() { return GetTickCount64() * 1000; }
+#elif defined(OPENSSL_APPLE)
+static uint64_t time_now() {
+  struct timeval tv;
+  uint64_t ret;
+
+  gettimeofday(&tv, NULL);
+  ret = tv.tv_sec;
+  ret *= 1000000;
+  ret += tv.tv_usec;
+  return ret;
+}
 #else
 static uint64_t time_now() {
   struct timespec ts;