Refactor linker/Android.bp for native-bridge

Define a "linker_bin_template" cc_defaults module that a native bridge
implementation can inherit to define a guest linker.

Break the debuggerd_init call off into separate
linker_debuggerd_{android,stub}.cpp files to allow opting in/out of the
debuggerd integration without needing to change how linker_main.cpp is
compiled. (This is necessary for a later commit that moves
linker_main.cpp into a new static library.)

Test: bionic unit tests
Bug: none
Merged-In: I7c5d79281bce1e69817b266dd91d43ea40f78522
Change-Id: I7c5d79281bce1e69817b266dd91d43ea40f78522
(cherry picked from commit 5adf402ee9d65c803b71d93b9e3de77b36ccb027)
diff --git a/linker/Android.bp b/linker/Android.bp
index bb9d26d..8061f40 100644
--- a/linker/Android.bp
+++ b/linker/Android.bp
@@ -1,22 +1,6 @@
-cc_library_static {
-    name: "liblinker_malloc",
-    defaults: ["linux_bionic_supported"],
-    recovery_available: true,
-    native_bridge_supported: true,
-
-    srcs: [
-        "linker_memory.cpp",
-    ],
-    cflags: [
-        "-Wall",
-        "-Werror",
-    ],
-
-    // We need to access Bionic private headers in the linker.
-    include_dirs: ["bionic/libc"],
-
-    static_libs: ["libasync_safe", "libbase"],
-}
+// ========================================================
+// linker_wrapper - Linux Bionic (on the host)
+// ========================================================
 
 // This is used for bionic on (host) Linux to bootstrap our linker embedded into
 // a binary.
@@ -66,6 +50,94 @@
     include_dirs: ["bionic/libc"],
 }
 
+// ========================================================
+// linker default configuration
+// ========================================================
+
+// Configuration for the linker binary and any of its static libraries.
+cc_defaults {
+    name: "linker_defaults",
+    arch: {
+        arm: {
+            cflags: ["-D__work_around_b_24465209__"],
+        },
+        x86: {
+            cflags: ["-D__work_around_b_24465209__"],
+        },
+    },
+
+    cflags: [
+        "-fno-stack-protector",
+        "-Wstrict-overflow=5",
+        "-fvisibility=hidden",
+        "-Wall",
+        "-Wextra",
+        "-Wunused",
+        "-Werror",
+    ],
+
+    // TODO: split out the asflags.
+    asflags: [
+        "-fno-stack-protector",
+        "-Wstrict-overflow=5",
+        "-fvisibility=hidden",
+        "-Wall",
+        "-Wextra",
+        "-Wunused",
+        "-Werror",
+    ],
+
+    product_variables: {
+        debuggable: {
+            cppflags: ["-DUSE_LD_CONFIG_FILE"],
+        },
+    },
+
+    cppflags: ["-Wold-style-cast"],
+
+    static_libs: [
+        "libziparchive",
+        "libbase",
+        "libz",
+
+        "libasync_safe",
+
+        "liblog",
+    ],
+
+    // We need to access Bionic private headers in the linker.
+    include_dirs: ["bionic/libc"],
+}
+
+// ========================================================
+// linker components
+// ========================================================
+
+// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
+// native bridge implementations).
+cc_defaults {
+    name: "linker_all_targets",
+    defaults: ["linux_bionic_supported"],
+    recovery_available: true,
+    native_bridge_supported: true,
+}
+
+cc_library_static {
+    name: "liblinker_malloc",
+    defaults: ["linker_defaults", "linker_all_targets"],
+    srcs: ["linker_memory.cpp"],
+}
+
+cc_library_static {
+    name: "liblinker_debuggerd_stub",
+    defaults: ["linker_defaults", "linker_all_targets"],
+    srcs: ["linker_debuggerd_stub.cpp"],
+}
+
+// ========================================================
+// template for the linker binary
+// ========================================================
+
 filegroup {
     name: "linker_sources",
     srcs: [
@@ -137,30 +209,50 @@
     ],
 }
 
