Extract the linker and embed it into host bionic binaries

The linux kernel requires that the ELF interpreter (runtime linker)
that's referenced by PT_INTERP be either an absolute path, or a relative
path from the current working directory. We'd prefer a relative path
from the binary, similarly to how we handle looking up shared libraries,
but that's not supported.

Instead, extract the load sections from the runtime linker ELF binary
and embed them into each host bionic binary, omitting the PT_INTERP
declaration. The kernel will treat it as a static binary, and we'll use
a special entry point (linker_wrapper) to fix up the arguments passed by
the kernel before jumping to the embedded linker. From the linker's
point of view, it looks like the kernel loaded the linker like normal.

Bug: 31559095
Test: Enable host bionic,
      out/soong/host/linux_bionic-x86/nativetest64/libdemangle_test/libdemangle_test
Change-Id: I8d0aea9790b5e86fcc3ea6e2d00cfa33907e2853
diff --git a/cc/binary.go b/cc/binary.go
index 4ddaf94..4b10070 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -15,8 +15,6 @@
 package cc
 
 import (
-	"path/filepath"
-
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
@@ -152,6 +150,10 @@
 				[]string{"libc", "libc_nomalloc", "libcompiler_rt"})
 			deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
 		}
+
+		if ctx.Os() == android.LinuxBionic && !binary.static() {
+			deps.LinkerScript = "host_bionic_linker_script"
+		}
 	}
 
 	if !binary.static() && inList("libc", deps.StaticLibs) {
@@ -243,15 +245,7 @@
 					case android.Android:
 						flags.DynamicLinker = "/system/bin/linker"
 					case android.LinuxBionic:
-						// The linux kernel expects the linker to be an
-						// absolute path
-						path := android.PathForOutput(ctx,
-							"host", "linux_bionic-x86", "bin", "linker")
-						if p, err := filepath.Abs(path.String()); err == nil {
-							flags.DynamicLinker = p
-						} else {
-							ctx.ModuleErrorf("can't find path to dynamic linker: %q", err)
-						}
+						flags.DynamicLinker = ""
 					default:
 						ctx.ModuleErrorf("unknown dynamic linker")
 					}
@@ -304,6 +298,11 @@
 		}
 	}
 
+	if deps.LinkerScript.Valid() {
+		flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String())
+		linkerDeps = append(linkerDeps, deps.LinkerScript.Path())
+	}
+
 	if flags.DynamicLinker != "" {
 		flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
 	}
diff --git a/cc/cc.go b/cc/cc.go
index 0e1d896..af58f9d 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -74,6 +74,7 @@
 	ReexportGeneratedHeaders []string
 
 	CrtBegin, CrtEnd string
+	LinkerScript     string
 }
 
 type PathDeps struct {
@@ -98,6 +99,7 @@
 
 	// Paths to crt*.o files
 	CrtBegin, CrtEnd android.OptionalPath
+	LinkerScript     android.OptionalPath
 }
 
 type Flags struct {
@@ -274,6 +276,7 @@
 	objDepTag             = dependencyTag{name: "obj"}
 	crtBeginDepTag        = dependencyTag{name: "crtbegin"}
 	crtEndDepTag          = dependencyTag{name: "crtend"}
+	linkerScriptDepTag    = dependencyTag{name: "linker script"}
 	reuseObjTag           = dependencyTag{name: "reuse objects"}
 	ndkStubDepTag         = dependencyTag{name: "ndk stub", library: true}
 	ndkLateStubDepTag     = dependencyTag{name: "ndk late stub", library: true}
@@ -834,6 +837,9 @@
 	if deps.CrtEnd != "" {
 		actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
 	}
+	if deps.LinkerScript != "" {
+		actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
+	}
 
 	version := ctx.sdkVersion()
 	actx.AddVariationDependencies([]blueprint.Variation{
@@ -989,6 +995,17 @@
 				} else {
 					ctx.ModuleErrorf("module %q is not a genrule", name)
 				}
+			case linkerScriptDepTag:
+				if genRule, ok := m.(genrule.SourceFileGenerator); ok {
+					files := genRule.GeneratedSourceFiles()
+					if len(files) == 1 {
+						depPaths.LinkerScript = android.OptionalPathForPath(files[0])
+					} else if len(files) > 1 {
+						ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", name)
+					}
+				} else {
+					ctx.ModuleErrorf("module %q is not a genrule", name)
+				}
 			default:
 				ctx.ModuleErrorf("depends on non-cc module %q", name)
 			}