Don't pass -Wl,--no-undefined to host targets

Host builds were compiling without -Wl,--no-undefined because of an ASAN
issue.  Pass -Wl,--no-undefined for host builds unless sanitzers are
enabled.  Also fix LOCAL_ALLOW_UNDEFINED_SYMBOLS on darwin, where
disallowing undefined symbols is the default.

Test: m -j host
Test: m -j SANITIZE_HOST=address host
Bug: 32305815
Change-Id: Ia4bb305a50b1c1048b119f75726d52f82e21438c
diff --git a/cc/linker.go b/cc/linker.go
index 399074d..0923338 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -150,7 +150,12 @@
 	toolchain := ctx.toolchain()
 
 	if !ctx.noDefaultCompilerFlags() {
-		if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
+		if Bool(linker.Properties.Allow_undefined_symbols) {
+			if ctx.Darwin() {
+				// darwin defaults to treating undefined symbols as errors
+				flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
+			}
+		} else if !ctx.Darwin() {
 			flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
 		}
 
diff --git a/cc/sanitize.go b/cc/sanitize.go
index ff0d772..d9e28cc 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -255,6 +255,9 @@
 			// libraries needed with -fsanitize=address. http://b/18650275 (WAI)
 			flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
 			flags.LdFlags = append(flags.LdFlags, "-Wl,--no-as-needed")
+			// Host ASAN only links symbols in the final executable, so
+			// there will always be undefined symbols in intermediate libraries.
+			_, flags.LdFlags = removeFromList("-Wl,--no-undefined", flags.LdFlags)
 		} else {
 			flags.CFlags = append(flags.CFlags, "-mllvm", "-asan-globals=0")
 			flags.DynamicLinker = "/system/bin/linker_asan"