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 |
| 16 | // assembly file and linker script which will embed those segments as sections |
| 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" |
| 29 | "text/template" |
| 30 | ) |
| 31 | |
| 32 | var linkerScriptTemplate = template.Must(template.New("linker_script").Parse(` |
| 33 | ENTRY(__dlwrap__start) |
| 34 | SECTIONS { |
| 35 | __dlwrap_original_start = _start; |
| 36 | /DISCARD/ : { *(.interp) } |
| 37 | |
| 38 | {{range .}} |
| 39 | . = {{ printf "0x%x" .Vaddr }}; |
| 40 | {{.Name}} : { KEEP(*({{.Name}})) } |
| 41 | {{end}} |
| 42 | |
| 43 | .text : { *(.text .text.*) } |
| 44 | .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } |
| 45 | .data : { *(.data .data.* .gnu.linkonce.d.*) } |
| 46 | .bss : { *(.dynbss) *(.bss .bss.* .gnu.linkonce.b.*) *(COMMON) } |
| 47 | } |
| 48 | `)) |
| 49 | |
| 50 | type LinkerSection struct { |
| 51 | Name string |
| 52 | Vaddr uint64 |
| 53 | } |
| 54 | |
| 55 | func main() { |
| 56 | var asmPath string |
| 57 | var scriptPath string |
| 58 | |
| 59 | flag.StringVar(&asmPath, "s", "", "Path to save the assembly file") |
| 60 | flag.StringVar(&scriptPath, "T", "", "Path to save the linker script") |
| 61 | flag.Parse() |
| 62 | |
| 63 | f, err := os.Open(flag.Arg(0)) |
| 64 | if err != nil { |
| 65 | log.Fatalf("Error opening %q: %v", flag.Arg(0), err) |
| 66 | } |
| 67 | defer f.Close() |
| 68 | |
| 69 | ef, err := elf.NewFile(f) |
| 70 | if err != nil { |
Colin Cross | f46e37f | 2018-03-21 16:25:58 -0700 | [diff] [blame^] | 71 | log.Fatalf("Unable to read elf file: %v", err) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | asm := &bytes.Buffer{} |
| 75 | |
| 76 | fmt.Fprintln(asm, ".globl __dlwrap_linker_entry") |
| 77 | fmt.Fprintf(asm, ".set __dlwrap_linker_entry, 0x%x\n\n", ef.Entry) |
| 78 | |
| 79 | baseLoadAddr := uint64(0x1000) |
| 80 | sections := []LinkerSection{} |
| 81 | load := 0 |
| 82 | for _, prog := range ef.Progs { |
| 83 | if prog.Type != elf.PT_LOAD { |
| 84 | continue |
| 85 | } |
| 86 | |
| 87 | sectionName := fmt.Sprintf(".linker.sect%d", load) |
| 88 | flags := "" |
| 89 | if prog.Flags&elf.PF_W != 0 { |
| 90 | flags += "w" |
| 91 | } |
| 92 | if prog.Flags&elf.PF_X != 0 { |
| 93 | flags += "x" |
| 94 | } |
| 95 | fmt.Fprintf(asm, ".section %s, \"a%s\"\n", sectionName, flags) |
| 96 | |
| 97 | if load == 0 { |
| 98 | fmt.Fprintln(asm, ".globl __dlwrap_linker_code_start") |
| 99 | fmt.Fprintln(asm, "__dlwrap_linker_code_start:") |
| 100 | } |
| 101 | |
| 102 | buffer, _ := ioutil.ReadAll(prog.Open()) |
| 103 | bytesToAsm(asm, buffer) |
| 104 | |
| 105 | // Fill in zeros for any BSS sections. It would be nice to keep |
| 106 | // this as a true BSS, but ld/gold isn't preserving those, |
| 107 | // instead combining the segments with the following segment, |
| 108 | // and BSS only exists at the end of a LOAD segment. The |
| 109 | // linker doesn't use a lot of BSS, so this isn't a huge |
| 110 | // problem. |
| 111 | if prog.Memsz > prog.Filesz { |
| 112 | fmt.Fprintf(asm, ".fill 0x%x, 1, 0\n", prog.Memsz-prog.Filesz) |
| 113 | } |
| 114 | fmt.Fprintln(asm) |
| 115 | |
| 116 | sections = append(sections, LinkerSection{ |
| 117 | Name: sectionName, |
| 118 | Vaddr: baseLoadAddr + prog.Vaddr, |
| 119 | }) |
| 120 | |
| 121 | load += 1 |
| 122 | } |
| 123 | |
| 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 | |
| 130 | if scriptPath != "" { |
| 131 | buf := &bytes.Buffer{} |
| 132 | if err := linkerScriptTemplate.Execute(buf, sections); err != nil { |
Colin Cross | f46e37f | 2018-03-21 16:25:58 -0700 | [diff] [blame^] | 133 | log.Fatalf("Failed to create linker script: %v", err) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 134 | } |
| 135 | if err := ioutil.WriteFile(scriptPath, buf.Bytes(), 0777); err != nil { |
Colin Cross | f46e37f | 2018-03-21 16:25:58 -0700 | [diff] [blame^] | 136 | log.Fatalf("Unable to write %q: %v", scriptPath, err) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func bytesToAsm(asm io.Writer, buf []byte) { |
| 142 | for i, b := range buf { |
| 143 | if i%64 == 0 { |
| 144 | if i != 0 { |
| 145 | fmt.Fprint(asm, "\n") |
| 146 | } |
| 147 | fmt.Fprint(asm, ".byte ") |
| 148 | } else { |
| 149 | fmt.Fprint(asm, ",") |
| 150 | } |
| 151 | fmt.Fprintf(asm, "%d", b) |
| 152 | } |
| 153 | fmt.Fprintln(asm) |
| 154 | } |