123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- // Copyright 2016 Google Inc. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package cc
- import (
- "android/soong/android"
- "android/soong/cc/config"
- "fmt"
- "path/filepath"
- "github.com/google/blueprint"
- "github.com/google/blueprint/proptools"
- )
- // This file contains the basic functionality for linking against static libraries and shared
- // libraries. Final linking into libraries or executables is handled in library.go, binary.go, etc.
- const (
- packRelocationsDefault = true
- )
- type BaseLinkerProperties struct {
- // list of modules whose object files should be linked into this module
- // in their entirety. For static library modules, all of the .o files from the intermediate
- // directory of the dependency will be linked into this modules .a file. For a shared library,
- // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
- Whole_static_libs []string `android:"arch_variant,variant_prepend"`
- // list of modules that should be statically linked into this module.
- Static_libs []string `android:"arch_variant,variant_prepend"`
- // list of modules that should be dynamically linked into this module.
- Shared_libs []string `android:"arch_variant"`
- // list of modules that should only provide headers for this module.
- Header_libs []string `android:"arch_variant,variant_prepend"`
- // list of module-specific flags that will be used for all link steps
- Ldflags []string `android:"arch_variant"`
- // list of system libraries that will be dynamically linked to
- // shared library and executable modules. If unset, generally defaults to libc,
- // libm, and libdl. Set to [] to prevent linking against the defaults.
- System_shared_libs []string `android:"arch_variant"`
- // allow the module to contain undefined symbols. By default,
- // modules cannot contain undefined symbols that are not satisified by their immediate
- // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
- // This flag should only be necessary for compiling low-level libraries like libc.
- Allow_undefined_symbols *bool `android:"arch_variant"`
- // don't link in libclang_rt.builtins-*.a
- No_libcrt *bool `android:"arch_variant"`
- // Use clang lld instead of gnu ld.
- Use_clang_lld *bool `android:"arch_variant"`
- // -l arguments to pass to linker for host-provided shared libraries
- Host_ldlibs []string `android:"arch_variant"`
- // list of shared libraries to re-export include directories from. Entries must be
- // present in shared_libs.
- Export_shared_lib_headers []string `android:"arch_variant"`
- // list of static libraries to re-export include directories from. Entries must be
- // present in static_libs.
- Export_static_lib_headers []string `android:"arch_variant"`
- // list of header libraries to re-export include directories from. Entries must be
- // present in header_libs.
- Export_header_lib_headers []string `android:"arch_variant"`
- // list of generated headers to re-export include directories from. Entries must be
- // present in generated_headers.
- Export_generated_headers []string `android:"arch_variant"`
- // don't link in crt_begin and crt_end. This flag should only be necessary for
- // compiling crt or libc.
- Nocrt *bool `android:"arch_variant"`
- // deprecated and ignored because lld makes it unnecessary. See b/189475744.
- Group_static_libs *bool `android:"arch_variant"`
- // list of modules that should be installed with this module. This is similar to 'required'
- // but '.vendor' suffix will be appended to the module names if the shared libraries have
- // vendor variants and this module uses VNDK.
- Runtime_libs []string `android:"arch_variant"`
- // list of runtime libs that should not be installed along with this module.
- Exclude_runtime_libs []string `android:"arch_variant"`
- Target struct {
- Vendor, Product struct {
- // list of shared libs that only should be used to build vendor or
- // product variant of the C/C++ module.
- Shared_libs []string
- // list of static libs that only should be used to build vendor or
- // product variant of the C/C++ module.
- Static_libs []string
- // list of ehader libs that only should be used to build vendor or product
- // variant of the C/C++ module.
- Header_libs []string
- // list of shared libs that should not be used to build vendor or
- // product variant of the C/C++ module.
- Exclude_shared_libs []string
- // list of static libs that should not be used to build vendor or
- // product variant of the C/C++ module.
- Exclude_static_libs []string
- // list of header libs that should not be used to build vendor or
- // product variant of the C/C++ module.
- Exclude_header_libs []string
- // list of runtime libs that should not be installed along with the
- // vendor or product variant of the C/C++ module.
- Exclude_runtime_libs []string
- // version script for vendor or product variant
- Version_script *string `android:"arch_variant"`
- } `android:"arch_variant"`
- Recovery struct {
- // list of shared libs that only should be used to build the recovery
- // variant of the C/C++ module.
- Shared_libs []string
- // list of static libs that only should be used to build the recovery
- // variant of the C/C++ module.
- Static_libs []string
- // list of shared libs that should not be used to build
- // the recovery variant of the C/C++ module.
- Exclude_shared_libs []string
- // list of static libs that should not be used to build
- // the recovery variant of the C/C++ module.
- Exclude_static_libs []string
- // list of header libs that should not be used to build the recovery variant
- // of the C/C++ module.
- Exclude_header_libs []string
- // list of runtime libs that should not be installed along with the
- // recovery variant of the C/C++ module.
- Exclude_runtime_libs []string
- }
- Ramdisk struct {
- // list of static libs that only should be used to build the recovery
- // variant of the C/C++ module.
- Static_libs []string
- // list of shared libs that should not be used to build
- // the ramdisk variant of the C/C++ module.
- Exclude_shared_libs []string
- // list of static libs that should not be used to build
- // the ramdisk variant of the C/C++ module.
- Exclude_static_libs []string
- // list of runtime libs that should not be installed along with the
- // ramdisk variant of the C/C++ module.
- Exclude_runtime_libs []string
- }
- Vendor_ramdisk struct {
- // list of shared libs that should not be used to build
- // the recovery variant of the C/C++ module.
- Exclude_shared_libs []string
- // list of static libs that should not be used to build
- // the vendor ramdisk variant of the C/C++ module.
- Exclude_static_libs []string
- // list of runtime libs that should not be installed along with the
- // vendor ramdisk variant of the C/C++ module.
- Exclude_runtime_libs []string
- }
- Platform struct {
- // list of shared libs that should be use to build the platform variant
- // of a module that sets sdk_version. This should rarely be necessary,
- // in most cases the same libraries are available for the SDK and platform
- // variants.
- Shared_libs []string
- // list of ehader libs that only should be used to build platform variant of
- // the C/C++ module.
- Header_libs []string
- // list of shared libs that should not be used to build the platform variant
- // of the C/C++ module.
- Exclude_shared_libs []string
- }
- Apex struct {
- // list of shared libs that should not be used to build the apex variant of
- // the C/C++ module.
- Exclude_shared_libs []string
- // list of static libs that should not be used to build the apex
- // variant of the C/C++ module.
- Exclude_static_libs []string
- }
- } `android:"arch_variant"`
- // make android::build:GetBuildNumber() available containing the build ID.
- Use_version_lib *bool `android:"arch_variant"`
- // Generate compact dynamic relocation table, default true.
- Pack_relocations *bool `android:"arch_variant"`
- // local file name to pass to the linker as --version-script
- Version_script *string `android:"path,arch_variant"`
- // local file name to pass to the linker as --dynamic-list
- Dynamic_list *string `android:"path,arch_variant"`
- // local files to pass to the linker as --script
- Linker_scripts []string `android:"path,arch_variant"`
- // list of static libs that should not be used to build this module
- Exclude_static_libs []string `android:"arch_variant"`
- // list of shared libs that should not be used to build this module
- Exclude_shared_libs []string `android:"arch_variant"`
- }
- func invertBoolPtr(value *bool) *bool {
- if value == nil {
- return nil
- }
- ret := !(*value)
- return &ret
- }
- func (blp *BaseLinkerProperties) crt() *bool {
- val := invertBoolPtr(blp.Nocrt)
- if val != nil && *val {
- // == True
- //
- // Since crt is enabled for almost every module compiling against the Bionic runtime,
- // use `nil` when it's enabled, and rely on the Starlark macro to set it to True by default.
- // This keeps the BUILD files clean.
- return nil
- }
- return val // can be False or nil
- }
- func (blp *BaseLinkerProperties) libCrt() *bool {
- return invertBoolPtr(blp.No_libcrt)
- }
- func NewBaseLinker(sanitize *sanitize) *baseLinker {
- return &baseLinker{sanitize: sanitize}
- }
- // baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
- type baseLinker struct {
- Properties BaseLinkerProperties
- dynamicProperties struct {
- BuildStubs bool `blueprint:"mutated"`
- }
- sanitize *sanitize
- }
- func (linker *baseLinker) appendLdflags(flags []string) {
- linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
- }
- // linkerInit initializes dynamic properties of the linker.
- func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
- }
- func (linker *baseLinker) linkerProps() []interface{} {
- return []interface{}{&linker.Properties, &linker.dynamicProperties}
- }
- func (linker *baseLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
- deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
- deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
- deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
- deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
- deps.RuntimeLibs = append(deps.RuntimeLibs, linker.Properties.Runtime_libs...)
- deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
- deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
- deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
- deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
- deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Exclude_shared_libs)
- deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Exclude_static_libs)
- deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Exclude_static_libs)
- deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Exclude_runtime_libs)
- // Record the libraries that need to be excluded when building for APEX. Unlike other
- // target.*.exclude_* properties, SharedLibs and StaticLibs are not modified here because
- // this module hasn't yet passed the apexMutator. Therefore, we can't tell whether this is
- // an apex variant of not. Record the exclude list in the deps struct for now. The info is
- // used to mark the dependency tag when adding dependencies to the deps. Then inside
- // GenerateAndroidBuildActions, the marked dependencies are ignored (i.e. not used) for APEX
- // variants.
- deps.ExcludeLibsForApex = append(deps.ExcludeLibsForApex, linker.Properties.Target.Apex.Exclude_shared_libs...)
- deps.ExcludeLibsForApex = append(deps.ExcludeLibsForApex, linker.Properties.Target.Apex.Exclude_static_libs...)
- if Bool(linker.Properties.Use_version_lib) {
- deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion")
- }
- if ctx.inVendor() {
- deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Vendor.Shared_libs...)
- deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
- deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
- deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Vendor.Static_libs...)
- deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
- deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Vendor.Header_libs...)
- deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Vendor.Exclude_header_libs)
- deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
- deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
- deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor.Exclude_runtime_libs)
- }
- if ctx.inProduct() {
- deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Product.Shared_libs...)
- deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Product.Exclude_shared_libs)
- deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Product.Exclude_shared_libs)
- deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Product.Static_libs...)
- deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Product.Exclude_static_libs)
- deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Product.Exclude_header_libs)
- deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Product.Exclude_static_libs)
- deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Product.Exclude_static_libs)
- deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Product.Exclude_runtime_libs)
- }
- if ctx.inRecovery() {
- deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Recovery.Shared_libs...)
- deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Recovery.Exclude_shared_libs)
- deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Recovery.Exclude_shared_libs)
- deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Recovery.Static_libs...)
- deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
- deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Recovery.Exclude_header_libs)
- deps.ReexportHeaderLibHeaders = removeListFromList(deps.ReexportHeaderLibHeaders, linker.Properties.Target.Recovery.Exclude_header_libs)
- deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Recovery.Exclude_static_libs)
- deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
- deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Recovery.Exclude_runtime_libs)
- }
- if ctx.inRamdisk() {
- deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Ramdisk.Exclude_shared_libs)
- deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Ramdisk.Exclude_shared_libs)
- deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Ramdisk.Static_libs...)
- deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Ramdisk.Exclude_static_libs)
- deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Ramdisk.Exclude_static_libs)
- deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Ramdisk.Exclude_static_libs)
- deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Ramdisk.Exclude_runtime_libs)
- }
- if ctx.inVendorRamdisk() {
- deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
- deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
- deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
- deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
- deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
- deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_runtime_libs)
- }
- if !ctx.useSdk() {
- deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Platform.Shared_libs...)
- deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Platform.Exclude_shared_libs)
- deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Platform.Header_libs...)
- }
- deps.SystemSharedLibs = linker.Properties.System_shared_libs
- if deps.SystemSharedLibs == nil {
- // Provide a default system_shared_libs if it is unspecified. Note: If an
- // empty list [] is specified, it implies that the module declines the
- // default system_shared_libs.
- deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
- }
- if ctx.toolchain().Bionic() {
- // libclang_rt.builtins has to be last on the command line
- if !Bool(linker.Properties.No_libcrt) && !ctx.header() {
- deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
- }
- if inList("libdl", deps.SharedLibs) {
- // If system_shared_libs has libc but not libdl, make sure shared_libs does not
- // have libdl to avoid loading libdl before libc.
- if inList("libc", deps.SystemSharedLibs) {
- if !inList("libdl", deps.SystemSharedLibs) {
- ctx.PropertyErrorf("shared_libs",
- "libdl must be in system_shared_libs, not shared_libs")
- }
- _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
- }
- }
- // If libc and libdl are both in system_shared_libs make sure libdl comes after libc
- // to avoid loading libdl before libc.
- if inList("libdl", deps.SystemSharedLibs) && inList("libc", deps.SystemSharedLibs) &&
- indexList("libdl", deps.SystemSharedLibs) < indexList("libc", deps.SystemSharedLibs) {
- ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
- }
- } else if ctx.toolchain().Musl() {
- if !Bool(linker.Properties.No_libcrt) && !ctx.header() {
- deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
- }
- }
- deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
- if ctx.Windows() && ctx.ModuleName() != "libwinpthread" {
- deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
- }
- return deps
- }
- func (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
- if linker.Properties.Use_clang_lld != nil {
- return Bool(linker.Properties.Use_clang_lld)
- }
- return true
- }
- // Check whether the SDK version is not older than the specific one
- func CheckSdkVersionAtLeast(ctx ModuleContext, SdkVersion android.ApiLevel) bool {
- if ctx.minSdkVersion() == "current" {
- return true
- }
- parsedSdkVersion, err := nativeApiLevelFromUser(ctx, ctx.minSdkVersion())
- if err != nil {
- ctx.PropertyErrorf("min_sdk_version",
- "Invalid min_sdk_version value (must be int or current): %q",
- ctx.minSdkVersion())
- }
- if parsedSdkVersion.LessThan(SdkVersion) {
- return false
- }
- return true
- }
- // ModuleContext extends BaseModuleContext
- // BaseModuleContext should know if LLD is used?
- func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
- toolchain := ctx.toolchain()
- hod := "Host"
- if ctx.Os().Class == android.Device {
- hod = "Device"
- }
- if linker.useClangLld(ctx) {
- flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod))
- if !BoolDefault(linker.Properties.Pack_relocations, packRelocationsDefault) {
- flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=none")
- } else if ctx.Device() {
- // SHT_RELR relocations are only supported at API level >= 30.
- // ANDROID_RELR relocations were supported at API level >= 28.
- // Relocation packer was supported at API level >= 23.
- // Do the best we can...
- if (!ctx.useSdk() && ctx.minSdkVersion() == "") || CheckSdkVersionAtLeast(ctx, android.FirstShtRelrVersion) {
- flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android+relr")
- } else if CheckSdkVersionAtLeast(ctx, android.FirstAndroidRelrVersion) {
- flags.Global.LdFlags = append(flags.Global.LdFlags,
- "-Wl,--pack-dyn-relocs=android+relr",
- "-Wl,--use-android-relr-tags")
- } else if CheckSdkVersionAtLeast(ctx, android.FirstPackedRelocationsVersion) {
- flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android")
- }
- }
- } else {
- flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
- }
- if Bool(linker.Properties.Allow_undefined_symbols) {
- if ctx.Darwin() {
- // darwin defaults to treating undefined symbols as errors
- flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-undefined,dynamic_lookup")
- }
- } else if !ctx.Darwin() && !ctx.Windows() {
- flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--no-undefined")
- }
- if linker.useClangLld(ctx) {
- flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Lldflags())
- } else {
- flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Ldflags())
- }
- if !ctx.toolchain().Bionic() && ctx.Os() != android.LinuxMusl {
- CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
- flags.Local.LdFlags = append(flags.Local.LdFlags, linker.Properties.Host_ldlibs...)
- if !ctx.Windows() {
- // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
- // builds
- flags.Global.LdFlags = append(flags.Global.LdFlags,
- "-ldl",
- "-lpthread",
- "-lm",
- )
- if !ctx.Darwin() {
- flags.Global.LdFlags = append(flags.Global.LdFlags, "-lrt")
- }
- }
- }
- CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
- flags.Local.LdFlags = append(flags.Local.LdFlags, proptools.NinjaAndShellEscapeList(linker.Properties.Ldflags)...)
- if ctx.Host() && !ctx.Windows() && !ctx.static() {
- flags.Global.LdFlags = append(flags.Global.LdFlags, RpathFlags(ctx)...)
- }
- if ctx.useSdk() {
- // The bionic linker now has support gnu style hashes (which are much faster!), but shipping
- // to older devices requires the old style hash. Fortunately, we can build with both and
- // it'll work anywhere.
- flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--hash-style=both")
- }
- flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.ToolchainLdflags())
- // Version_script is not needed when linking stubs lib where the version
- // script is created from the symbol map file.
- if !linker.dynamicProperties.BuildStubs {
- versionScript := ctx.ExpandOptionalSource(
- linker.Properties.Version_script, "version_script")
- if ctx.inVendor() && linker.Properties.Target.Vendor.Version_script != nil {
- versionScript = ctx.ExpandOptionalSource(
- linker.Properties.Target.Vendor.Version_script,
- "target.vendor.version_script")
- } else if ctx.inProduct() && linker.Properties.Target.Product.Version_script != nil {
- versionScript = ctx.ExpandOptionalSource(
- linker.Properties.Target.Product.Version_script,
- "target.product.version_script")
- }
- if versionScript.Valid() {
- if ctx.Darwin() {
- ctx.PropertyErrorf("version_script", "Not supported on Darwin")
- } else {
- flags.Local.LdFlags = append(flags.Local.LdFlags,
- "-Wl,--version-script,"+versionScript.String())
- flags.LdFlagsDeps = append(flags.LdFlagsDeps, versionScript.Path())
- if linker.sanitize.isSanitizerEnabled(cfi) {
- cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath)
- flags.Local.LdFlags = append(flags.Local.LdFlags,
- "-Wl,--version-script,"+cfiExportsMap.String())
- flags.LdFlagsDeps = append(flags.LdFlagsDeps, cfiExportsMap)
- }
- }
- }
- dynamicList := android.OptionalPathForModuleSrc(ctx, linker.Properties.Dynamic_list)
- if dynamicList.Valid() {
- if ctx.Darwin() {
- ctx.PropertyErrorf("dynamic_list", "Not supported on Darwin")
- } else {
- flags.Local.LdFlags = append(flags.Local.LdFlags,
- "-Wl,--dynamic-list,"+dynamicList.String())
- flags.LdFlagsDeps = append(flags.LdFlagsDeps, dynamicList.Path())
- }
- }
- linkerScriptPaths := android.PathsForModuleSrc(ctx, linker.Properties.Linker_scripts)
- if len(linkerScriptPaths) > 0 && (ctx.Darwin() || ctx.Windows()) {
- ctx.PropertyErrorf("linker_scripts", "Only supported for ELF files")
- } else {
- for _, linkerScriptPath := range linkerScriptPaths {
- flags.Local.LdFlags = append(flags.Local.LdFlags,
- "-Wl,--script,"+linkerScriptPath.String())
- flags.LdFlagsDeps = append(flags.LdFlagsDeps, linkerScriptPath)
- }
- }
- }
- return flags
- }
- // RpathFlags returns the rpath linker flags for current target to search the following directories relative
- // to the binary:
- //
- // - "." to find libraries alongside tests
- // - "lib[64]" to find libraries in a subdirectory of the binaries' directory
- // - "../lib[64]" to find libraries when the binaries are in a bin directory
- // - "../../lib[64]" to find libraries in out/host/linux-x86/lib64 when the test or binary is in
- // out/host/linux-x86/nativetest/<test dir>/<test>
- // - "../../../lib[[64] to find libraries in out/host/linux-x86/lib64 when the test or binary is in
- // out/host/linux-x86/testcases/<test dir>/<CPU>/<test>
- func RpathFlags(ctx android.ModuleContext) []string {
- key := struct {
- os android.OsType
- arch android.ArchType
- }{ctx.Target().Os, ctx.Target().Arch.ArchType}
- return ctx.Config().OnceStringSlice(android.NewCustomOnceKey(key), func() []string {
- rpathPrefix := `\$$ORIGIN/`
- if key.os == android.Darwin {
- rpathPrefix = "@loader_path/"
- }
- var libDir string
- if key.arch.Multilib == "lib64" {
- libDir = "lib64"
- } else {
- libDir = "lib"
- }
- return []string{
- "-Wl,-rpath," + rpathPrefix,
- "-Wl,-rpath," + rpathPrefix + libDir,
- "-Wl,-rpath," + rpathPrefix + filepath.Join("..", libDir),
- "-Wl,-rpath," + rpathPrefix + filepath.Join("../..", libDir),
- "-Wl,-rpath," + rpathPrefix + filepath.Join("../../..", libDir),
- }
- })
- }
- func (linker *baseLinker) link(ctx ModuleContext,
- flags Flags, deps PathDeps, objs Objects) android.Path {
- panic(fmt.Errorf("baseLinker doesn't know how to link"))
- }
- func (linker *baseLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
- specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, linker.Properties.Shared_libs...)
- // Must distinguish nil and [] in system_shared_libs - ensure that [] in
- // either input list doesn't come out as nil.
- if specifiedDeps.systemSharedLibs == nil {
- specifiedDeps.systemSharedLibs = linker.Properties.System_shared_libs
- } else {
- specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, linker.Properties.System_shared_libs...)
- }
- return specifiedDeps
- }
- // Injecting version symbols
- // Some host modules want a version number, but we don't want to rebuild it every time. Optionally add a step
- // after linking that injects a constant placeholder with the current version number.
- func init() {
- pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject")
- }
- var injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol",
- blueprint.RuleParams{
- Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " +
- "-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $$(cat $buildNumberFile)",
- CommandDeps: []string{"$symbolInjectCmd"},
- },
- "buildNumberFile")
- func (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) {
- buildNumberFile := ctx.Config().BuildNumberFile(ctx)
- ctx.Build(pctx, android.BuildParams{
- Rule: injectVersionSymbol,
- Description: "inject version symbol",
- Input: in,
- Output: out,
- OrderOnly: android.Paths{buildNumberFile},
- Args: map[string]string{
- "buildNumberFile": buildNumberFile.String(),
- },
- })
- }
|