blob: aaca1ddc4b47aadb2e2cf818a496acdc6838f7b7 [file] [log] [blame]
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001// 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 Cross9cfe6112021-06-11 18:02:22 -070016// assembly file and linker script which will embed those segments as sections
Dan Willemsenc77a0b32017-09-18 23:19:12 -070017// in another binary.
18package main
19
20import (
21 "bytes"
22 "debug/elf"
23 "flag"
24 "fmt"
25 "io"
26 "io/ioutil"
27 "log"
28 "os"
Colin Cross5c180482021-07-27 16:11:51 -070029 "strconv"
Dan Willemsenc77a0b32017-09-18 23:19:12 -070030)
31
Dan Willemsenc77a0b32017-09-18 23:19:12 -070032func main() {
33 var asmPath string
Colin Cross9cfe6112021-06-11 18:02:22 -070034 var scriptPath string
Dan Willemsenc77a0b32017-09-18 23:19:12 -070035
36 flag.StringVar(&asmPath, "s", "", "Path to save the assembly file")
Colin Cross9cfe6112021-06-11 18:02:22 -070037 flag.StringVar(&scriptPath, "T", "", "Path to save the linker script")
Dan Willemsenc77a0b32017-09-18 23:19:12 -070038 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 Crossf46e37f2018-03-21 16:25:58 -070048 log.Fatalf("Unable to read elf file: %v", err)
Dan Willemsenc77a0b32017-09-18 23:19:12 -070049 }
50
51 asm := &bytes.Buffer{}
Colin Cross9cfe6112021-06-11 18:02:22 -070052 script := &bytes.Buffer{}
Dan Willemsenc77a0b32017-09-18 23:19:12 -070053 baseLoadAddr := uint64(0x1000)
Dan Willemsenc77a0b32017-09-18 23:19:12 -070054 load := 0
Dan Willemsena0790e32018-10-12 00:24:23 -070055
56 fmt.Fprintln(asm, ".globl __dlwrap_linker_offset")
57 fmt.Fprintf(asm, ".set __dlwrap_linker_offset, 0x%x\n", baseLoadAddr)
58
Colin Cross9cfe6112021-06-11 18:02:22 -070059 fmt.Fprintln(script, "ENTRY(__dlwrap__start)")
60 fmt.Fprintln(script, "SECTIONS {")
61
Colin Cross5c180482021-07-27 16:11:51 -070062 progsWithFlagsCount := make(map[string]int)
63
Dan Willemsenc77a0b32017-09-18 23:19:12 -070064 for _, prog := range ef.Progs {
65 if prog.Type != elf.PT_LOAD {
66 continue
67 }
68
Colin Cross5c180482021-07-27 16:11:51 -070069 progName := progNameFromFlags(prog.Flags, progsWithFlagsCount)
70 sectionName := ".linker_" + progName
71 symName := "__dlwrap_linker_" + progName
Dan Willemsena0790e32018-10-12 00:24:23 -070072
Dan Willemsenc77a0b32017-09-18 23:19:12 -070073 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 Cross5c180482021-07-27 16:11:51 -070082 if load == 0 {
83 fmt.Fprintln(asm, ".globl __dlwrap_linker")
84 fmt.Fprintln(asm, "__dlwrap_linker:")
85 fmt.Fprintln(asm)
86 }
87
Dan Willemsena0790e32018-10-12 00:24:23 -070088 fmt.Fprintf(asm, ".globl %s\n%s:\n\n", symName, symName)
89
Colin Cross1cd80d92021-06-29 11:28:24 -070090 fmt.Fprintf(script, " %s 0x%x : {\n", sectionName, baseLoadAddr+prog.Vaddr)
Colin Cross9cfe6112021-06-11 18:02:22 -070091 fmt.Fprintf(script, " KEEP(*(%s));\n", sectionName)
92 fmt.Fprintln(script, " }")
Dan Willemsenc77a0b32017-09-18 23:19:12 -070093
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 Willemsenc77a0b32017-09-18 23:19:12 -0700108 load += 1
109 }
110
Colin Cross5c180482021-07-27 16:11:51 -0700111 fmt.Fprintln(asm, ".globl __dlwrap_linker_end")
112 fmt.Fprintln(asm, "__dlwrap_linker_end:")
113 fmt.Fprintln(asm)
114
Colin Cross1cd80d92021-06-29 11:28:24 -0700115 fmt.Fprintln(asm, `.section .note.android.embedded_linker,"a",%note`)
116
Colin Cross4d9b1f12022-02-16 15:34:52 -0800117 // Discard the PT_INTERP section so that the linker doesn't need to be passed the
118 // --no-dynamic-linker flag.
Colin Cross36fd4eb2022-02-25 18:25:45 -0800119 fmt.Fprintln(script, " /DISCARD/ : { *(.interp) }")
Colin Cross4d9b1f12022-02-16 15:34:52 -0800120
Colin Cross9cfe6112021-06-11 18:02:22 -0700121 fmt.Fprintln(script, "}")
Colin Cross1cd80d92021-06-29 11:28:24 -0700122 fmt.Fprintln(script, "INSERT BEFORE .note.android.embedded_linker;")
Colin Cross9cfe6112021-06-11 18:02:22 -0700123
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700124 if asmPath != "" {
125 if err := ioutil.WriteFile(asmPath, asm.Bytes(), 0777); err != nil {
Colin Crossf46e37f2018-03-21 16:25:58 -0700126 log.Fatalf("Unable to write %q: %v", asmPath, err)
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700127 }
128 }
129
Colin Cross9cfe6112021-06-11 18:02:22 -0700130 if scriptPath != "" {
131 if err := ioutil.WriteFile(scriptPath, script.Bytes(), 0777); err != nil {
132 log.Fatalf("Unable to write %q: %v", scriptPath, err)
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700133 }
134 }
135}
136
137func 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 Cross009f3df2021-06-11 17:57:09 -0700151
Colin Cross5c180482021-07-27 16:11:51 -0700152func progNameFromFlags(flags elf.ProgFlag, progsWithFlagsCount map[string]int) string {
153 s := ""
154 if flags&elf.PF_R != 0 {
155 s += "r"
Colin Cross009f3df2021-06-11 17:57:09 -0700156 }
Colin Cross5c180482021-07-27 16:11:51 -0700157 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 Cross009f3df2021-06-11 17:57:09 -0700173}