Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 15 | // This tool extracts ELF LOAD segments from our linker binary, and produces an |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame^] | 16 | // assembly file and linker script which will embed those segments as sections |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 17 | // in another binary. |
| 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "bytes" |
| 22 | "debug/elf" |
| 23 | "flag" |
| 24 | "fmt" |
| 25 | "io" |
| 26 | "io/ioutil" |
| 27 | "log" |
| 28 | "os" |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 29 | "strings" |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 32 | func main() { |
| 33 | var asmPath string |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame^] | 34 | var scriptPath string |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 35 | |
| 36 | flag.StringVar(&asmPath, "s", "", "Path to save the assembly file") |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame^] | 37 | flag.StringVar(&scriptPath, "T", "", "Path to save the linker script") |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 38 | flag.Parse() |
| 39 | |
| 40 | f, err := os.Open(flag.Arg(0)) |
| 41 | if err != nil { |
| 42 | log.Fatalf("Error opening %q: %v", flag.Arg(0), err) |
| 43 | } |
| 44 | defer f.Close() |
| 45 | |
| 46 | ef, err := elf.NewFile(f) |
| 47 | if err != nil { |
Colin Cross | f46e37f | 2018-03-21 16:25:58 -0700 | [diff] [blame] | 48 | log.Fatalf("Unable to read elf file: %v", err) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | asm := &bytes.Buffer{} |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame^] | 52 | script := &bytes.Buffer{} |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 53 | baseLoadAddr := uint64(0x1000) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 54 | load := 0 |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 55 | |
| 56 | fmt.Fprintln(asm, ".globl __dlwrap_linker_offset") |
| 57 | fmt.Fprintf(asm, ".set __dlwrap_linker_offset, 0x%x\n", baseLoadAddr) |
| 58 | |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame^] | 59 | fmt.Fprintln(script, "ENTRY(__dlwrap__start)") |
| 60 | fmt.Fprintln(script, "SECTIONS {") |
| 61 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 62 | for _, prog := range ef.Progs { |
| 63 | if prog.Type != elf.PT_LOAD { |
| 64 | continue |
| 65 | } |
| 66 | |
Colin Cross | 009f3df | 2021-06-11 17:57:09 -0700 | [diff] [blame] | 67 | var progName string |
| 68 | progSection := progToFirstSection(prog, ef.Sections) |
| 69 | if progSection != nil { |
| 70 | progName = progSection.Name |
| 71 | } else { |
| 72 | progName = fmt.Sprintf(".sect%d", load) |
| 73 | } |
| 74 | sectionName := ".linker" + progName |
| 75 | symName := "__dlwrap_linker" + strings.ReplaceAll(progName, ".", "_") |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 76 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 77 | flags := "" |
| 78 | if prog.Flags&elf.PF_W != 0 { |
| 79 | flags += "w" |
| 80 | } |
| 81 | if prog.Flags&elf.PF_X != 0 { |
| 82 | flags += "x" |
| 83 | } |
| 84 | fmt.Fprintf(asm, ".section %s, \"a%s\"\n", sectionName, flags) |
| 85 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 86 | fmt.Fprintf(asm, ".globl %s\n%s:\n\n", symName, symName) |
| 87 | |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame^] | 88 | fmt.Fprintf(script, " %s %d : {\n", sectionName, baseLoadAddr+prog.Vaddr) |
| 89 | fmt.Fprintf(script, " KEEP(*(%s));\n", sectionName) |
| 90 | fmt.Fprintln(script, " }") |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 91 | |
| 92 | buffer, _ := ioutil.ReadAll(prog.Open()) |
| 93 | bytesToAsm(asm, buffer) |
| 94 | |
| 95 | // Fill in zeros for any BSS sections. It would be nice to keep |
| 96 | // this as a true BSS, but ld/gold isn't preserving those, |
| 97 | // instead combining the segments with the following segment, |
| 98 | // and BSS only exists at the end of a LOAD segment. The |
| 99 | // linker doesn't use a lot of BSS, so this isn't a huge |
| 100 | // problem. |
| 101 | if prog.Memsz > prog.Filesz { |
| 102 | fmt.Fprintf(asm, ".fill 0x%x, 1, 0\n", prog.Memsz-prog.Filesz) |
| 103 | } |
| 104 | fmt.Fprintln(asm) |
| 105 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 106 | load += 1 |
| 107 | } |
| 108 | |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame^] | 109 | fmt.Fprintln(script, "}") |
| 110 | fmt.Fprintln(script, "INSERT BEFORE .note.android.ident;") |
| 111 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 112 | if asmPath != "" { |
| 113 | if err := ioutil.WriteFile(asmPath, asm.Bytes(), 0777); err != nil { |
Colin Cross | f46e37f | 2018-03-21 16:25:58 -0700 | [diff] [blame] | 114 | log.Fatalf("Unable to write %q: %v", asmPath, err) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame^] | 118 | if scriptPath != "" { |
| 119 | if err := ioutil.WriteFile(scriptPath, script.Bytes(), 0777); err != nil { |
| 120 | log.Fatalf("Unable to write %q: %v", scriptPath, err) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func bytesToAsm(asm io.Writer, buf []byte) { |
| 126 | for i, b := range buf { |
| 127 | if i%64 == 0 { |
| 128 | if i != 0 { |
| 129 | fmt.Fprint(asm, "\n") |
| 130 | } |
| 131 | fmt.Fprint(asm, ".byte ") |
| 132 | } else { |
| 133 | fmt.Fprint(asm, ",") |
| 134 | } |
| 135 | fmt.Fprintf(asm, "%d", b) |
| 136 | } |
| 137 | fmt.Fprintln(asm) |
| 138 | } |
Colin Cross | 009f3df | 2021-06-11 17:57:09 -0700 | [diff] [blame] | 139 | |
| 140 | func progToFirstSection(prog *elf.Prog, sections []*elf.Section) *elf.Section { |
| 141 | for _, section := range sections { |
| 142 | if section.Addr == prog.Vaddr { |
| 143 | return section |
| 144 | } |
| 145 | } |
| 146 | return nil |
| 147 | } |