blob: 31729a5f8e01687083ca030e5cdc92ff94cd26d5 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -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 "fmt"
19
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "android/soong/android"
21)
22
23//
24// Objects (for crt*.o)
25//
26
27func init() {
Colin Crosse40b4ea2018-10-02 22:25:58 -070028 android.RegisterModuleType("cc_object", ObjectFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070029}
30
31type objectLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070032 *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -070033 Properties ObjectLinkerProperties
34}
35
Pete Bentley74c9bba2019-08-16 20:25:06 +010036type ObjectLinkerProperties struct {
37 // list of modules that should only provide headers for this module.
38 Header_libs []string `android:"arch_variant,variant_prepend"`
39
40 // names of other cc_object modules to link into this module using partial linking
41 Objs []string `android:"arch_variant"`
42
43 // if set, add an extra objcopy --prefix-symbols= step
44 Prefix_symbols *string
45
46 // if set, the path to a linker script to pass to ld -r when combining multiple object files.
47 Linker_script *string `android:"path,arch_variant"`
48}
49
Patrice Arrudabaff0ce2019-03-26 10:39:49 -070050// cc_object runs the compiler without running the linker. It is rarely
51// necessary, but sometimes used to generate .s files from .c files to use as
52// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -070053func ObjectFactory() android.Module {
Dan Willemsen967c6a92016-11-17 00:30:56 -080054 module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth)
Peter Collingbourne1c648b82019-09-26 12:24:45 -070055 module.sanitize = &sanitize{}
Colin Crossb916a382016-07-29 17:28:03 -070056 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -070057 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -070058 }
59 module.compiler = NewBaseCompiler()
Peter Collingbourne486e42c2018-10-25 10:53:44 -070060
61 // Clang's address-significance tables are incompatible with ld -r.
62 module.compiler.appendCflags([]string{"-fno-addrsig"})
63
Colin Cross907769f2018-09-27 11:02:39 -070064 module.stl = &stl{}
Colin Cross4d9c2d12016-07-29 12:48:20 -070065 return module.Init()
66}
67
68func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070069 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -070070}
71
Colin Cross42742b82016-08-01 13:20:05 -070072func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -070073 return []interface{}{&object.Properties}
74}
75
Colin Cross42742b82016-08-01 13:20:05 -070076func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -070077
Colin Cross37047f12016-12-13 17:06:13 -080078func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross87dd9632017-11-03 13:31:05 -070079 if ctx.useVndk() && ctx.toolchain().Bionic() {
Dan Willemsen0c162932017-09-18 16:33:37 -070080 // Needed for VNDK builds where bionic headers aren't automatically added.
81 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc")
82 }
83
Paul Duffina37832a2019-07-18 12:31:26 +010084 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -070085 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
86 return deps
87}
88
Pete Bentley74c9bba2019-08-16 20:25:06 +010089func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen8536d6b2018-10-07 20:54:34 -070090 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -070091
Pete Bentley74c9bba2019-08-16 20:25:06 +010092 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
93 flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+lds.String())
94 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
95 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070096 return flags
97}
98
99func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700100 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700102 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700103
104 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700105 builderFlags := flagsToBuilderFlags(flags)
106
Pete Bentleyab65ba92019-10-18 12:39:56 +0100107 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700108 outputFile = objs.objFiles[0]
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700109
Nan Zhang0007d812017-11-07 10:57:05 -0800110 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700111 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Nan Zhang0007d812017-11-07 10:57:05 -0800112 TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700113 builderFlags, output)
114 outputFile = output
115 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 } else {
117 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700119
Nan Zhang0007d812017-11-07 10:57:05 -0800120 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700121 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Nan Zhang0007d812017-11-07 10:57:05 -0800122 TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700123 builderFlags, output)
124 output = input
125 }
126
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700127 TransformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 }
129
130 ctx.CheckbuildFile(outputFile)
131 return outputFile
132}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900133
134func (object *objectLinker) unstrippedOutputFilePath() android.Path {
135 return nil
136}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700137
138func (object *objectLinker) nativeCoverage() bool {
139 return true
140}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900141
142func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
143 return android.OptionalPath{}
144}