blob: 2dcb8948e6e070be4607f034f40ca2285d83998b [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"
Dan Willemsena0790e32018-10-12 00:24:23 -070029 "strings"
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
Dan Willemsenc77a0b32017-09-18 23:19:12 -070062 for _, prog := range ef.Progs {
63 if prog.Type != elf.PT_LOAD {
64 continue
65 }
66
Colin Cross009f3df2021-06-11 17:57:09 -070067 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 Willemsena0790e32018-10-12 00:24:23 -070076
Dan Willemsenc77a0b32017-09-18 23:19:12 -070077 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 Willemsena0790e32018-10-12 00:24:23 -070086 fmt.Fprintf(asm, ".globl %s\n%s:\n\n", symName, symName)
87
Colin Cross9cfe6112021-06-11 18:02:22 -070088 fmt.Fprintf(script, " %s %d : {\n", sectionName, baseLoadAddr+prog.Vaddr)
89 fmt.Fprintf(script, " KEEP(*(%s));\n", sectionName)
90 fmt.Fprintln(script, " }")
Dan Willemsenc77a0b32017-09-18 23:19:12 -070091
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 Willemsenc77a0b32017-09-18 23:19:12 -0700106 load += 1
107 }
108
Colin Cross9cfe6112021-06-11 18:02:22 -0700109 fmt.Fprintln(script, "}")
110 fmt.Fprintln(script, "INSERT BEFORE .note.android.ident;")
111
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700112 if asmPath != "" {
113 if err := ioutil.WriteFile(asmPath, asm.Bytes(), 0777); err != nil {
Colin Crossf46e37f2018-03-21 16:25:58 -0700114 log.Fatalf("Unable to write %q: %v", asmPath, err)
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700115 }
116 }
117
Colin Cross9cfe6112021-06-11 18:02:22 -0700118 if scriptPath != "" {
119 if err := ioutil.WriteFile(scriptPath, script.Bytes(), 0777); err != nil {
120 log.Fatalf("Unable to write %q: %v", scriptPath, err)
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700121 }
122 }
123}
124
125func 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 Cross009f3df2021-06-11 17:57:09 -0700139
140func 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}