blob: 8989b29acace3c0e484471a960f8fd9caf6a8142 [file] [log] [blame]
Dan Willemsen394e9dc2016-09-14 15:04:48 -07001// 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
15package cc
16
17import (
18 "runtime"
19
20 "github.com/google/blueprint"
21
22 "android/soong/android"
23)
24
25func init() {
26 pctx.SourcePathVariable("relocationPackerCmd", "prebuilts/misc/${config.HostPrebuiltTag}/relocation_packer/relocation_packer")
27}
28
29var relocationPackerRule = pctx.AndroidStaticRule("packRelocations",
30 blueprint.RuleParams{
31 Command: "rm -f $out && cp $in $out && $relocationPackerCmd $out",
32 CommandDeps: []string{"$relocationPackerCmd"},
Dan Willemsen394e9dc2016-09-14 15:04:48 -070033 })
34
35type RelocationPackerProperties struct {
Chih-Hung Hsieh86814712018-05-23 18:30:46 -070036 // Generate compact dynamic relocation table, default true.
Dan Willemsen394e9dc2016-09-14 15:04:48 -070037 Pack_relocations *bool `android:"arch_variant"`
38
39 // This will be true even if we're embedded in Make, in which case
40 // we'll defer to make to actually do the packing.
41 PackingRelocations bool `blueprint:"mutated"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070042
43 // Use clang lld instead of gnu ld.
44 Use_clang_lld *bool
Dan Willemsen394e9dc2016-09-14 15:04:48 -070045}
46
47type relocationPacker struct {
48 Properties RelocationPackerProperties
49}
50
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070051func (p *relocationPacker) useClangLld(ctx BaseModuleContext) bool {
52 if p.Properties.Use_clang_lld != nil {
53 return Bool(p.Properties.Use_clang_lld)
54 }
55 return ctx.Config().UseClangLld()
56}
57
Dan Willemsen394e9dc2016-09-14 15:04:48 -070058func (p *relocationPacker) packingInit(ctx BaseModuleContext) {
Rahul Chaudhryccf4f832018-03-23 19:02:56 +000059 enabled := true
Dan Willemsen394e9dc2016-09-14 15:04:48 -070060 // Relocation packer isn't available on Darwin yet
61 if runtime.GOOS == "darwin" {
62 enabled = false
63 }
64 if ctx.Target().Os != android.Android {
65 enabled = false
66 }
Colin Cross6510f912017-11-29 00:27:14 -080067 if ctx.Config().Getenv("DISABLE_RELOCATION_PACKER") == "true" {
Dan Willemsen394e9dc2016-09-14 15:04:48 -070068 enabled = false
69 }
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070070 // Relocation packer does not work with lld output files.
71 // Packed files won't load.
72 if p.useClangLld(ctx) {
73 enabled = false
74 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070075 if ctx.useSdk() {
Dan Willemsen394e9dc2016-09-14 15:04:48 -070076 enabled = false
77 }
78 if p.Properties.Pack_relocations != nil &&
79 *p.Properties.Pack_relocations == false {
80 enabled = false
81 }
82
83 p.Properties.PackingRelocations = enabled
84}
85
86func (p *relocationPacker) needsPacking(ctx ModuleContext) bool {
Colin Cross6510f912017-11-29 00:27:14 -080087 if ctx.Config().EmbeddedInMake() {
Dan Willemsen394e9dc2016-09-14 15:04:48 -070088 return false
89 }
90 return p.Properties.PackingRelocations
91}
92
93func (p *relocationPacker) pack(ctx ModuleContext, in, out android.ModuleOutPath, flags builderFlags) {
Colin Crossae887032017-10-23 17:16:14 -070094 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070095 Rule: relocationPackerRule,
96 Description: "pack relocations",
97 Output: out,
98 Input: in,
Dan Willemsen394e9dc2016-09-14 15:04:48 -070099 })
100}