diff --git a/configure.ac b/configure.ac
index 28537ac..09d4d9a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -221,6 +221,8 @@ AC_REPLACE_FUNCS([strcasecmp strcasestr strlcat strlcpy strndup])
 dnl Find the math libary, then check for cbrt function in it.
 AC_CHECK_LIB(m, sqrt)
 AC_CHECK_FUNCS([cbrt])
+# But we don't want to link everything with libm
+LIBS="${LIBS//-lm}"
 
 dnl Check for libtsol for Solaris Trusted Extensions module
 AC_CHECK_LIB(tsol, bsllow, [BUILD_TSOL_MODULE=yes], [BUILD_TSOL_MODULE=no])
@@ -1092,7 +1094,6 @@ AM_CONDITIONAL(AIGLX_DRI_LOADER, { test "x$DRI" = xyes || test "x$DRI2" = xyes;
 
 if test "x$GLX_USE_TLS" = xyes ; then
 	GLX_DEFINES="-DGLX_USE_TLS -DPTHREADS"
-	GLX_SYS_LIBS="$GLX_SYS_LIBS -lpthread"
 fi
 AC_SUBST([GLX_DEFINES])
 AC_SUBST([GLX_SYS_LIBS])
@@ -1516,6 +1517,7 @@ XSERVER_CFLAGS="${XSERVER_CFLAGS} ${XSERVERCFLAGS_CFLAGS}"
 XSERVER_LIBS="$DIX_LIB $MI_LIB $OS_LIB"
 XSERVER_SYS_LIBS="${XSERVERLIBS_LIBS} ${SYS_LIBS} ${LIBS}"
 XSERVER_SYS_LIBS="${XSERVER_SYS_LIBS} -lproject"
+XSERVER_SYS_LIBS="${XSERVER_SYS_LIBS} -lm"
 AC_SUBST([XSERVER_LIBS])
 AC_SUBST([XSERVER_SYS_LIBS])
 
diff --git a/hw/xfree86/Makefile.am b/hw/xfree86/Makefile.am
index c3899b5..36ca3d6 100644
--- a/hw/xfree86/Makefile.am
+++ b/hw/xfree86/Makefile.am
@@ -25,10 +25,10 @@ if INT10MODULE
 INT10_SUBDIR = int10
 endif
 
-SUBDIRS = common ddc x86emu $(INT10_SUBDIR) os-support parser \
-	  ramdac $(VGAHW_SUBDIR) loader modes $(DRI_SUBDIR) \
-	  $(DRI2_SUBDIR) . $(VBE_SUBDIR) i2c dixmods \
-	  fbdevhw shadowfb exa $(XF86UTILS_SUBDIR) doc man
+SUBDIRS = common ddc i2c x86emu $(INT10_SUBDIR) fbdevhw os-support parser \
+	  ramdac shadowfb $(VBE_SUBDIR) $(VGAHW_SUBDIR) loader \
+	  $(DRI_SUBDIR) $(DRI2_SUBDIR) . dixmods exa modes \
+	  $(XF86UTILS_SUBDIR) doc man
 
 DIST_SUBDIRS = common ddc i2c x86emu int10 fbdevhw os-support \
                parser ramdac shadowfb vbe vgahw \
@@ -38,7 +38,7 @@ DIST_SUBDIRS = common ddc i2c x86emu int10 fbdevhw os-support \
 bin_PROGRAMS = Xorg
 nodist_Xorg_SOURCES = sdksyms.c
 
-AM_CFLAGS = $(DIX_CFLAGS) @XORG_CFLAGS@
+AM_CFLAGS = $(DIX_CFLAGS) $(PIXMAN_CFLAGS) @XORG_CFLAGS@
 INCLUDES = $(XORG_INCS) -I$(srcdir)/parser -I$(top_srcdir)/miext/cw \
 	-I$(srcdir)/ddc -I$(srcdir)/i2c -I$(srcdir)/modes -I$(srcdir)/ramdac \
 	-I$(srcdir)/dri -I$(srcdir)/dri2
@@ -123,6 +123,9 @@ include $(SDKSYMS_DEP)
 i2c/libi2c.la:
 	$(AM_V_at)cd i2c && $(MAKE) libi2c.la
 
+modes/libxf86modes.la:
+	$(AM_V_at)cd modes && $(MAKE) libxf86modes.la
+
 dixmods/libdixmods.la:
 	$(AM_V_at)cd dixmods && $(MAKE) libdixmods.la
 
diff --git a/hw/xfree86/dixmods/Makefile.am b/hw/xfree86/dixmods/Makefile.am
index c02677a..a9557b0 100644
--- a/hw/xfree86/dixmods/Makefile.am
+++ b/hw/xfree86/dixmods/Makefile.am
@@ -18,7 +18,9 @@ INCLUDES = @XORG_INCS@ \
            -I$(top_srcdir)/glx
 
 libfb_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+libfb_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 libfb_la_LIBADD = $(top_builddir)/fb/libfb.la
+libfb_la_LIBADD += -lpixman-1
 libfb_la_SOURCES = $(top_builddir)/fb/fbcmap_mi.c fbmodule.c
 libfb_la_CFLAGS = $(AM_CFLAGS)
 
@@ -28,9 +30,12 @@ libwfb_la_SOURCES = $(top_builddir)/fb/fbcmap_mi.c fbmodule.c
 libwfb_la_CFLAGS = $(AM_CFLAGS) -DFB_ACCESS_WRAPPER
 
 libglx_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+libglx_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 libglx_la_LIBADD = $(top_builddir)/glx/libglx.la $(GLX_SYS_LIBS)
 if AIGLX_DRI_LOADER
 libglx_la_LIBADD += $(top_builddir)/glx/libglxdri.la
+libglx_la_LDFLAGS += -R$(extsmoduledir)
+libglx_la_LDFLAGS += $(LIBDRM_LIBS)
 if NO_UNDEFINED
 libglx_la_LIBADD += ../dri/libdri.la ../dri2/libdri2.la
 endif
@@ -39,6 +44,8 @@ libglx_la_SOURCES = glxmodule.c
 
 libshadow_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
 libshadow_la_LIBADD = $(top_builddir)/miext/shadow/libshadow.la
+libshadow_la_DEPENDENCIES = libfb.la
+libshadow_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 if NO_UNDEFINED
 libshadow_la_LIBADD += libfb.la
 endif
diff --git a/hw/xfree86/i2c/Makefile.am b/hw/xfree86/i2c/Makefile.am
index cb18db1..06cd033 100644
--- a/hw/xfree86/i2c/Makefile.am
+++ b/hw/xfree86/i2c/Makefile.am
@@ -22,22 +22,30 @@ sdk_HEADERS = xf86i2c.h bt829.h fi1236.h msp3430.h tda8425.h tda9850.h tda9885.h
 # i2c drivers
 #
 bt829_drv_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+bt829_drv_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 bt829_drv_la_SOURCES = bt829.c bt829.h bt829_module.c
 
 fi1236_drv_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+fi1236_drv_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
+fi1236_drv_la_LIBADD  = -lm
 fi1236_drv_la_SOURCES = fi1236.c fi1236.h fi1236_module.c tda9885.c
 
 msp3430_drv_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+msp3430_drv_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 msp3430_drv_la_SOURCES = msp3430.c msp3430.h msp3430_module.c
 
 tda8425_drv_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+tda8425_drv_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 tda8425_drv_la_SOURCES = tda8425.c tda8425.h tda8425_module.c
 
 tda9850_drv_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+tda9850_drv_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 tda9850_drv_la_SOURCES = tda9850.c tda9850.h tda9850_module.c
 
 tda9885_drv_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+tda9885_drv_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 tda9885_drv_la_SOURCES = tda9885.c tda9885.h tda9885_module.c
 
 uda1380_drv_la_LDFLAGS = -module -avoid-version $(LD_NO_UNDEFINED_FLAG)
+uda1380_drv_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
 uda1380_drv_la_SOURCES = uda1380.c uda1380.h uda1380_module.c
diff --git a/hw/xfree86/shadowfb/Makefile.am b/hw/xfree86/shadowfb/Makefile.am
index 5756fca..6ec3116 100644
--- a/hw/xfree86/shadowfb/Makefile.am
+++ b/hw/xfree86/shadowfb/Makefile.am
@@ -8,3 +8,7 @@ sdk_HEADERS = shadowfb.h
 INCLUDES = $(XORG_INCS)
 
 AM_CFLAGS = $(DIX_CFLAGS) $(XORG_CFLAGS)
+
+libshadowfb_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
+libshadowfb_la_LIBADD += -lXfont
+
diff --git a/hw/xfree86/utils/gtf/Makefile.am b/hw/xfree86/utils/gtf/Makefile.am
index f77bf60..64a7362 100644
--- a/hw/xfree86/utils/gtf/Makefile.am
+++ b/hw/xfree86/utils/gtf/Makefile.am
@@ -24,4 +24,6 @@ bin_PROGRAMS = gtf
 
 gtf_SOURCES = gtf.c
 gtf_CFLAGS = $(XORG_CFLAGS)
-gtf_LDADD = -lm
+# Include -lc as workaround for bug 17815318 so libm can actually go away
+# if -xlibmil manages to inline all math functions.
+gtf_LDADD = -lc -lm
diff --git a/hw/xfree86/vbe/Makefile.am b/hw/xfree86/vbe/Makefile.am
index 0b24faf..40ba628 100644
--- a/hw/xfree86/vbe/Makefile.am
+++ b/hw/xfree86/vbe/Makefile.am
@@ -11,3 +11,5 @@ AM_CFLAGS = $(DIX_CFLAGS) $(XORG_CFLAGS)
 
 INCLUDES = $(XORG_INCS) -I$(srcdir)/../ddc -I$(srcdir)/../i2c \
            -I$(srcdir)/../int10
+
+libvbe_la_LDFLAGS += $(XORG_EXTERNS_FLAG)
diff --git a/hw/xfree86/vgahw/Makefile.am b/hw/xfree86/vgahw/Makefile.am
index f48e46a..5b69aad 100644
--- a/hw/xfree86/vgahw/Makefile.am
+++ b/hw/xfree86/vgahw/Makefile.am
@@ -1,5 +1,6 @@
 module_LTLIBRARIES = libvgahw.la
 libvgahw_la_LDFLAGS = -avoid-version
+libvgahw_la_LIBADD = $(PCIACCESS_LIBS)
 libvgahw_la_SOURCES = vgaHW.c vgaHWmodule.c
 INCLUDES = $(XORG_INCS) -I$(srcdir)/../ddc -I$(srcdir)/../i2c
 AM_CFLAGS = $(DIX_CFLAGS) $(XORG_CFLAGS)
