2017-09-19 08:19:12 +02:00
|
|
|
// 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
|
2021-06-12 03:02:22 +02:00
|
|
|
// assembly file and linker script which will embed those segments as sections
|
2017-09-19 08:19:12 +02:00
|
|
|
// in another binary.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"debug/elf"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-07-28 01:11:51 +02:00
|
|
|
"strconv"
|
2017-09-19 08:19:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var asmPath string
|
2021-06-12 03:02:22 +02:00
|
|
|
var scriptPath string
|
2017-09-19 08:19:12 +02:00
|
|
|
|
|
|
|
flag.StringVar(&asmPath, "s", "", "Path to save the assembly file")
|
2021-06-12 03:02:22 +02:00
|
|
|
flag.StringVar(&scriptPath, "T", "", "Path to save the linker script")
|
2017-09-19 08:19:12 +02:00
|
|
|
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 {
|
2018-03-22 00:25:58 +01:00
|
|
|
log.Fatalf("Unable to read elf file: %v", err)
|
2017-09-19 08:19:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
asm := &bytes.Buffer{}
|
2021-06-12 03:02:22 +02:00
|
|
|
script := &bytes.Buffer{}
|
2017-09-19 08:19:12 +02:00
|
|
|
baseLoadAddr := uint64(0x1000)
|
|
|
|
load := 0
|
2018-10-12 09:24:23 +02:00
|
|
|
|
|
|
|
fmt.Fprintln(asm, ".globl __dlwrap_linker_offset")
|
|
|
|
fmt.Fprintf(asm, ".set __dlwrap_linker_offset, 0x%x\n", baseLoadAddr)
|
|
|
|
|
2021-06-12 03:02:22 +02:00
|
|
|
fmt.Fprintln(script, "ENTRY(__dlwrap__start)")
|
|
|
|
fmt.Fprintln(script, "SECTIONS {")
|
|
|
|
|
2021-07-28 01:11:51 +02:00
|
|
|
progsWithFlagsCount := make(map[string]int)
|
|
|
|
|
2017-09-19 08:19:12 +02:00
|
|
|
for _, prog := range ef.Progs {
|
|
|
|
if prog.Type != elf.PT_LOAD {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-28 01:11:51 +02:00
|
|
|
progName := progNameFromFlags(prog.Flags, progsWithFlagsCount)
|
|
|
|
sectionName := ".linker_" + progName
|
|
|
|
symName := "__dlwrap_linker_" + progName
|
2018-10-12 09:24:23 +02:00
|
|
|
|
2017-09-19 08:19:12 +02:00
|
|
|
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)
|
|
|
|
|
2021-07-28 01:11:51 +02:00
|
|
|
if load == 0 {
|
|
|
|
fmt.Fprintln(asm, ".globl __dlwrap_linker")
|
|
|
|
fmt.Fprintln(asm, "__dlwrap_linker:")
|
|
|
|
fmt.Fprintln(asm)
|
|
|
|
}
|
|
|
|
|
2018-10-12 09:24:23 +02:00
|
|
|
fmt.Fprintf(asm, ".globl %s\n%s:\n\n", symName, symName)
|
|
|
|
|
2021-06-29 20:28:24 +02:00
|
|
|
fmt.Fprintf(script, " %s 0x%x : {\n", sectionName, baseLoadAddr+prog.Vaddr)
|
2021-06-12 03:02:22 +02:00
|
|
|
fmt.Fprintf(script, " KEEP(*(%s));\n", sectionName)
|
|
|
|
fmt.Fprintln(script, " }")
|
2017-09-19 08:19:12 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
load += 1
|
|
|
|
}
|
|
|
|
|
2021-07-28 01:11:51 +02:00
|
|
|
fmt.Fprintln(asm, ".globl __dlwrap_linker_end")
|
|
|
|
fmt.Fprintln(asm, "__dlwrap_linker_end:")
|
|
|
|
fmt.Fprintln(asm)
|
|
|
|
|
2021-06-29 20:28:24 +02:00
|
|
|
fmt.Fprintln(asm, `.section .note.android.embedded_linker,"a",%note`)
|
|
|
|
|
2021-06-12 03:02:22 +02:00
|
|
|
fmt.Fprintln(script, "}")
|
2021-06-29 20:28:24 +02:00
|
|
|
fmt.Fprintln(script, "INSERT BEFORE .note.android.embedded_linker;")
|
2021-06-12 03:02:22 +02:00
|
|
|
|
2017-09-19 08:19:12 +02:00
|
|
|
if asmPath != "" {
|
|
|
|
if err := ioutil.WriteFile(asmPath, asm.Bytes(), 0777); err != nil {
|
2018-03-22 00:25:58 +01:00
|
|
|
log.Fatalf("Unable to write %q: %v", asmPath, err)
|
2017-09-19 08:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 03:02:22 +02:00
|
|
|
if scriptPath != "" {
|
|
|
|
if err := ioutil.WriteFile(scriptPath, script.Bytes(), 0777); err != nil {
|
|
|
|
log.Fatalf("Unable to write %q: %v", scriptPath, err)
|
2017-09-19 08:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-06-12 02:57:09 +02:00
|
|
|
|
2021-07-28 01:11:51 +02:00
|
|
|
func progNameFromFlags(flags elf.ProgFlag, progsWithFlagsCount map[string]int) string {
|
|
|
|
s := ""
|
|
|
|
if flags&elf.PF_R != 0 {
|
|
|
|
s += "r"
|
|
|
|
}
|
|
|
|
if flags&elf.PF_W != 0 {
|
|
|
|
s += "w"
|
2021-06-12 02:57:09 +02:00
|
|
|
}
|
2021-07-28 01:11:51 +02:00
|
|
|
if flags&elf.PF_X != 0 {
|
|
|
|
s += "x"
|
|
|
|
}
|
|
|
|
|
|
|
|
count := progsWithFlagsCount[s]
|
|
|
|
count++
|
|
|
|
progsWithFlagsCount[s] = count
|
|
|
|
|
|
|
|
if count > 1 {
|
|
|
|
s += strconv.Itoa(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2021-06-12 02:57:09 +02:00
|
|
|
}
|