Fix gcc libraries for host targets with platform libc++

Targeting the platform-provided libc++ on the host requires passing
-nodefautlibs to gcc, and then re-adding all the default libs except
libc++.

Change-Id: I5a42375bcc819b07f6ee02e9d76e7f5088fa00e0
diff --git a/cc/cc.go b/cc/cc.go
index 9f06354..f527bf1 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -743,6 +743,11 @@
 	}
 }
 
+var (
+	hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
+	hostStaticGccLibs  = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
+)
+
 func (c *CCLinked) Flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
 	stl := c.stl(ctx)
 	if ctx.Failed() {
@@ -756,7 +761,12 @@
 		if ctx.Host() {
 			flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
 			flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
-			flags.LdFlags = append(flags.LdFlags, "-lc", "-lm", "-lpthread")
+			flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
+			if c.shared() {
+				flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
+			} else {
+				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
+			}
 		}
 	case "stlport", "stlport_static":
 		if ctx.Device() {
@@ -785,7 +795,11 @@
 		if ctx.Host() {
 			flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
 			flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
-			flags.LdFlags = append(flags.LdFlags, "-lc", "-lm")
+			if c.shared() {
+				flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
+			} else {
+				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
+			}
 		}
 	default:
 		panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))