-filegroup {
-    name: "linker_version_script",
-    srcs: ["linker.generic.map"],
-}
-
-filegroup {
-    name: "linker_version_script_arm",
-    srcs: ["linker.arm.map"],
-}
-
 cc_defaults {
-    name: "linker_defaults",
+    name: "linker_version_script_overlay",
+    arch: {
+        arm:    { version_script: "linker.arm.map"      },
+        arm64:  { version_script: "linker.generic.map"  },
+        x86:    { version_script: "linker.generic.map"  },
+        x86_64: { version_script: "linker.generic.map"  },
+        mips:   { version_script: "linker.generic.map"  },
+        mips64: { version_script: "linker.generic.map"  },
+    },
+}
+
+// A template for the linker binary. May be inherited by native bridge implementations.
+cc_defaults {
+    name: "linker_bin_template",
+    defaults: ["linker_defaults"],
+
+    srcs: [":linker_sources"],
+
     arch: {
         arm: {
-            cflags: ["-D__work_around_b_24465209__"],
+            srcs: [":linker_sources_arm"],
+            static_libs: ["libunwind_llvm"],
+        },
+        arm64: {
+            srcs: [":linker_sources_arm64"],
         },
         x86: {
-            cflags: ["-D__work_around_b_24465209__"],
+            srcs: [":linker_sources_x86"],
+        },
+        x86_64: {
+            srcs: [":linker_sources_x86_64"],
+        },
+        mips: {
+            srcs: [":linker_sources_mips"],
+        },
+        mips64: {
+            srcs: [":linker_sources_mips64"],
         },
     },
 
-    // -shared is used to overwrite the -Bstatic and -static
-    // flags triggered by LOCAL_FORCE_STATIC_EXECUTABLE.
-    // This dynamic linker is actually a shared object linked with static libraries.
+    // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
+    // static_executable. This dynamic linker is actually a shared object linked with static
+    // libraries.
     ldflags: [
         "-shared",
         "-Wl,-Bsymbolic",
@@ -168,35 +260,6 @@
         "-Wl,-soname,ld-android.so",
     ],
 
-    cflags: [
-        "-fno-stack-protector",
-        "-Wstrict-overflow=5",
-        "-fvisibility=hidden",
-        "-Wall",
-        "-Wextra",
-        "-Wunused",
-        "-Werror",
-    ],
-
-    // TODO: split out the asflags.
-    asflags: [
-        "-fno-stack-protector",
-        "-Wstrict-overflow=5",
-        "-fvisibility=hidden",
-        "-Wall",
-        "-Wextra",
-        "-Wunused",
-        "-Werror",
-    ],
-
-    product_variables: {
-        debuggable: {
-            cppflags: ["-DUSE_LD_CONFIG_FILE"],
-        },
-    },
-
-    cppflags: ["-Wold-style-cast"],
-
     // we are going to link libc++_static manually because
     // when stl is not set to "none" build system adds libdl
     // to the list of static libraries which needs to be
@@ -222,58 +285,13 @@
     sanitize: {
         hwaddress: false,
     },
-}
-
-cc_binary {
-    defaults: ["linux_bionic_supported", "linker_defaults"],
-    srcs: [ ":linker_sources" ],
-
-    arch: {
-        arm: {
-            srcs: [ ":linker_sources_arm" ],
-            version_script: ":linker_version_script_arm",
-            static_libs: ["libunwind_llvm"],
-        },
-        arm64: {
-            srcs: [":linker_sources_arm64"],
-            version_script: ":linker_version_script",
-        },
-        x86: {
-            srcs: [":linker_sources_x86"],
-            version_script: ":linker_version_script",
-        },
-        x86_64: {
-            srcs: [":linker_sources_x86_64"],
-            version_script: ":linker_version_script",
-        },
-        mips: {
-            srcs: [":linker_sources_mips"],
-            version_script: ":linker_version_script",
-        },
-        mips64: {
-            srcs: [":linker_sources_mips64"],
-            version_script: ":linker_version_script",
-        },
-    },
-
-    // We need to access Bionic private headers in the linker.
-    include_dirs: ["bionic/libc"],
 
     static_libs: [
+        "liblinker_malloc",
+
+        "libc++_static",
         "libc_nomalloc",
         "libm",
-        "libziparchive",
-        "libbase",
-        "libz",
-
-        "libasync_safe",
-
-        "liblog",
-        "libc++_static",
-
-        // Important: The liblinker_malloc should be the last library in the list
-        // to overwrite any other malloc implementations by other static libraries.
-        "liblinker_malloc",
     ],
 
     // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
@@ -287,9 +305,25 @@
     // linker stops linking against libgcc.a's arm32 unwinder.
     whole_static_libs: ["libc_unwind_static"],
 
+    system_shared_libs: [],
+
+    // Opt out of native_coverage when opting out of system_shared_libs
+    native_coverage: false,
+}
+
+// ========================================================
+// linker[_asan][64] binary
+// ========================================================
+
+cc_binary {
     name: "linker",
+    defaults: [
+        "linker_bin_template",
+        "linux_bionic_supported",
+        "linker_version_script_overlay",
+    ],
+
     symlinks: ["linker_asan"],
-    recovery_available: true,
     multilib: {
         lib32: {
             cflags: ["-DLIB_PATH=\"lib\""],
@@ -299,28 +333,38 @@
             suffix: "64",
         },
     },
-    system_shared_libs: [],
 
-    // Opt out of native_coverage when opting out of system_shared_libs
-    native_coverage: false,
+    compile_multilib: "both",
+    xom: false,
+
+    recovery_available: true,
+    apex_available: [
+        "//apex_available:platform",
+        "com.android.runtime",
+    ],
 
     target: {
         android: {
+            srcs: [
+                "linker_debuggerd_android.cpp",
+            ],
             static_libs: [
                 "libc++demangle",
                 "libdebuggerd_handler_fallback",
             ],
         },
+        linux_bionic: {
+            static_libs: [
+                "liblinker_debuggerd_stub",
+            ],
+        }
     },
-    compile_multilib: "both",
-    xom: false,
-
-    apex_available: [
-        "//apex_available:platform",
-        "com.android.runtime",
-    ],
 }
 
+// ========================================================
+// assorted modules
+// ========================================================
+
 sh_binary {
     name: "ldd",
     src: "ldd",
@@ -347,25 +391,11 @@
 
     // for x86, exclude libgcc_eh.a for the same reasons as above
     arch: {
-        arm: {
-            version_script: "linker.arm.map",
-        },
-        arm64: {
-            version_script: "linker.generic.map",
-        },
         x86: {
             ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
-            version_script: "linker.generic.map",
         },
         x86_64: {
             ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
-            version_script: "linker.generic.map",
-        },
-        mips: {
-            version_script: "linker.generic.map",
-        },
-        mips64: {
-            version_script: "linker.generic.map",
         },
     },
 
@@ -379,7 +409,7 @@
     stl: "none",
 
     name: "ld-android",
-    defaults: ["linux_bionic_supported"],
+    defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
     recovery_available: true,
     native_bridge_supported: true,