linker.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. "strconv"
  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. type BaseLinkerProperties struct {
  26. // list of modules whose object files should be linked into this module
  27. // in their entirety. For static library modules, all of the .o files from the intermediate
  28. // directory of the dependency will be linked into this modules .a file. For a shared library,
  29. // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
  30. Whole_static_libs []string `android:"arch_variant,variant_prepend"`
  31. // list of modules that should be statically linked into this module.
  32. Static_libs []string `android:"arch_variant,variant_prepend"`
  33. // list of modules that should be dynamically linked into this module.
  34. Shared_libs []string `android:"arch_variant"`
  35. // list of modules that should only provide headers for this module.
  36. Header_libs []string `android:"arch_variant,variant_prepend"`
  37. // list of module-specific flags that will be used for all link steps
  38. Ldflags []string `android:"arch_variant"`
  39. // list of system libraries that will be dynamically linked to
  40. // shared library and executable modules. If unset, generally defaults to libc,
  41. // libm, and libdl. Set to [] to prevent linking against the defaults.
  42. System_shared_libs []string `android:"arch_variant"`
  43. // allow the module to contain undefined symbols. By default,
  44. // modules cannot contain undefined symbols that are not satisified by their immediate
  45. // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
  46. // This flag should only be necessary for compiling low-level libraries like libc.
  47. Allow_undefined_symbols *bool `android:"arch_variant"`
  48. // don't link in libclang_rt.builtins-*.a
  49. No_libcrt *bool `android:"arch_variant"`
  50. // Use clang lld instead of gnu ld.
  51. Use_clang_lld *bool `android:"arch_variant"`
  52. // -l arguments to pass to linker for host-provided shared libraries
  53. Host_ldlibs []string `android:"arch_variant"`
  54. // list of shared libraries to re-export include directories from. Entries must be
  55. // present in shared_libs.
  56. Export_shared_lib_headers []string `android:"arch_variant"`
  57. // list of static libraries to re-export include directories from. Entries must be
  58. // present in static_libs.
  59. Export_static_lib_headers []string `android:"arch_variant"`
  60. // list of header libraries to re-export include directories from. Entries must be
  61. // present in header_libs.
  62. Export_header_lib_headers []string `android:"arch_variant"`
  63. // list of generated headers to re-export include directories from. Entries must be
  64. // present in generated_headers.
  65. Export_generated_headers []string `android:"arch_variant"`
  66. // don't link in crt_begin and crt_end. This flag should only be necessary for
  67. // compiling crt or libc.
  68. Nocrt *bool `android:"arch_variant"`
  69. // group static libraries. This can resolve missing symbols issues with interdependencies
  70. // between static libraries, but it is generally better to order them correctly instead.
  71. Group_static_libs *bool `android:"arch_variant"`
  72. // list of modules that should be installed with this module. This is similar to 'required'
  73. // but '.vendor' suffix will be appended to the module names if the shared libraries have
  74. // vendor variants and this module uses VNDK.
  75. Runtime_libs []string `android:"arch_variant"`
  76. Target struct {
  77. Vendor struct {
  78. // list of shared libs that only should be used to build the vendor
  79. // variant of the C/C++ module.
  80. Shared_libs []string
  81. // list of static libs that only should be used to build the vendor
  82. // variant of the C/C++ module.
  83. Static_libs []string
  84. // list of shared libs that should not be used to build the vendor variant
  85. // of the C/C++ module.
  86. Exclude_shared_libs []string
  87. // list of static libs that should not be used to build the vendor variant
  88. // of the C/C++ module.
  89. Exclude_static_libs []string
  90. // list of header libs that should not be used to build the vendor variant
  91. // of the C/C++ module.
  92. Exclude_header_libs []string
  93. // list of runtime libs that should not be installed along with the vendor
  94. // variant of the C/C++ module.
  95. Exclude_runtime_libs []string
  96. // version script for this vendor variant
  97. Version_script *string `android:"arch_variant"`
  98. }
  99. Recovery struct {
  100. // list of shared libs that only should be used to build the recovery
  101. // variant of the C/C++ module.
  102. Shared_libs []string
  103. // list of static libs that only should be used to build the recovery
  104. // variant of the C/C++ module.
  105. Static_libs []string
  106. // list of shared libs that should not be used to build
  107. // the recovery variant of the C/C++ module.
  108. Exclude_shared_libs []string
  109. // list of static libs that should not be used to build
  110. // the recovery variant of the C/C++ module.
  111. Exclude_static_libs []string
  112. // list of header libs that should not be used to build the recovery variant
  113. // of the C/C++ module.
  114. Exclude_header_libs []string
  115. }
  116. Ramdisk struct {
  117. // list of static libs that only should be used to build the recovery
  118. // variant of the C/C++ module.
  119. Static_libs []string
  120. // list of shared libs that should not be used to build
  121. // the ramdisk variant of the C/C++ module.
  122. Exclude_shared_libs []string
  123. // list of static libs that should not be used to build
  124. // the ramdisk variant of the C/C++ module.
  125. Exclude_static_libs []string
  126. }
  127. }
  128. // make android::build:GetBuildNumber() available containing the build ID.
  129. Use_version_lib *bool `android:"arch_variant"`
  130. // Generate compact dynamic relocation table, default true.
  131. Pack_relocations *bool `android:"arch_variant"`
  132. // local file name to pass to the linker as --version_script
  133. Version_script *string `android:"path,arch_variant"`
  134. // list of static libs that should not be used to build this module
  135. Exclude_static_libs []string
  136. }
  137. func NewBaseLinker(sanitize *sanitize) *baseLinker {
  138. return &baseLinker{sanitize: sanitize}
  139. }
  140. // baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
  141. type baseLinker struct {
  142. Properties BaseLinkerProperties
  143. dynamicProperties struct {
  144. RunPaths []string `blueprint:"mutated"`
  145. BuildStubs bool `blueprint:"mutated"`
  146. }
  147. sanitize *sanitize
  148. }
  149. func (linker *baseLinker) appendLdflags(flags []string) {
  150. linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
  151. }
  152. func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
  153. if ctx.toolchain().Is64Bit() {
  154. linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
  155. } else {
  156. linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
  157. }
  158. }
  159. func (linker *baseLinker) linkerProps() []interface{} {
  160. return []interface{}{&linker.Properties, &linker.dynamicProperties}
  161. }
  162. func (linker *baseLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
  163. deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
  164. deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
  165. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
  166. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
  167. deps.RuntimeLibs = append(deps.RuntimeLibs, linker.Properties.Runtime_libs...)
  168. deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
  169. deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
  170. deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
  171. deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
  172. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Exclude_static_libs)
  173. if Bool(linker.Properties.Use_version_lib) {
  174. deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion")
  175. }
  176. if ctx.useVndk() {
  177. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Vendor.Shared_libs...)
  178. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
  179. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
  180. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Vendor.Static_libs...)
  181. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
  182. deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Vendor.Exclude_header_libs)
  183. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
  184. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
  185. deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor.Exclude_runtime_libs)
  186. }
  187. if ctx.inRecovery() {
  188. deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Recovery.Shared_libs...)
  189. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Recovery.Exclude_shared_libs)
  190. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Recovery.Exclude_shared_libs)
  191. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Recovery.Static_libs...)
  192. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
  193. deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Recovery.Exclude_header_libs)
  194. deps.ReexportHeaderLibHeaders = removeListFromList(deps.ReexportHeaderLibHeaders, linker.Properties.Target.Recovery.Exclude_header_libs)
  195. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Recovery.Exclude_static_libs)
  196. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
  197. }
  198. if ctx.inRamdisk() {
  199. deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Recovery.Exclude_shared_libs)
  200. deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Recovery.Exclude_shared_libs)
  201. deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Recovery.Static_libs...)
  202. deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
  203. deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Recovery.Exclude_static_libs)
  204. deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
  205. }
  206. if ctx.toolchain().Bionic() {
  207. // libclang_rt.builtins and libatomic have to be last on the command line
  208. if !Bool(linker.Properties.No_libcrt) {
  209. deps.LateStaticLibs = append(deps.LateStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
  210. deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
  211. }
  212. systemSharedLibs := linker.Properties.System_shared_libs
  213. if systemSharedLibs == nil {
  214. // Provide a default system_shared_libs if it is unspecified. Note: If an
  215. // empty list [] is specified, it implies that the module declines the
  216. // default system_shared_libs.
  217. systemSharedLibs = []string{"libc", "libm", "libdl"}
  218. }
  219. if inList("libdl", deps.SharedLibs) {
  220. // If system_shared_libs has libc but not libdl, make sure shared_libs does not
  221. // have libdl to avoid loading libdl before libc.
  222. if inList("libc", systemSharedLibs) {
  223. if !inList("libdl", systemSharedLibs) {
  224. ctx.PropertyErrorf("shared_libs",
  225. "libdl must be in system_shared_libs, not shared_libs")
  226. }
  227. _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
  228. }
  229. }
  230. if inList("libc_scudo", deps.SharedLibs) {
  231. // libc_scudo is an alternate implementation of all
  232. // allocation functions (malloc, free), that uses
  233. // the scudo allocator instead of the default native
  234. // allocator. If this library is in the list, make
  235. // sure it's first so it properly overrides the
  236. // allocation functions of all other shared libraries.
  237. _, deps.SharedLibs = removeFromList("libc_scudo", deps.SharedLibs)
  238. deps.SharedLibs = append([]string{"libc_scudo"}, deps.SharedLibs...)
  239. }
  240. // If libc and libdl are both in system_shared_libs make sure libdl comes after libc
  241. // to avoid loading libdl before libc.
  242. if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
  243. indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
  244. ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
  245. }
  246. deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
  247. }
  248. if ctx.Fuchsia() {
  249. if ctx.ModuleName() != "libbioniccompat" &&
  250. ctx.ModuleName() != "libcompiler_rt-extras" &&
  251. ctx.ModuleName() != "libcompiler_rt" {
  252. deps.StaticLibs = append(deps.StaticLibs, "libbioniccompat")
  253. }
  254. if ctx.ModuleName() != "libcompiler_rt" && ctx.ModuleName() != "libcompiler_rt-extras" {
  255. deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt")
  256. }
  257. }
  258. if ctx.Windows() {
  259. deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
  260. }
  261. return deps
  262. }
  263. func (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
  264. // Clang lld is not ready for for Darwin host executables yet.
  265. // See https://lld.llvm.org/AtomLLD.html for status of lld for Mach-O.
  266. if ctx.Darwin() {
  267. return false
  268. }
  269. if linker.Properties.Use_clang_lld != nil {
  270. return Bool(linker.Properties.Use_clang_lld)
  271. }
  272. return true
  273. }
  274. // Check whether the SDK version is not older than the specific one
  275. func CheckSdkVersionAtLeast(ctx ModuleContext, SdkVersion int) bool {
  276. if ctx.sdkVersion() == "current" {
  277. return true
  278. }
  279. parsedSdkVersion, err := strconv.Atoi(ctx.sdkVersion())
  280. if err != nil {
  281. ctx.PropertyErrorf("sdk_version",
  282. "Invalid sdk_version value (must be int or current): %q",
  283. ctx.sdkVersion())
  284. }
  285. if parsedSdkVersion < SdkVersion {
  286. return false
  287. }
  288. return true
  289. }
  290. // ModuleContext extends BaseModuleContext
  291. // BaseModuleContext should know if LLD is used?
  292. func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
  293. toolchain := ctx.toolchain()
  294. hod := "Host"
  295. if ctx.Os().Class == android.Device {
  296. hod = "Device"
  297. }
  298. if linker.useClangLld(ctx) {
  299. flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod))
  300. if !BoolDefault(linker.Properties.Pack_relocations, true) {
  301. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=none")
  302. } else if ctx.Device() {
  303. // The SHT_RELR relocations is only supported by API level >= 28.
  304. // Do not turn this on if older version NDK is used.
  305. if !ctx.useSdk() || CheckSdkVersionAtLeast(ctx, 28) {
  306. flags.Global.LdFlags = append(flags.Global.LdFlags,
  307. "-Wl,--pack-dyn-relocs=android+relr",
  308. "-Wl,--use-android-relr-tags")
  309. } else if CheckSdkVersionAtLeast(ctx, 23) {
  310. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android")
  311. }
  312. }
  313. } else {
  314. flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
  315. }
  316. if Bool(linker.Properties.Allow_undefined_symbols) {
  317. if ctx.Darwin() {
  318. // darwin defaults to treating undefined symbols as errors
  319. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-undefined,dynamic_lookup")
  320. }
  321. } else if !ctx.Darwin() && !ctx.Windows() {
  322. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--no-undefined")
  323. }
  324. if linker.useClangLld(ctx) {
  325. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.ClangLldflags())
  326. } else {
  327. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.ClangLdflags())
  328. }
  329. if !ctx.toolchain().Bionic() && !ctx.Fuchsia() {
  330. CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
  331. flags.Local.LdFlags = append(flags.Local.LdFlags, linker.Properties.Host_ldlibs...)
  332. if !ctx.Windows() {
  333. // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
  334. // builds
  335. flags.Global.LdFlags = append(flags.Global.LdFlags,
  336. "-ldl",
  337. "-lpthread",
  338. "-lm",
  339. )
  340. if !ctx.Darwin() {
  341. flags.Global.LdFlags = append(flags.Global.LdFlags, "-lrt")
  342. }
  343. }
  344. }
  345. if ctx.Fuchsia() {
  346. flags.Global.LdFlags = append(flags.Global.LdFlags, "-lfdio", "-lzircon")
  347. }
  348. if ctx.toolchain().LibclangRuntimeLibraryArch() != "" {
  349. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--exclude-libs="+config.BuiltinsRuntimeLibrary(ctx.toolchain())+".a")
  350. }
  351. CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
  352. flags.Local.LdFlags = append(flags.Local.LdFlags, proptools.NinjaAndShellEscapeList(linker.Properties.Ldflags)...)
  353. if ctx.Host() && !ctx.Windows() {
  354. rpath_prefix := `\$$ORIGIN/`
  355. if ctx.Darwin() {
  356. rpath_prefix = "@loader_path/"
  357. }
  358. if !ctx.static() {
  359. for _, rpath := range linker.dynamicProperties.RunPaths {
  360. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
  361. }
  362. }
  363. }
  364. if ctx.useSdk() {
  365. // The bionic linker now has support gnu style hashes (which are much faster!), but shipping
  366. // to older devices requires the old style hash. Fortunately, we can build with both and
  367. // it'll work anywhere.
  368. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--hash-style=both")
  369. }
  370. flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.ToolchainClangLdflags())
  371. if Bool(linker.Properties.Group_static_libs) {
  372. flags.GroupStaticLibs = true
  373. }
  374. // Version_script is not needed when linking stubs lib where the version
  375. // script is created from the symbol map file.
  376. if !linker.dynamicProperties.BuildStubs {
  377. versionScript := ctx.ExpandOptionalSource(
  378. linker.Properties.Version_script, "version_script")
  379. if ctx.useVndk() && linker.Properties.Target.Vendor.Version_script != nil {
  380. versionScript = ctx.ExpandOptionalSource(
  381. linker.Properties.Target.Vendor.Version_script,
  382. "target.vendor.version_script")
  383. }
  384. if versionScript.Valid() {
  385. if ctx.Darwin() {
  386. ctx.PropertyErrorf("version_script", "Not supported on Darwin")
  387. } else {
  388. flags.Local.LdFlags = append(flags.Local.LdFlags,
  389. "-Wl,--version-script,"+versionScript.String())
  390. flags.LdFlagsDeps = append(flags.LdFlagsDeps, versionScript.Path())
  391. if linker.sanitize.isSanitizerEnabled(cfi) {
  392. cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath)
  393. flags.Local.LdFlags = append(flags.Local.LdFlags,
  394. "-Wl,--version-script,"+cfiExportsMap.String())
  395. flags.LdFlagsDeps = append(flags.LdFlagsDeps, cfiExportsMap)
  396. }
  397. }
  398. }
  399. }
  400. return flags
  401. }
  402. func (linker *baseLinker) link(ctx ModuleContext,
  403. flags Flags, deps PathDeps, objs Objects) android.Path {
  404. panic(fmt.Errorf("baseLinker doesn't know how to link"))
  405. }
  406. // Injecting version symbols
  407. // Some host modules want a version number, but we don't want to rebuild it every time. Optionally add a step
  408. // after linking that injects a constant placeholder with the current version number.
  409. func init() {
  410. pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject")
  411. }
  412. var injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol",
  413. blueprint.RuleParams{
  414. Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " +
  415. "-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $$(cat $buildNumberFile)",
  416. CommandDeps: []string{"$symbolInjectCmd"},
  417. },
  418. "buildNumberFile")
  419. func (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) {
  420. buildNumberFile := ctx.Config().BuildNumberFile(ctx)
  421. ctx.Build(pctx, android.BuildParams{
  422. Rule: injectVersionSymbol,
  423. Description: "inject version symbol",
  424. Input: in,
  425. Output: out,
  426. OrderOnly: android.Paths{buildNumberFile},
  427. Args: map[string]string{
  428. "buildNumberFile": buildNumberFile.String(),
  429. },
  430. })
  431. }
  432. // Rule to generate .bss symbol ordering file.
  433. var (
  434. _ = pctx.SourcePathVariable("genSortedBssSymbolsPath", "build/soong/scripts/gen_sorted_bss_symbols.sh")
  435. gen_sorted_bss_symbols = pctx.AndroidStaticRule("gen_sorted_bss_symbols",
  436. blueprint.RuleParams{
  437. Command: "CROSS_COMPILE=$crossCompile $genSortedBssSymbolsPath ${in} ${out}",
  438. CommandDeps: []string{"$genSortedBssSymbolsPath", "${crossCompile}nm"},
  439. },
  440. "crossCompile")
  441. )
  442. func (linker *baseLinker) sortBssSymbolsBySize(ctx ModuleContext, in android.Path, symbolOrderingFile android.ModuleOutPath, flags builderFlags) string {
  443. crossCompile := gccCmd(flags.toolchain, "")
  444. ctx.Build(pctx, android.BuildParams{
  445. Rule: gen_sorted_bss_symbols,
  446. Description: "generate bss symbol order " + symbolOrderingFile.Base(),
  447. Output: symbolOrderingFile,
  448. Input: in,
  449. Args: map[string]string{
  450. "crossCompile": crossCompile,
  451. },
  452. })
  453. return "-Wl,--symbol-ordering-file," + symbolOrderingFile.String()
  454. }