linker.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // Copyright 2016 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cc
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "android/soong/android"
  19. "android/soong/cc/config"
  20. "github.com/google/blueprint"
  21. "github.com/google/blueprint/proptools"
  22. )
  23. // This file contains the basic functionality for linking against static libraries and shared
  24. // libraries. Final linking into libraries or executables is handled in library.go, binary.go, etc.
  25. const (
  26. packRelocationsDefault = true
  27. )
  28. type BaseLinkerProperties struct {
  29. // list of modules whose object files should be linked into this module
  30. // in their entirety. For static library modules, all of the .o files from the intermediate
  31. // directory of the dependency will be linked into this modules .a file. For a shared library,
  32. // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
  33. Whole_static_libs []string `android:"arch_variant,variant_prepend"`
  34. // list of modules that should be statically linked into this module.
  35. Static_libs []string `android:"arch_variant,variant_prepend"`
  36. // list of modules that should be dynamically linked into this module.
  37. Shared_libs []string `android:"arch_variant"`
  38. // list of modules that should only provide headers for this module.
  39. Header_libs []string `android:"arch_variant,variant_prepend"`
  40. // list of module-specific flags that will be used for all link steps
  41. Ldflags []string `android:"arch_variant"`
  42. // list of system libraries that will be dynamically linked to
  43. // shared library and executable modules. If unset, generally defaults to libc,
  44. // libm, and libdl. Set to [] to prevent linking against the defaults.
  45. System_shared_libs []string `android:"arch_variant"`
  46. // allow the module to contain undefined symbols. By default,
  47. // modules cannot contain undefined symbols that are not satisified by their immediate
  48. // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
  49. // This flag should only be necessary for compiling low-level libraries like libc.
  50. Allow_undefined_symbols *bool `android:"arch_variant"`
  51. // don't link in libclang_rt.builtins-*.a
  52. No_libcrt *bool `android:"arch_variant"`
  53. // Use clang lld instead of gnu ld.
  54. Use_clang_lld *bool `android:"arch_variant"`
  55. // -l arguments to pass to linker for host-provided shared libraries
  56. Host_ldlibs []string `android:"arch_variant"`
  57. // list of shared libraries to re-export include directories from. Entries must be
  58. // present in shared_libs.
  59. Export_shared_lib_headers []string `android:"arch_variant"`
  60. // list of static libraries to re-export include directories from. Entries must be
  61. // present in static_libs.
  62. Export_static_lib_headers []string `android:"arch_variant"`
  63. // list of header libraries to re-export include directories from. Entries must be
  64. // present in header_libs.
  65. Export_header_lib_headers []string `android:"arch_variant"`
  66. // list of generated headers to re-export include directories from. Entries must be
  67. // present in generated_headers.
  68. Export_generated_headers []string `android:"arch_variant"`
  69. // don't link in crt_begin and crt_end. This flag should only be necessary for
  70. // compiling crt or libc.
  71. Nocrt *bool `android:"arch_variant"`
  72. // deprecated and ignored because lld makes it unnecessary. See b/189475744.
  73. Group_static_libs *bool `android:"arch_variant"`
  74. // list of modules that should be installed with this module. This is similar to 'required'
  75. // but '.vendor' suffix will be appended to the module names if the shared libraries have
  76. // vendor variants and this module uses VNDK.
  77. Runtime_libs []string `android:"arch_variant"`
  78. // list of runtime libs that should not be installed along with this module.
  79. Exclude_runtime_libs []string `android:"arch_variant"`
  80. Target struct {
  81. Vendor, Product struct {
  82. // list of shared libs that only should be used to build vendor or
  83. // product variant of the C/C++ module.
  84. Shared_libs []string
  85. // list of static libs that only should be used to build vendor or
  86. // product variant of the C/C++ module.
  87. Static_libs []string
  88. // list of ehader libs that only should be used to build vendor or product
  89. // variant of the C/C++ module.
  90. Header_libs []string
  91. // list of shared libs that should not be used to build vendor or
  92. // product variant of the C/C++ module.
  93. Exclude_shared_libs []string
  94. // list of static libs that should not be used to build vendor or
  95. // product variant of the C/C++ module.
  96. Exclude_static_libs []string
  97. // list of header libs that should not be used to build vendor or
  98. // product variant of the C/C++ module.
  99. Exclude_header_libs []string
  100. // list of runtime libs that should not be installed along with the
  101. // vendor or product variant of the C/C++ module.
  102. Exclude_runtime_libs []string
  103. // version script for vendor or product variant
  104. Version_script *string `android:"arch_variant"`
  105. } `android:"arch_variant"`
  106. Recovery struct {
  107. // list of shared libs that only should be used to build the recovery
  108. // variant of the C/C++ module.
  109. Shared_libs []string
  110. // list of static libs that only should be used to build the recovery
  111. // variant of the C/C++ module.
  112. Static_libs []string
  113. // list of shared libs that should not be used to build
  114. // the recovery variant of the C/C++ module.
  115. Exclude_shared_libs []string
  116. // list of static libs that should not be used to build
  117. // the recovery variant of the C/C++ module.
  118. Exclude_static_libs []string
  119. // list of header libs that should not be used to build the recovery variant
  120. // of the C/C++ module.
  121. Exclude_header_libs []string
  122. // list of runtime libs that should not be installed along with the
  123. // recovery variant of the C/C++ module.
  124. Exclude_runtime_libs []string
  125. }
  126. Ramdisk struct {
  127. // list of static libs that only should be used to build the recovery
  128. // variant of the C/C++ module.
  129. Static_libs []string
  130. // list of shared libs that should not be used to build
  131. // the ramdisk variant of the C/C++ module.
  132. Exclude_shared_libs []string
  133. // list of static libs that should not be used to build
  134. // the ramdisk variant of the C/C++ module.
  135. Exclude_static_libs []string
  136. // list of runtime libs that should not be installed along with the
  137. // ramdisk variant of the C/C++ module.
  138. Exclude_runtime_libs []string
  139. }
  140. Vendor_ramdisk struct {
  141. // list of shared libs that should not be used to build
  142. // the recovery variant of the C/C++ module.
  143. Exclude_shared_libs []string
  144. // list of static libs that should not be used to build
  145. // the vendor ramdisk variant of the C/C++ module.
  146. Exclude_static_libs []string
  147. // list of runtime libs that should not be installed along with the
  148. // vendor ramdisk variant of the C/C++ module.
  149. Exclude_runtime_libs []string
  150. }
  151. Platform struct {
  152. // list of shared libs that should be use to build the platform variant
  153. // of a module that sets sdk_version. This should rarely be necessary,
  154. // in most cases the same libraries are available for the SDK and platform
  155. // variants.
  156. Shared_libs []string
  157. // list of ehader libs that only should be used to build platform variant of
  158. // the C/C++ module.
  159. Header_libs []string
  160. // list of shared libs that should not be used to build the platform variant
  161. // of the C/C++ module.
  162. Exclude_shared_libs []string
  163. }
  164. Apex struct {
  165. // list of shared libs that should not be used to build the apex variant of
  166. // the C/C++ module.
  167. Exclude_shared_libs []string
  168. // list of static libs that should not be used to build the apex
  169. // variant of the C/C++ module.
  170. Exclude_static_libs []string
  171. }
  172. } `android:"arch_variant"`
  173. // make android::build:GetBuildNumber() available containing the build ID.
  174. Use_version_lib *bool `android:"arch_variant"`
  175. // Generate compact dynamic relocation table, default true.
  176. Pack_relocations *bool `android:"arch_variant"`
  177. // local file name to pass to the linker as --version-script
  178. Version_script *string `android:"path,arch_variant"`
  179. // local file name to pass to the linker as --dynamic-list
  180. Dynamic_list *string `android:"path,arch_variant"`
  181. // local files to pass to the linker as --script
  182. Linker_scripts []string `android:"path,arch_variant"`
  183. // list of static libs that should not be used to build this module
  184. Exclude_static_libs []string `android:"arch_variant"`
  185. // list of shared libs that should not be used to build this module
  186. Exclude_shared_libs []string `android:"arch_variant"`
  187. }
  188. func (blp *BaseLinkerProperties) crt() bool {
  189. // Since crt is enabled for almost every module compiling against the Bionic runtime,
  190. // we interpret `nil` as enabled.
  191. return blp.Nocrt == nil || !*blp.Nocrt
  192. }
  193. func (blp *BaseLinkerProperties) libCrt() bool {
  194. return blp.No_libcrt == nil || !*blp.No_libcrt
  195. }
  196. func NewBaseLinker(sanitize *sanitize) *baseLinker {
  197. return &baseLinker{sanitize: sanitize}
  198. }
  199. // baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
  200. type baseLinker struct {
  201. Properties BaseLinkerProperties
  202. dynamicProperties struct {
  203. BuildStubs bool `blueprint:"mutated"`
  204. }
  205. sanitize *sanitize
  206. }
  207. func (linker *baseLinker) appendLdflags(flags []string) {
  208. linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
  209. }
  210. // linkerInit initializes dynamic properties of the linker.
  211. func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
  212. }
  213. func (linker *baseLinker) linkerProps() []interface{} {
  214. return []interface{}{&linker.Properties, &linker.dynamicProperties}
  215. }
  216. func (linker *baseLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
  217. deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
  218. deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
  219. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
  220. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
  221. deps.RuntimeLibs = append(deps.RuntimeLibs, linker.Properties.Runtime_libs...)
  222. deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
  223. deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
  224. deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
  225. deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
  226. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Exclude_shared_libs)
  227. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Exclude_static_libs)
  228. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Exclude_static_libs)
  229. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Exclude_runtime_libs)
  230. // Record the libraries that need to be excluded when building for APEX. Unlike other
  231. // target.*.exclude_* properties, SharedLibs and StaticLibs are not modified here because
  232. // this module hasn't yet passed the apexMutator. Therefore, we can't tell whether this is
  233. // an apex variant of not. Record the exclude list in the deps struct for now. The info is
  234. // used to mark the dependency tag when adding dependencies to the deps. Then inside
  235. // GenerateAndroidBuildActions, the marked dependencies are ignored (i.e. not used) for APEX
  236. // variants.
  237. deps.ExcludeLibsForApex = append(deps.ExcludeLibsForApex, linker.Properties.Target.Apex.Exclude_shared_libs...)
  238. deps.ExcludeLibsForApex = append(deps.ExcludeLibsForApex, linker.Properties.Target.Apex.Exclude_static_libs...)
  239. if Bool(linker.Properties.Use_version_lib) {
  240. deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion")
  241. }
  242. if ctx.inVendor() {
  243. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Vendor.Shared_libs...)
  244. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
  245. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
  246. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Vendor.Static_libs...)
  247. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
  248. deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Vendor.Header_libs...)
  249. deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Vendor.Exclude_header_libs)
  250. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
  251. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
  252. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor.Exclude_runtime_libs)
  253. }
  254. if ctx.inProduct() {
  255. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Product.Shared_libs...)
  256. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Product.Exclude_shared_libs)
  257. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Product.Exclude_shared_libs)
  258. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Product.Static_libs...)
  259. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Product.Exclude_static_libs)
  260. deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Product.Exclude_header_libs)
  261. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Product.Exclude_static_libs)
  262. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Product.Exclude_static_libs)
  263. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Product.Exclude_runtime_libs)
  264. }
  265. if ctx.inRecovery() {
  266. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Recovery.Shared_libs...)
  267. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Recovery.Exclude_shared_libs)
  268. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Recovery.Exclude_shared_libs)
  269. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Recovery.Static_libs...)
  270. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
  271. deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Recovery.Exclude_header_libs)
  272. deps.ReexportHeaderLibHeaders = removeListFromList(deps.ReexportHeaderLibHeaders, linker.Properties.Target.Recovery.Exclude_header_libs)
  273. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Recovery.Exclude_static_libs)
  274. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
  275. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Recovery.Exclude_runtime_libs)
  276. }
  277. if ctx.inRamdisk() {
  278. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Ramdisk.Exclude_shared_libs)
  279. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Ramdisk.Exclude_shared_libs)
  280. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Ramdisk.Static_libs...)
  281. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Ramdisk.Exclude_static_libs)
  282. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Ramdisk.Exclude_static_libs)
  283. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Ramdisk.Exclude_static_libs)
  284. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Ramdisk.Exclude_runtime_libs)
  285. }
  286. if ctx.inVendorRamdisk() {
  287. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
  288. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
  289. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
  290. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
  291. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
  292. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_runtime_libs)
  293. }
  294. if !ctx.useSdk() {
  295. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Platform.Shared_libs...)
  296. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Platform.Exclude_shared_libs)
  297. deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Platform.Header_libs...)
  298. }
  299. deps.SystemSharedLibs = linker.Properties.System_shared_libs
  300. if deps.SystemSharedLibs == nil {
  301. // Provide a default system_shared_libs if it is unspecified. Note: If an
  302. // empty list [] is specified, it implies that the module declines the
  303. // default system_shared_libs.
  304. deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
  305. }
  306. if ctx.toolchain().Bionic() {
  307. // libclang_rt.builtins has to be last on the command line
  308. if linker.Properties.libCrt() && !ctx.header() {
  309. deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
  310. }
  311. if inList("libdl", deps.SharedLibs) {
  312. // If system_shared_libs has libc but not libdl, make sure shared_libs does not
  313. // have libdl to avoid loading libdl before libc.
  314. if inList("libc", deps.SystemSharedLibs) {
  315. if !inList("libdl", deps.SystemSharedLibs) {
  316. ctx.PropertyErrorf("shared_libs",
  317. "libdl must be in system_shared_libs, not shared_libs")
  318. }
  319. _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
  320. }
  321. }
  322. // If libc and libdl are both in system_shared_libs make sure libdl comes after libc
  323. // to avoid loading libdl before libc.
  324. if inList("libdl", deps.SystemSharedLibs) && inList("libc", deps.SystemSharedLibs) &&
  325. indexList("libdl", deps.SystemSharedLibs) < indexList("libc", deps.SystemSharedLibs) {
  326. ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
  327. }
  328. } else if ctx.toolchain().Musl() {
  329. if linker.Properties.libCrt() && !ctx.header() {
  330. deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
  331. }
  332. }
  333. deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
  334. if ctx.Windows() && ctx.ModuleName() != "libwinpthread" {
  335. deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
  336. }
  337. return deps
  338. }
  339. func (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
  340. if linker.Properties.Use_clang_lld != nil {
  341. return Bool(linker.Properties.Use_clang_lld)
  342. }
  343. return true
  344. }
  345. // Check whether the SDK version is not older than the specific one
  346. func CheckSdkVersionAtLeast(ctx ModuleContext, SdkVersion android.ApiLevel) bool {
  347. if ctx.minSdkVersion() == "current" {
  348. return true
  349. }
  350. parsedSdkVersion, err := nativeApiLevelFromUser(ctx, ctx.minSdkVersion())
  351. if err != nil {
  352. ctx.PropertyErrorf("min_sdk_version",
  353. "Invalid min_sdk_version value (must be int or current): %q",
  354. ctx.minSdkVersion())
  355. }
  356. if parsedSdkVersion.LessThan(SdkVersion) {
  357. return false
  358. }
  359. return true
  360. }
  361. // ModuleContext extends BaseModuleContext
  362. // BaseModuleContext should know if LLD is used?
  363. func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
  364. toolchain := ctx.toolchain()
  365. hod := "Host"
  366. if ctx.Os().Class == android.Device {
  367. hod = "Device"
  368. }
  369. if linker.useClangLld(ctx) {
  370. flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod))
  371. if !BoolDefault(linker.Properties.Pack_relocations, packRelocationsDefault) {
  372. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=none")
  373. } else if ctx.Device() {
  374. // SHT_RELR relocations are only supported at API level >= 30.
  375. // ANDROID_RELR relocations were supported at API level >= 28.
  376. // Relocation packer was supported at API level >= 23.
  377. // Do the best we can...
  378. if (!ctx.useSdk() && ctx.minSdkVersion() == "") || CheckSdkVersionAtLeast(ctx, android.FirstShtRelrVersion) {
  379. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android+relr")
  380. } else if CheckSdkVersionAtLeast(ctx, android.FirstAndroidRelrVersion) {
  381. flags.Global.LdFlags = append(flags.Global.LdFlags,
  382. "-Wl,--pack-dyn-relocs=android+relr",
  383. "-Wl,--use-android-relr-tags")
  384. } else if CheckSdkVersionAtLeast(ctx, android.FirstPackedRelocationsVersion) {
  385. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android")
  386. }
  387. }
  388. } else {
  389. flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
  390. }
  391. if Bool(linker.Properties.Allow_undefined_symbols) {
  392. if ctx.Darwin() {
  393. // darwin defaults to treating undefined symbols as errors
  394. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-undefined,dynamic_lookup")
  395. }
  396. } else if !ctx.Darwin() && !ctx.Windows() {
  397. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--no-undefined")
  398. }
  399. if linker.useClangLld(ctx) {
  400. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Lldflags())
  401. } else {
  402. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Ldflags())
  403. }
  404. if !ctx.toolchain().Bionic() && ctx.Os() != android.LinuxMusl {
  405. CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
  406. flags.Local.LdFlags = append(flags.Local.LdFlags, linker.Properties.Host_ldlibs...)
  407. if !ctx.Windows() {
  408. // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
  409. // builds
  410. flags.Global.LdFlags = append(flags.Global.LdFlags,
  411. "-ldl",
  412. "-lpthread",
  413. "-lm",
  414. )
  415. if !ctx.Darwin() {
  416. flags.Global.LdFlags = append(flags.Global.LdFlags, "-lrt")
  417. }
  418. }
  419. }
  420. CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
  421. flags.Local.LdFlags = append(flags.Local.LdFlags, proptools.NinjaAndShellEscapeList(linker.Properties.Ldflags)...)
  422. if ctx.Host() && !ctx.Windows() && !ctx.static() {
  423. flags.Global.LdFlags = append(flags.Global.LdFlags, RpathFlags(ctx)...)
  424. }
  425. if ctx.useSdk() {
  426. // The bionic linker now has support gnu style hashes (which are much faster!), but shipping
  427. // to older devices requires the old style hash. Fortunately, we can build with both and
  428. // it'll work anywhere.
  429. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--hash-style=both")
  430. }
  431. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.ToolchainLdflags())
  432. // Version_script is not needed when linking stubs lib where the version
  433. // script is created from the symbol map file.
  434. if !linker.dynamicProperties.BuildStubs {
  435. versionScript := ctx.ExpandOptionalSource(
  436. linker.Properties.Version_script, "version_script")
  437. if ctx.inVendor() && linker.Properties.Target.Vendor.Version_script != nil {
  438. versionScript = ctx.ExpandOptionalSource(
  439. linker.Properties.Target.Vendor.Version_script,
  440. "target.vendor.version_script")
  441. } else if ctx.inProduct() && linker.Properties.Target.Product.Version_script != nil {
  442. versionScript = ctx.ExpandOptionalSource(
  443. linker.Properties.Target.Product.Version_script,
  444. "target.product.version_script")
  445. }
  446. if versionScript.Valid() {
  447. if ctx.Darwin() {
  448. ctx.PropertyErrorf("version_script", "Not supported on Darwin")
  449. } else {
  450. flags.Local.LdFlags = append(flags.Local.LdFlags,
  451. config.VersionScriptFlagPrefix+versionScript.String())
  452. flags.LdFlagsDeps = append(flags.LdFlagsDeps, versionScript.Path())
  453. if linker.sanitize.isSanitizerEnabled(cfi) {
  454. cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath+"/"+cfiExportsMapFilename)
  455. flags.Local.LdFlags = append(flags.Local.LdFlags,
  456. config.VersionScriptFlagPrefix+cfiExportsMap.String())
  457. flags.LdFlagsDeps = append(flags.LdFlagsDeps, cfiExportsMap)
  458. }
  459. }
  460. }
  461. dynamicList := android.OptionalPathForModuleSrc(ctx, linker.Properties.Dynamic_list)
  462. if dynamicList.Valid() {
  463. if ctx.Darwin() {
  464. ctx.PropertyErrorf("dynamic_list", "Not supported on Darwin")
  465. } else {
  466. flags.Local.LdFlags = append(flags.Local.LdFlags,
  467. "-Wl,--dynamic-list,"+dynamicList.String())
  468. flags.LdFlagsDeps = append(flags.LdFlagsDeps, dynamicList.Path())
  469. }
  470. }
  471. linkerScriptPaths := android.PathsForModuleSrc(ctx, linker.Properties.Linker_scripts)
  472. if len(linkerScriptPaths) > 0 && (ctx.Darwin() || ctx.Windows()) {
  473. ctx.PropertyErrorf("linker_scripts", "Only supported for ELF files")
  474. } else {
  475. for _, linkerScriptPath := range linkerScriptPaths {
  476. flags.Local.LdFlags = append(flags.Local.LdFlags,
  477. "-Wl,--script,"+linkerScriptPath.String())
  478. flags.LdFlagsDeps = append(flags.LdFlagsDeps, linkerScriptPath)
  479. }
  480. }
  481. }
  482. return flags
  483. }
  484. // RpathFlags returns the rpath linker flags for current target to search the following directories relative
  485. // to the binary:
  486. //
  487. // - "." to find libraries alongside tests
  488. // - "lib[64]" to find libraries in a subdirectory of the binaries' directory
  489. // - "../lib[64]" to find libraries when the binaries are in a bin directory
  490. // - "../../lib[64]" to find libraries in out/host/linux-x86/lib64 when the test or binary is in
  491. // out/host/linux-x86/nativetest/<test dir>/<test>
  492. // - "../../../lib[[64] to find libraries in out/host/linux-x86/lib64 when the test or binary is in
  493. // out/host/linux-x86/testcases/<test dir>/<CPU>/<test>
  494. func RpathFlags(ctx android.ModuleContext) []string {
  495. key := struct {
  496. os android.OsType
  497. arch android.ArchType
  498. }{ctx.Target().Os, ctx.Target().Arch.ArchType}
  499. return ctx.Config().OnceStringSlice(android.NewCustomOnceKey(key), func() []string {
  500. rpathPrefix := `\$$ORIGIN/`
  501. if key.os == android.Darwin {
  502. rpathPrefix = "@loader_path/"
  503. }
  504. var libDir string
  505. if key.arch.Multilib == "lib64" {
  506. libDir = "lib64"
  507. } else {
  508. libDir = "lib"
  509. }
  510. return []string{
  511. "-Wl,-rpath," + rpathPrefix,
  512. "-Wl,-rpath," + rpathPrefix + libDir,
  513. "-Wl,-rpath," + rpathPrefix + filepath.Join("..", libDir),
  514. "-Wl,-rpath," + rpathPrefix + filepath.Join("../..", libDir),
  515. "-Wl,-rpath," + rpathPrefix + filepath.Join("../../..", libDir),
  516. }
  517. })
  518. }
  519. func (linker *baseLinker) link(ctx ModuleContext,
  520. flags Flags, deps PathDeps, objs Objects) android.Path {
  521. panic(fmt.Errorf("baseLinker doesn't know how to link"))
  522. }
  523. func (linker *baseLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
  524. specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, linker.Properties.Shared_libs...)
  525. // Must distinguish nil and [] in system_shared_libs - ensure that [] in
  526. // either input list doesn't come out as nil.
  527. if specifiedDeps.systemSharedLibs == nil {
  528. specifiedDeps.systemSharedLibs = linker.Properties.System_shared_libs
  529. } else {
  530. specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, linker.Properties.System_shared_libs...)
  531. }
  532. return specifiedDeps
  533. }
  534. // Injecting version symbols
  535. // Some host modules want a version number, but we don't want to rebuild it every time. Optionally add a step
  536. // after linking that injects a constant placeholder with the current version number.
  537. func init() {
  538. pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject")
  539. }
  540. var injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol",
  541. blueprint.RuleParams{
  542. Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " +
  543. "-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $$(cat $buildNumberFile)",
  544. CommandDeps: []string{"$symbolInjectCmd"},
  545. },
  546. "buildNumberFile")
  547. func (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) {
  548. buildNumberFile := ctx.Config().BuildNumberFile(ctx)
  549. ctx.Build(pctx, android.BuildParams{
  550. Rule: injectVersionSymbol,
  551. Description: "inject version symbol",
  552. Input: in,
  553. Output: out,
  554. OrderOnly: android.Paths{buildNumberFile},
  555. Args: map[string]string{
  556. "buildNumberFile": buildNumberFile.String(),
  557. },
  558. })
  559. }