linker.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. "android/soong/android"
  17. "android/soong/cc/config"
  18. "fmt"
  19. "path/filepath"
  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 invertBoolPtr(value *bool) *bool {
  189. if value == nil {
  190. return nil
  191. }
  192. ret := !(*value)
  193. return &ret
  194. }
  195. func (blp *BaseLinkerProperties) crt() *bool {
  196. val := invertBoolPtr(blp.Nocrt)
  197. if val != nil && *val {
  198. // == True
  199. //
  200. // Since crt is enabled for almost every module compiling against the Bionic runtime,
  201. // use `nil` when it's enabled, and rely on the Starlark macro to set it to True by default.
  202. // This keeps the BUILD files clean.
  203. return nil
  204. }
  205. return val // can be False or nil
  206. }
  207. func (blp *BaseLinkerProperties) libCrt() *bool {
  208. return invertBoolPtr(blp.No_libcrt)
  209. }
  210. func NewBaseLinker(sanitize *sanitize) *baseLinker {
  211. return &baseLinker{sanitize: sanitize}
  212. }
  213. // baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
  214. type baseLinker struct {
  215. Properties BaseLinkerProperties
  216. dynamicProperties struct {
  217. BuildStubs bool `blueprint:"mutated"`
  218. }
  219. sanitize *sanitize
  220. }
  221. func (linker *baseLinker) appendLdflags(flags []string) {
  222. linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
  223. }
  224. // linkerInit initializes dynamic properties of the linker.
  225. func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
  226. }
  227. func (linker *baseLinker) linkerProps() []interface{} {
  228. return []interface{}{&linker.Properties, &linker.dynamicProperties}
  229. }
  230. func (linker *baseLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
  231. deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
  232. deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
  233. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
  234. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
  235. deps.RuntimeLibs = append(deps.RuntimeLibs, linker.Properties.Runtime_libs...)
  236. deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
  237. deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
  238. deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
  239. deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
  240. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Exclude_shared_libs)
  241. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Exclude_static_libs)
  242. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Exclude_static_libs)
  243. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Exclude_runtime_libs)
  244. // Record the libraries that need to be excluded when building for APEX. Unlike other
  245. // target.*.exclude_* properties, SharedLibs and StaticLibs are not modified here because
  246. // this module hasn't yet passed the apexMutator. Therefore, we can't tell whether this is
  247. // an apex variant of not. Record the exclude list in the deps struct for now. The info is
  248. // used to mark the dependency tag when adding dependencies to the deps. Then inside
  249. // GenerateAndroidBuildActions, the marked dependencies are ignored (i.e. not used) for APEX
  250. // variants.
  251. deps.ExcludeLibsForApex = append(deps.ExcludeLibsForApex, linker.Properties.Target.Apex.Exclude_shared_libs...)
  252. deps.ExcludeLibsForApex = append(deps.ExcludeLibsForApex, linker.Properties.Target.Apex.Exclude_static_libs...)
  253. if Bool(linker.Properties.Use_version_lib) {
  254. deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion")
  255. }
  256. if ctx.inVendor() {
  257. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Vendor.Shared_libs...)
  258. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
  259. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
  260. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Vendor.Static_libs...)
  261. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
  262. deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Vendor.Header_libs...)
  263. deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Vendor.Exclude_header_libs)
  264. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
  265. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
  266. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor.Exclude_runtime_libs)
  267. }
  268. if ctx.inProduct() {
  269. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Product.Shared_libs...)
  270. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Product.Exclude_shared_libs)
  271. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Product.Exclude_shared_libs)
  272. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Product.Static_libs...)
  273. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Product.Exclude_static_libs)
  274. deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Product.Exclude_header_libs)
  275. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Product.Exclude_static_libs)
  276. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Product.Exclude_static_libs)
  277. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Product.Exclude_runtime_libs)
  278. }
  279. if ctx.inRecovery() {
  280. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Recovery.Shared_libs...)
  281. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Recovery.Exclude_shared_libs)
  282. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Recovery.Exclude_shared_libs)
  283. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Recovery.Static_libs...)
  284. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
  285. deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Recovery.Exclude_header_libs)
  286. deps.ReexportHeaderLibHeaders = removeListFromList(deps.ReexportHeaderLibHeaders, linker.Properties.Target.Recovery.Exclude_header_libs)
  287. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Recovery.Exclude_static_libs)
  288. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
  289. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Recovery.Exclude_runtime_libs)
  290. }
  291. if ctx.inRamdisk() {
  292. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Ramdisk.Exclude_shared_libs)
  293. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Ramdisk.Exclude_shared_libs)
  294. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Ramdisk.Static_libs...)
  295. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Ramdisk.Exclude_static_libs)
  296. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Ramdisk.Exclude_static_libs)
  297. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Ramdisk.Exclude_static_libs)
  298. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Ramdisk.Exclude_runtime_libs)
  299. }
  300. if ctx.inVendorRamdisk() {
  301. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
  302. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
  303. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
  304. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
  305. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
  306. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_runtime_libs)
  307. }
  308. if !ctx.useSdk() {
  309. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Platform.Shared_libs...)
  310. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Platform.Exclude_shared_libs)
  311. deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Platform.Header_libs...)
  312. }
  313. deps.SystemSharedLibs = linker.Properties.System_shared_libs
  314. if deps.SystemSharedLibs == nil {
  315. // Provide a default system_shared_libs if it is unspecified. Note: If an
  316. // empty list [] is specified, it implies that the module declines the
  317. // default system_shared_libs.
  318. deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
  319. }
  320. if ctx.toolchain().Bionic() {
  321. // libclang_rt.builtins has to be last on the command line
  322. if !Bool(linker.Properties.No_libcrt) && !ctx.header() {
  323. deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
  324. }
  325. if inList("libdl", deps.SharedLibs) {
  326. // If system_shared_libs has libc but not libdl, make sure shared_libs does not
  327. // have libdl to avoid loading libdl before libc.
  328. if inList("libc", deps.SystemSharedLibs) {
  329. if !inList("libdl", deps.SystemSharedLibs) {
  330. ctx.PropertyErrorf("shared_libs",
  331. "libdl must be in system_shared_libs, not shared_libs")
  332. }
  333. _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
  334. }
  335. }
  336. // If libc and libdl are both in system_shared_libs make sure libdl comes after libc
  337. // to avoid loading libdl before libc.
  338. if inList("libdl", deps.SystemSharedLibs) && inList("libc", deps.SystemSharedLibs) &&
  339. indexList("libdl", deps.SystemSharedLibs) < indexList("libc", deps.SystemSharedLibs) {
  340. ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
  341. }
  342. } else if ctx.toolchain().Musl() {
  343. if !Bool(linker.Properties.No_libcrt) && !ctx.header() {
  344. deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
  345. }
  346. }
  347. deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
  348. if ctx.Windows() && ctx.ModuleName() != "libwinpthread" {
  349. deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
  350. }
  351. return deps
  352. }
  353. func (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
  354. if linker.Properties.Use_clang_lld != nil {
  355. return Bool(linker.Properties.Use_clang_lld)
  356. }
  357. return true
  358. }
  359. // Check whether the SDK version is not older than the specific one
  360. func CheckSdkVersionAtLeast(ctx ModuleContext, SdkVersion android.ApiLevel) bool {
  361. if ctx.minSdkVersion() == "current" {
  362. return true
  363. }
  364. parsedSdkVersion, err := nativeApiLevelFromUser(ctx, ctx.minSdkVersion())
  365. if err != nil {
  366. ctx.PropertyErrorf("min_sdk_version",
  367. "Invalid min_sdk_version value (must be int or current): %q",
  368. ctx.minSdkVersion())
  369. }
  370. if parsedSdkVersion.LessThan(SdkVersion) {
  371. return false
  372. }
  373. return true
  374. }
  375. // ModuleContext extends BaseModuleContext
  376. // BaseModuleContext should know if LLD is used?
  377. func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
  378. toolchain := ctx.toolchain()
  379. hod := "Host"
  380. if ctx.Os().Class == android.Device {
  381. hod = "Device"
  382. }
  383. if linker.useClangLld(ctx) {
  384. flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod))
  385. if !BoolDefault(linker.Properties.Pack_relocations, packRelocationsDefault) {
  386. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=none")
  387. } else if ctx.Device() {
  388. // SHT_RELR relocations are only supported at API level >= 30.
  389. // ANDROID_RELR relocations were supported at API level >= 28.
  390. // Relocation packer was supported at API level >= 23.
  391. // Do the best we can...
  392. if (!ctx.useSdk() && ctx.minSdkVersion() == "") || CheckSdkVersionAtLeast(ctx, android.FirstShtRelrVersion) {
  393. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android+relr")
  394. } else if CheckSdkVersionAtLeast(ctx, android.FirstAndroidRelrVersion) {
  395. flags.Global.LdFlags = append(flags.Global.LdFlags,
  396. "-Wl,--pack-dyn-relocs=android+relr",
  397. "-Wl,--use-android-relr-tags")
  398. } else if CheckSdkVersionAtLeast(ctx, android.FirstPackedRelocationsVersion) {
  399. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android")
  400. }
  401. }
  402. } else {
  403. flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
  404. }
  405. if Bool(linker.Properties.Allow_undefined_symbols) {
  406. if ctx.Darwin() {
  407. // darwin defaults to treating undefined symbols as errors
  408. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-undefined,dynamic_lookup")
  409. }
  410. } else if !ctx.Darwin() && !ctx.Windows() {
  411. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--no-undefined")
  412. }
  413. if linker.useClangLld(ctx) {
  414. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Lldflags())
  415. } else {
  416. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Ldflags())
  417. }
  418. if !ctx.toolchain().Bionic() && ctx.Os() != android.LinuxMusl {
  419. CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
  420. flags.Local.LdFlags = append(flags.Local.LdFlags, linker.Properties.Host_ldlibs...)
  421. if !ctx.Windows() {
  422. // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
  423. // builds
  424. flags.Global.LdFlags = append(flags.Global.LdFlags,
  425. "-ldl",
  426. "-lpthread",
  427. "-lm",
  428. )
  429. if !ctx.Darwin() {
  430. flags.Global.LdFlags = append(flags.Global.LdFlags, "-lrt")
  431. }
  432. }
  433. }
  434. CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
  435. flags.Local.LdFlags = append(flags.Local.LdFlags, proptools.NinjaAndShellEscapeList(linker.Properties.Ldflags)...)
  436. if ctx.Host() && !ctx.Windows() && !ctx.static() {
  437. flags.Global.LdFlags = append(flags.Global.LdFlags, RpathFlags(ctx)...)
  438. }
  439. if ctx.useSdk() {
  440. // The bionic linker now has support gnu style hashes (which are much faster!), but shipping
  441. // to older devices requires the old style hash. Fortunately, we can build with both and
  442. // it'll work anywhere.
  443. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--hash-style=both")
  444. }
  445. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.ToolchainLdflags())
  446. // Version_script is not needed when linking stubs lib where the version
  447. // script is created from the symbol map file.
  448. if !linker.dynamicProperties.BuildStubs {
  449. versionScript := ctx.ExpandOptionalSource(
  450. linker.Properties.Version_script, "version_script")
  451. if ctx.inVendor() && linker.Properties.Target.Vendor.Version_script != nil {
  452. versionScript = ctx.ExpandOptionalSource(
  453. linker.Properties.Target.Vendor.Version_script,
  454. "target.vendor.version_script")
  455. } else if ctx.inProduct() && linker.Properties.Target.Product.Version_script != nil {
  456. versionScript = ctx.ExpandOptionalSource(
  457. linker.Properties.Target.Product.Version_script,
  458. "target.product.version_script")
  459. }
  460. if versionScript.Valid() {
  461. if ctx.Darwin() {
  462. ctx.PropertyErrorf("version_script", "Not supported on Darwin")
  463. } else {
  464. flags.Local.LdFlags = append(flags.Local.LdFlags,
  465. "-Wl,--version-script,"+versionScript.String())
  466. flags.LdFlagsDeps = append(flags.LdFlagsDeps, versionScript.Path())
  467. if linker.sanitize.isSanitizerEnabled(cfi) {
  468. cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath)
  469. flags.Local.LdFlags = append(flags.Local.LdFlags,
  470. "-Wl,--version-script,"+cfiExportsMap.String())
  471. flags.LdFlagsDeps = append(flags.LdFlagsDeps, cfiExportsMap)
  472. }
  473. }
  474. }
  475. dynamicList := android.OptionalPathForModuleSrc(ctx, linker.Properties.Dynamic_list)
  476. if dynamicList.Valid() {
  477. if ctx.Darwin() {
  478. ctx.PropertyErrorf("dynamic_list", "Not supported on Darwin")
  479. } else {
  480. flags.Local.LdFlags = append(flags.Local.LdFlags,
  481. "-Wl,--dynamic-list,"+dynamicList.String())
  482. flags.LdFlagsDeps = append(flags.LdFlagsDeps, dynamicList.Path())
  483. }
  484. }
  485. linkerScriptPaths := android.PathsForModuleSrc(ctx, linker.Properties.Linker_scripts)
  486. if len(linkerScriptPaths) > 0 && (ctx.Darwin() || ctx.Windows()) {
  487. ctx.PropertyErrorf("linker_scripts", "Only supported for ELF files")
  488. } else {
  489. for _, linkerScriptPath := range linkerScriptPaths {
  490. flags.Local.LdFlags = append(flags.Local.LdFlags,
  491. "-Wl,--script,"+linkerScriptPath.String())
  492. flags.LdFlagsDeps = append(flags.LdFlagsDeps, linkerScriptPath)
  493. }
  494. }
  495. }
  496. return flags
  497. }
  498. // RpathFlags returns the rpath linker flags for current target to search the following directories relative
  499. // to the binary:
  500. //
  501. // - "." to find libraries alongside tests
  502. // - "lib[64]" to find libraries in a subdirectory of the binaries' directory
  503. // - "../lib[64]" to find libraries when the binaries are in a bin directory
  504. // - "../../lib[64]" to find libraries in out/host/linux-x86/lib64 when the test or binary is in
  505. // out/host/linux-x86/nativetest/<test dir>/<test>
  506. // - "../../../lib[[64] to find libraries in out/host/linux-x86/lib64 when the test or binary is in
  507. // out/host/linux-x86/testcases/<test dir>/<CPU>/<test>
  508. func RpathFlags(ctx android.ModuleContext) []string {
  509. key := struct {
  510. os android.OsType
  511. arch android.ArchType
  512. }{ctx.Target().Os, ctx.Target().Arch.ArchType}
  513. return ctx.Config().OnceStringSlice(android.NewCustomOnceKey(key), func() []string {
  514. rpathPrefix := `\$$ORIGIN/`
  515. if key.os == android.Darwin {
  516. rpathPrefix = "@loader_path/"
  517. }
  518. var libDir string
  519. if key.arch.Multilib == "lib64" {
  520. libDir = "lib64"
  521. } else {
  522. libDir = "lib"
  523. }
  524. return []string{
  525. "-Wl,-rpath," + rpathPrefix,
  526. "-Wl,-rpath," + rpathPrefix + libDir,
  527. "-Wl,-rpath," + rpathPrefix + filepath.Join("..", libDir),
  528. "-Wl,-rpath," + rpathPrefix + filepath.Join("../..", libDir),
  529. "-Wl,-rpath," + rpathPrefix + filepath.Join("../../..", libDir),
  530. }
  531. })
  532. }
  533. func (linker *baseLinker) link(ctx ModuleContext,
  534. flags Flags, deps PathDeps, objs Objects) android.Path {
  535. panic(fmt.Errorf("baseLinker doesn't know how to link"))
  536. }
  537. func (linker *baseLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
  538. specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, linker.Properties.Shared_libs...)
  539. // Must distinguish nil and [] in system_shared_libs - ensure that [] in
  540. // either input list doesn't come out as nil.
  541. if specifiedDeps.systemSharedLibs == nil {
  542. specifiedDeps.systemSharedLibs = linker.Properties.System_shared_libs
  543. } else {
  544. specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, linker.Properties.System_shared_libs...)
  545. }
  546. return specifiedDeps
  547. }
  548. // Injecting version symbols
  549. // Some host modules want a version number, but we don't want to rebuild it every time. Optionally add a step
  550. // after linking that injects a constant placeholder with the current version number.
  551. func init() {
  552. pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject")
  553. }
  554. var injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol",
  555. blueprint.RuleParams{
  556. Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " +
  557. "-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $$(cat $buildNumberFile)",
  558. CommandDeps: []string{"$symbolInjectCmd"},
  559. },
  560. "buildNumberFile")
  561. func (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) {
  562. buildNumberFile := ctx.Config().BuildNumberFile(ctx)
  563. ctx.Build(pctx, android.BuildParams{
  564. Rule: injectVersionSymbol,
  565. Description: "inject version symbol",
  566. Input: in,
  567. Output: out,
  568. OrderOnly: android.Paths{buildNumberFile},
  569. Args: map[string]string{
  570. "buildNumberFile": buildNumberFile.String(),
  571. },
  572. })
  573. }