aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/extract_linker
diff options
context:
space:
mode:
authorDan Willemsen <dwillemsen@google.com>2017-09-18 23:19:12 -0700
committerDan Willemsen <dwillemsen@google.com>2017-09-20 15:34:33 -0700
commitc77a0b3f9c54e326341bc5b87ed5840c82ae79aa (patch)
treeb6b7e9b0f396a92690538e7f270e8e857b01b8a1 /cmd/extract_linker
parentefb1dd93bf9664e74a98ff14ad92f1da5e8378db (diff)
downloadbuild_soong-c77a0b3f9c54e326341bc5b87ed5840c82ae79aa.tar.gz
build_soong-c77a0b3f9c54e326341bc5b87ed5840c82ae79aa.tar.bz2
build_soong-c77a0b3f9c54e326341bc5b87ed5840c82ae79aa.zip
Extract the linker and embed it into host bionic binaries
The linux kernel requires that the ELF interpreter (runtime linker) that's referenced by PT_INTERP be either an absolute path, or a relative path from the current working directory. We'd prefer a relative path from the binary, similarly to how we handle looking up shared libraries, but that's not supported. Instead, extract the load sections from the runtime linker ELF binary and embed them into each host bionic binary, omitting the PT_INTERP declaration. The kernel will treat it as a static binary, and we'll use a special entry point (linker_wrapper) to fix up the arguments passed by the kernel before jumping to the embedded linker. From the linker's point of view, it looks like the kernel loaded the linker like normal. Bug: 31559095 Test: Enable host bionic, out/soong/host/linux_bionic-x86/nativetest64/libdemangle_test/libdemangle_test Change-Id: I8d0aea9790b5e86fcc3ea6e2d00cfa33907e2853
Diffstat (limited to 'cmd/extract_linker')
-rw-r--r--cmd/extract_linker/Android.bp20
-rw-r--r--cmd/extract_linker/main.go154
-rw-r--r--cmd/extract_linker/main_test.go63
3 files changed, 237 insertions, 0 deletions
diff --git a/cmd/extract_linker/Android.bp b/cmd/extract_linker/Android.bp
new file mode 100644
index 00000000..fe76ae41
--- /dev/null
+++ b/cmd/extract_linker/Android.bp
@@ -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"],
+}
+
diff --git a/cmd/extract_linker/main.go b/cmd/extract_linker/main.go
new file mode 100644
index 00000000..8530b4aa
--- /dev/null
+++ b/cmd/extract_linker/main.go
@@ -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)
+}
diff --git a/cmd/extract_linker/main_test.go b/cmd/extract_linker/main_test.go
new file mode 100644
index 00000000..6ac4ec60
--- /dev/null
+++ b/cmd/extract_linker/main_test.go
@@ -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())
+ }
+ })
+ }
+}