Merge changes from topic "host_bionic"

* changes:
  Extract the linker and embed it into host bionic binaries
  Add prefix_symbols support to cc_object
  Fix ToolchainClangCflags for host bionic
  Don't add a rpath to the linker
This commit is contained in:
Treehugger Robot
2017-09-21 00:46:41 +00:00
committed by Gerrit Code Review
9 changed files with 335 additions and 20 deletions

View File

@@ -310,7 +310,7 @@ toolchain_library {
enabled: false,
target: {
windows: {
enabled: true
enabled: true,
},
},
}
@@ -329,3 +329,45 @@ kernel_headers {
name: "device_kernel_headers",
vendor: true,
}
cc_genrule {
name: "host_bionic_linker_asm",
host_supported: true,
device_supported: false,
target: {
linux_bionic: {
enabled: true,
},
linux: {
enabled: false,
},
darwin: {
enabled: false,
},
},
tools: ["extract_linker"],
cmd: "$(location) -s $(out) $(in)",
srcs: [":linker"],
out: ["linker.s"],
}
cc_genrule {
name: "host_bionic_linker_script",
host_supported: true,
device_supported: false,
target: {
linux_bionic: {
enabled: true,
},
linux: {
enabled: false,
},
darwin: {
enabled: false,
},
},
tools: ["extract_linker"],
cmd: "$(location) -T $(out) $(in)",
srcs: [":linker"],
out: ["linker.script"],
}

View File

@@ -15,8 +15,6 @@
package cc
import (
"path/filepath"
"github.com/google/blueprint/proptools"
"android/soong/android"
@@ -152,6 +150,10 @@ func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
[]string{"libc", "libc_nomalloc", "libcompiler_rt"})
deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
}
if ctx.Os() == android.LinuxBionic && !binary.static() {
deps.LinkerScript = "host_bionic_linker_script"
}
}
if !binary.static() && inList("libc", deps.StaticLibs) {
@@ -243,15 +245,7 @@ func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags
case android.Android:
flags.DynamicLinker = "/system/bin/linker"
case android.LinuxBionic:
// The linux kernel expects the linker to be an
// absolute path
path := android.PathForOutput(ctx,
"host", "linux_bionic-x86", "bin", "linker")
if p, err := filepath.Abs(path.String()); err == nil {
flags.DynamicLinker = p
} else {
ctx.ModuleErrorf("can't find path to dynamic linker: %q", err)
}
flags.DynamicLinker = ""
default:
ctx.ModuleErrorf("unknown dynamic linker")
}
@@ -304,6 +298,11 @@ func (binary *binaryDecorator) link(ctx ModuleContext,
}
}
if deps.LinkerScript.Valid() {
flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String())
linkerDeps = append(linkerDeps, deps.LinkerScript.Path())
}
if flags.DynamicLinker != "" {
flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
}

View File

