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" |
Colin Cross | 5c18048 | 2021-07-27 16:11:51 -0700 | [diff] [blame] | 29 | "strconv" |
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 | |
Colin Cross | 5c18048 | 2021-07-27 16:11:51 -0700 | [diff] [blame] | 62 | progsWithFlagsCount := make(map[string]int) |
| 63 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 64 | for _, prog := range ef.Progs { |
| 65 | if prog.Type != elf.PT_LOAD { |
| 66 | continue |
| 67 | } |
| 68 | |
Colin Cross | 5c18048 | 2021-07-27 16:11:51 -0700 | [diff] [blame] | 69 | progName := progNameFromFlags(prog.Flags, progsWithFlagsCount) |
| 70 | sectionName := ".linker_" + progName |
| 71 | symName := "__dlwrap_linker_" + progName |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 72 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 73 | flags := "" |
| 74 | if prog.Flags&elf.PF_W != 0 { |
| 75 | flags += "w" |
| 76 | } |
| 77 | if prog.Flags&elf.PF_X != 0 { |
| 78 | flags += "x" |
| 79 | } |
| 80 | fmt.Fprintf(asm, ".section %s, \"a%s\"\n", sectionName, flags) |
| 81 | |
Colin Cross | 5c18048 | 2021-07-27 16:11:51 -0700 | [diff] [blame] | 82 | if load == 0 { |
| 83 | fmt.Fprintln(asm, ".globl __dlwrap_linker") |
| 84 | fmt.Fprintln(asm, "__dlwrap_linker:") |
| 85 | fmt.Fprintln(asm) |
| 86 | } |
| 87 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 88 | fmt.Fprintf(asm, ".globl %s\n%s:\n\n", symName, symName) |
| 89 | |
Colin Cross | 1cd80d9 | 2021-06-29 11:28:24 -0700 | [diff] [blame] | 90 | fmt.Fprintf(script, " %s 0x%x : {\n", sectionName, baseLoadAddr+prog.Vaddr) |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame] | 91 | fmt.Fprintf(script, " KEEP(*(%s));\n", sectionName) |
| 92 | fmt.Fprintln(script, " }") |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 93 | |
| 94 | buffer, _ := ioutil.ReadAll(prog.Open()) |
| 95 | bytesToAsm(asm, buffer) |
| 96 | |
| 97 | // Fill in zeros for any BSS sections. It would be nice to keep |
| 98 | // this as a true BSS, but ld/gold isn't preserving those, |
| 99 | // instead combining the segments with the following segment, |
| 100 | // and BSS only exists at the end of a LOAD segment. The |
| 101 | // linker doesn't use a lot of BSS, so this isn't a huge |
| 102 | // problem. |
| 103 | if prog.Memsz > prog.Filesz { |
| 104 | fmt.Fprintf(asm, ".fill 0x%x, 1, 0\n", prog.Memsz-prog.Filesz) |
| 105 | } |
| 106 | fmt.Fprintln(asm) |
| 107 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 108 | load += 1 |
| 109 | } |
| 110 | |
Colin Cross | 5c18048 | 2021-07-27 16:11:51 -0700 | [diff] [blame] | 111 | fmt.Fprintln(asm, ".globl __dlwrap_linker_end") |
| 112 | fmt.Fprintln(asm, "__dlwrap_linker_end:") |
| 113 | fmt.Fprintln(asm) |
| 114 | |
Colin Cross | 1cd80d9 | 2021-06-29 11:28:24 -0700 | [diff] [blame] | 115 | fmt.Fprintln(asm, `.section .note.android.embedded_linker,"a",%note`) |
| 116 | |
Colin Cross | 4d9b1f1 | 2022-02-16 15:34:52 -0800 | [diff] [blame] | 117 | // Discard the PT_INTERP section so that the linker doesn't need to be passed the |
| 118 | // --no-dynamic-linker flag. |
Colin Cross | 36fd4eb | 2022-02-25 18:25:45 -0800 | [diff] [blame] | 119 | fmt.Fprintln(script, " /DISCARD/ : { *(.interp) }") |
Colin Cross | 4d9b1f1 | 2022-02-16 15:34:52 -0800 | [diff] [blame] | 120 | |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame] | 121 | fmt.Fprintln(script, "}") |
Colin Cross | 1cd80d9 | 2021-06-29 11:28:24 -0700 | [diff] [blame] | 122 | fmt.Fprintln(script, "INSERT BEFORE .note.android.embedded_linker;") |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame] | 123 | |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 124 | if asmPath != "" { |
| 125 | if err := ioutil.WriteFile(asmPath, asm.Bytes(), 0777); err != nil { |
Colin Cross | f46e37f | 2018-03-21 16:25:58 -0700 | [diff] [blame] | 126 | log.Fatalf("Unable to write %q: %v", asmPath, err) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Colin Cross | 9cfe611 | 2021-06-11 18:02:22 -0700 | [diff] [blame] | 130 | if scriptPath != "" { |
| 131 | if err := ioutil.WriteFile(scriptPath, script.Bytes(), 0777); err != nil { |
| 132 | log.Fatalf("Unable to write %q: %v", scriptPath, err) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func bytesToAsm(asm io.Writer, buf []byte) { |
| 138 | for i, b := range buf { |
| 139 | if i%64 == 0 { |
| 140 | if i != 0 { |
| 141 | fmt.Fprint(asm, "\n") |
| 142 | } |
| 143 | fmt.Fprint(asm, ".byte ") |
| 144 | } else { |
| 145 | fmt.Fprint(asm, ",") |
| 146 | } |
| 147 | fmt.Fprintf(asm, "%d", b) |
| 148 | } |
| 149 | fmt.Fprintln(asm) |
| 150 | } |
Colin Cross | 009f3df | 2021-06-11 17:57:09 -0700 | [diff] [blame] | 151 | |
Colin Cross | 5c18048 | 2021-07-27 16:11:51 -0700 | [diff] [blame] | 152 | func progNameFromFlags(flags elf.ProgFlag, progsWithFlagsCount map[string]int) string { |
| 153 | s := "" |
| 154 | if flags&elf.PF_R != 0 { |
| 155 | s += "r" |
Colin Cross | 009f3df | 2021-06-11 17:57:09 -0700 | [diff] [blame] | 156 | } |
Colin Cross | 5c18048 | 2021-07-27 16:11:51 -0700 | [diff] [blame] | 157 | if flags&elf.PF_W != 0 { |
| 158 | s += "w" |
| 159 | } |
| 160 | if flags&elf.PF_X != 0 { |
| 161 | s += "x" |
| 162 | } |
| 163 | |
| 164 | count := progsWithFlagsCount[s] |
| 165 | count++ |
| 166 | progsWithFlagsCount[s] = count |
| 167 | |
| 168 | if count > 1 { |
| 169 | s += strconv.Itoa(count) |
| 170 | } |
| 171 | |
| 172 | return s |
Colin Cross | 009f3df | 2021-06-11 17:57:09 -0700 | [diff] [blame] | 173 | } |