• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package config
16
17import (
18	"android/soong/android"
19	"strings"
20)
21
22var (
23	linuxBionicCflags = []string{
24		"-Wa,--noexecstack",
25
26		"-fPIC",
27
28		"-U_FORTIFY_SOURCE",
29		"-D_FORTIFY_SOURCE=2",
30		"-fstack-protector-strong",
31
32		// From x86_64_device
33		"-ffunction-sections",
34		"-fno-short-enums",
35		"-funwind-tables",
36
37		// Tell clang where the gcc toolchain is
38		"--gcc-toolchain=${LinuxBionicGccRoot}",
39
40		// This is normally in ClangExtraTargetCflags, but this is considered host
41		"-nostdlibinc",
42	}
43
44	linuxBionicLdflags = []string{
45		"-Wl,-z,noexecstack",
46		"-Wl,-z,relro",
47		"-Wl,-z,now",
48		"-Wl,--build-id=md5",
49		"-Wl,--fatal-warnings",
50		"-Wl,--no-undefined-version",
51
52		// Use the device gcc toolchain
53		"--gcc-toolchain=${LinuxBionicGccRoot}",
54	}
55
56	linuxBionicLldflags = append(linuxBionicLdflags,
57		"-Wl,--compress-debug-sections=zstd",
58	)
59
60	// Embed the linker into host bionic binaries. This is needed to support host bionic,
61	// as the linux kernel requires that the ELF interpreter referenced by PT_INTERP be
62	// either an absolute path, or relative from CWD. To work around this, we extract
63	// the load sections from the runtime linker ELF binary and embed them into each host
64	// bionic binary, omitting the PT_INTERP declaration. The kernel will treat it as a static
65	// binary, and then we use a special entry point to fix up the arguments passed by
66	// the kernel before jumping to the embedded linker.
67	linuxBionicCrtBeginSharedBinary = append(android.CopyOf(bionicCrtBeginSharedBinary),
68		"host_bionic_linker_script")
69)
70
71const (
72	x86_64GccVersion = "4.9"
73)
74
75func init() {
76	pctx.StaticVariable("LinuxBionicCflags", strings.Join(linuxBionicCflags, " "))
77	pctx.StaticVariable("LinuxBionicLdflags", strings.Join(linuxBionicLdflags, " "))
78	pctx.StaticVariable("LinuxBionicLldflags", strings.Join(linuxBionicLldflags, " "))
79
80	// Use the device gcc toolchain for now
81	pctx.StaticVariable("LinuxBionicGccVersion", x86_64GccVersion)
82	pctx.SourcePathVariable("LinuxBionicGccRoot",
83		"prebuilts/gcc/${HostPrebuiltTag}/x86/x86_64-linux-android-${LinuxBionicGccVersion}")
84}
85
86type toolchainLinuxBionic struct {
87	toolchain64Bit
88	toolchainBionic
89}
90
91func (t *toolchainLinuxBionic) Name() string {
92	return "x86_64"
93}
94
95func (t *toolchainLinuxBionic) IncludeFlags() string {
96	return ""
97}
98
99func (t *toolchainLinuxBionic) ClangTriple() string {
100	// TODO: we don't have a triple yet b/31393676
101	return "x86_64-linux-android"
102}
103
104func (t *toolchainLinuxBionic) Cflags() string {
105	return "${config.LinuxBionicCflags}"
106}
107
108func (t *toolchainLinuxBionic) Cppflags() string {
109	return ""
110}
111
112func (t *toolchainLinuxBionic) Ldflags() string {
113	return "${config.LinuxBionicLdflags}"
114}
115
116func (t *toolchainLinuxBionic) Lldflags() string {
117	return "${config.LinuxBionicLldflags}"
118}
119
120func (t *toolchainLinuxBionic) ToolchainCflags() string {
121	return "-m64 -march=x86-64" +
122		// TODO: We're not really android, but we don't have a triple yet b/31393676
123		" -U__ANDROID__"
124}
125
126func (t *toolchainLinuxBionic) ToolchainLdflags() string {
127	return "-m64"
128}
129
130func (t *toolchainLinuxBionic) AvailableLibraries() []string {
131	return nil
132}
133
134func (toolchainLinuxBionic) LibclangRuntimeLibraryArch() string {
135	return "x86_64"
136}
137
138func (toolchainLinuxBionic) CrtBeginSharedBinary() []string {
139	return linuxBionicCrtBeginSharedBinary
140}
141
142var toolchainLinuxBionicSingleton Toolchain = &toolchainLinuxBionic{}
143
144func linuxBionicToolchainFactory(arch android.Arch) Toolchain {
145	return toolchainLinuxBionicSingleton
146}
147
148func init() {
149	registerToolchainFactory(android.LinuxBionic, android.X86_64, linuxBionicToolchainFactory)
150}
151