@@ -74,6 +74,7 @@ type Deps struct {
ReexportGeneratedHeaders []string
CrtBegin, CrtEnd string
LinkerScript string
}
type PathDeps struct {
@@ -98,6 +99,7 @@ type PathDeps struct {
// Paths to crt*.o files
CrtBegin, CrtEnd android.OptionalPath
LinkerScript android.OptionalPath
}
type Flags struct {
@@ -140,6 +142,9 @@ type Flags struct {
type ObjectLinkerProperties struct {
// names of other cc_object modules to link into this module using partial linking
Objs []string `android:"arch_variant"`
// if set, add an extra objcopy --prefix-symbols= step
Prefix_symbols string
}
// Properties used to compile all C or C++ modules
@@ -271,6 +276,7 @@ var (
objDepTag = dependencyTag{name: "obj"}
crtBeginDepTag = dependencyTag{name: "crtbegin"}
crtEndDepTag = dependencyTag{name: "crtend"}
linkerScriptDepTag = dependencyTag{name: "linker script"}
reuseObjTag = dependencyTag{name: "reuse objects"}
ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
@@ -831,6 +837,9 @@ func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
if deps.CrtEnd != "" {
actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
}
if deps.LinkerScript != "" {
actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
}
version := ctx.sdkVersion()
actx.AddVariationDependencies([]blueprint.Variation{
@@ -986,6 +995,17 @@ func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
} else {
ctx.ModuleErrorf("module %q is not a genrule", name)
}
case linkerScriptDepTag:
if genRule, ok := m.(genrule.SourceFileGenerator); ok {
files := genRule.GeneratedSourceFiles()
if len(files) == 1 {
depPaths.LinkerScript = android.OptionalPathForPath(files[0])
} else if len(files) > 1 {
ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", name)
}
} else {
ctx.ModuleErrorf("module %q is not a genrule", name)
}
default:
ctx.ModuleErrorf("depends on non-cc module %q", name)
}

View File

@@ -54,10 +54,6 @@ var (
// Tell clang where the gcc toolchain is
"--gcc-toolchain=${LinuxBionicGccRoot}",
// TODO: We're not really android, but we don't have a triple yet b/31393676
"-U__ANDROID__",
"-fno-emulated-tls",
// This is normally in ClangExtraTargetCflags, but this is considered host
"-nostdlibinc",
})
@@ -142,7 +138,9 @@ func (t *toolchainLinuxBionic) ClangLdflags() string {
}
func (t *toolchainLinuxBionic) ToolchainClangCflags() string {
return "-m64 -march=x86-64"
return "-m64 -march=x86-64" +
// TODO: We're not really android, but we don't have a triple yet b/31393676
" -U__ANDROID__ -fno-emulated-tls"
}
func (t *toolchainLinuxBionic) ToolchainClangLdflags() string {

View File

@@ -234,8 +234,10 @@ func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
rpath_prefix = "@loader_path/"
}
for _, rpath := range linker.dynamicProperties.RunPaths {
flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
if !ctx.static() {
for _, rpath := range linker.dynamicProperties.RunPaths {
flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
}
}
}

View File

@@ -78,12 +78,29 @@ func (object *objectLinker) link(ctx ModuleContext,
objs = objs.Append(deps.Objs)
var outputFile android.Path
builderFlags := flagsToBuilderFlags(flags)
if len(objs.objFiles) == 1 {
outputFile = objs.objFiles[0]
if object.Properties.Prefix_symbols != "" {
output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
TransformBinaryPrefixSymbols(ctx, object.Properties.Prefix_symbols, outputFile,
builderFlags, output)
outputFile = output
}
} else {
output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
TransformObjsToObj(ctx, objs.objFiles, flagsToBuilderFlags(flags), output)
outputFile = output
if object.Properties.Prefix_symbols != "" {
input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
TransformBinaryPrefixSymbols(ctx, object.Properties.Prefix_symbols, input,
builderFlags, output)
output = input
}
TransformObjsToObj(ctx, objs.objFiles, builderFlags, output)
}
ctx.CheckbuildFile(outputFile)

View File

@@ -0,0 +1,20 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
blueprint_go_binary {
name: "extract_linker",
srcs: ["main.go"],
testSrcs: ["main_test.go"],
}

154
cmd/extract_linker/main.go Normal file
View File

@@ -0,0 +1,154 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This tool extracts ELF LOAD segments from our linker binary, and produces an
// assembly file and linker script which will embed those segments as sections
// in another binary.
package main
import (
"bytes"
"debug/elf"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"text/template"
)
var linkerScriptTemplate = template.Must(template.New("linker_script").Parse(`
ENTRY(__dlwrap__start)
SECTIONS {
__dlwrap_original_start = _start;
/DISCARD/ : { *(.interp) }
{{range .}}
. = {{ printf "0x%x" .Vaddr }};
{{.Name}} : { KEEP(*({{.Name}})) }
{{end}}
.text : { *(.text .text.*) }
.rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }
.data : { *(.data .data.* .gnu.linkonce.d.*) }
.bss : { *(.dynbss) *(.bss .bss.* .gnu.linkonce.b.*) *(COMMON) }
}
`))
type LinkerSection struct {
Name string
Vaddr uint64
}
func main() {
var asmPath string
var scriptPath string
flag.StringVar(&asmPath, "s", "", "Path to save the assembly file")
flag.StringVar(&scriptPath, "T", "", "Path to save the linker script")
flag.Parse()
f, err := os.Open(flag.Arg(0))
if err != nil {
log.Fatalf("Error opening %q: %v", flag.Arg(0), err)
}
defer f.Close()
ef, err := elf.NewFile(f)
if err != nil {
log.Fatal("Unable to read elf file: %v", err)
}
asm := &bytes.Buffer{}
fmt.Fprintln(asm, ".globl __dlwrap_linker_entry")
fmt.Fprintf(asm, ".set __dlwrap_linker_entry, 0x%x\n\n", ef.Entry)
baseLoadAddr := uint64(0x1000)
sections := []LinkerSection{}
load := 0
for _, prog := range ef.Progs {
if prog.Type != elf.PT_LOAD {
continue
}
sectionName := fmt.Sprintf(".linker.sect%d", load)
flags := ""
if prog.Flags&elf.PF_W != 0 {
flags += "w"
}
if prog.Flags&elf.PF_X != 0 {
flags += "x"
}
fmt.Fprintf(asm, ".section %s, \"a%s\"\n", sectionName, flags)
if load == 0 {
fmt.Fprintln(asm, ".globl __dlwrap_linker_code_start")
fmt.Fprintln(asm, "__dlwrap_linker_code_start:")
}
buffer, _ := ioutil.ReadAll(prog.Open())
bytesToAsm(asm, buffer)
// Fill in zeros for any BSS sections. It would be nice to keep
// this as a true BSS, but ld/gold isn't preserving those,
// instead combining the segments with the following segment,
// and BSS only exists at the end of a LOAD segment. The
// linker doesn't use a lot of BSS, so this isn't a huge
// problem.
if prog.Memsz > prog.Filesz {
fmt.Fprintf(asm, ".fill 0x%x, 1, 0\n", prog.Memsz-prog.Filesz)
}
fmt.Fprintln(asm)
sections = append(sections, LinkerSection{
Name: sectionName,
Vaddr: baseLoadAddr + prog.Vaddr,
})
load += 1
}
if asmPath != "" {
if err := ioutil.WriteFile(asmPath, asm.Bytes(), 0777); err != nil {
log.Fatal("Unable to write %q: %v", asmPath, err)
}
}
if scriptPath != "" {
buf := &bytes.Buffer{}
if err := linkerScriptTemplate.Execute(buf, sections); err != nil {
log.Fatal("Failed to create linker script: %v", err)
}
if err := ioutil.WriteFile(scriptPath, buf.Bytes(), 0777); err != nil {
log.Fatal("Unable to write %q: %v", scriptPath, err)
}
}
}
func bytesToAsm(asm io.Writer, buf []byte) {
for i, b := range buf {
if i%64 == 0 {
if i != 0 {
fmt.Fprint(asm, "\n")
}
fmt.Fprint(asm, ".byte ")
} else {
fmt.Fprint(asm, ",")
}
fmt.Fprintf(asm, "%d", b)
}
fmt.Fprintln(asm)
}

View File

@@ -0,0 +1,63 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"testing"
)
var bytesToAsmTestCases = []struct {
name string
in []byte
out string
}{
{
name: "empty",
in: []byte{},
out: "\n",
},
{
name: "short",
in: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01},
out: ".byte 127,69,76,70,2,1\n",
},
{
name: "multiline",
in: []byte{0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x30, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
0x50, 0x98, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x88, 0xd1, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00,
0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00,
0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00},
out: ".byte 127,69,76,70,2,1,1,0,0,0,0,0,0,0,0,0,48,0,62,0,1,0,0,0,80,152,2,0,0,0,0,0,64,0,0,0,0,0,0,0,136,209,18,0,0,0,0,0,0,0,0,0,64,0,56,0,1,0,0,0,64,0,56,0\n" +
".byte 2,0,0,0,64,0,56,0\n",
},
}
func TestBytesToAsm(t *testing.T) {
for _, testcase := range bytesToAsmTestCases {
t.Run(testcase.name, func(t *testing.T) {
buf := bytes.Buffer{}
bytesToAsm(&buf, testcase.in)
if buf.String() != testcase.out {
t.Errorf("input: %#v\n want: %q\n got: %q\n", testcase.in, testcase.out, buf.String())
}
})
}
}