blob: d62dea1c0bdead5ca12f0722f16797df5772959e [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
Dan Willemsena0790e32018-10-12 00:24:23 -070016// assembly file and linker flags 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
Dan Willemsena0790e32018-10-12 00:24:23 -070034 var flagsPath string
Dan Willemsenc77a0b32017-09-18 23:19:12 -070035
36 flag.StringVar(&asmPath, "s", "", "Path to save the assembly file")
Dan Willemsena0790e32018-10-12 00:24:23 -070037 flag.StringVar(&flagsPath, "f", "", "Path to save the linker flags")
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{}
Dan Willemsenc77a0b32017-09-18 23:19:12 -070052 baseLoadAddr := uint64(0x1000)
Dan Willemsenc77a0b32017-09-18 23:19:12 -070053 load := 0
Dan Willemsena0790e32018-10-12 00:24:23 -070054 linkFlags := []string{}
55
56 fmt.Fprintln(asm, ".globl __dlwrap_linker_offset")
57 fmt.Fprintf(asm, ".set __dlwrap_linker_offset, 0x%x\n", baseLoadAddr)
58
Dan Willemsenc77a0b32017-09-18 23:19:12 -070059 for _, prog := range ef.Progs {
60 if prog.Type != elf.PT_LOAD {
61 continue
62 }
63
Colin Cross009f3df2021-06-11 17:57:09 -070064 var progName string
65 progSection := progToFirstSection(prog, ef.Sections)
66 if progSection != nil {
67 progName = progSection.Name
68 } else {
69 progName = fmt.Sprintf(".sect%d", load)
70 }
71 sectionName := ".linker" + progName
72 symName := "__dlwrap_linker" + strings.ReplaceAll(progName, ".", "_")
Dan Willemsena0790e32018-10-12 00:24:23 -070073
Dan Willemsenc77a0b32017-09-18 23:19:12 -070074 flags := ""
75 if prog.Flags&elf.PF_W != 0 {
76 flags += "w"
77 }
78 if prog.Flags&elf.PF_X != 0 {
79 flags += "x"
80 }
81 fmt.Fprintf(asm, ".section %s, \"a%s\"\n", sectionName, flags)
82
Dan Willemsena0790e32018-10-12 00:24:23 -070083 fmt.Fprintf(asm, ".globl %s\n%s:\n\n", symName, symName)
84
85 linkFlags = append(linkFlags,
86 fmt.Sprintf("-Wl,--undefined=%s", symName),
87 fmt.Sprintf("-Wl,--section-start=%s=0x%x",
88 sectionName, baseLoadAddr+prog.Vaddr))
Dan Willemsenc77a0b32017-09-18 23:19:12 -070089
90 buffer, _ := ioutil.ReadAll(prog.Open())
91 bytesToAsm(asm, buffer)
92
93 // Fill in zeros for any BSS sections. It would be nice to keep
94 // this as a true BSS, but ld/gold isn't preserving those,
95 // instead combining the segments with the following segment,
96 // and BSS only exists at the end of a LOAD segment. The
97 // linker doesn't use a lot of BSS, so this isn't a huge
98 // problem.
99 if prog.Memsz > prog.Filesz {
100 fmt.Fprintf(asm, ".fill 0x%x, 1, 0\n", prog.Memsz-prog.Filesz)
101 }
102 fmt.Fprintln(asm)
103
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700104 load += 1
105 }
106
107 if asmPath != "" {
108 if err := ioutil.WriteFile(asmPath, asm.Bytes(), 0777); err != nil {
Colin Crossf46e37f2018-03-21 16:25:58 -0700109 log.Fatalf("Unable to write %q: %v", asmPath, err)
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700110 }
111 }
112
Dan Willemsena0790e32018-10-12 00:24:23 -0700113 if flagsPath != "" {
114 flags := strings.Join(linkFlags, " ")
115 if err := ioutil.WriteFile(flagsPath, []byte(flags), 0777); err != nil {
116 log.Fatalf("Unable to write %q: %v", flagsPath, err)
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700117 }
118 }
119}
120
121func bytesToAsm(asm io.Writer, buf []byte) {
122 for i, b := range buf {
123 if i%64 == 0 {
124 if i != 0 {
125 fmt.Fprint(asm, "\n")
126 }
127 fmt.Fprint(asm, ".byte ")
128 } else {
129 fmt.Fprint(asm, ",")
130 }
131 fmt.Fprintf(asm, "%d", b)
132 }
133 fmt.Fprintln(asm)
134}
Colin Cross009f3df2021-06-11 17:57:09 -0700135
136func progToFirstSection(prog *elf.Prog, sections []*elf.Section) *elf.Section {
137 for _, section := range sections {
138 if section.Addr == prog.Vaddr {
139 return section
140 }
141 }
142 return nil
143}