| 1 | // Copyright 2014 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 | package elfexec |
| 16 | |
| 17 | import ( |
| 18 | "debug/elf" |
| 19 | "fmt" |
| 20 | "reflect" |
| 21 | "strings" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | func TestGetBase(t *testing.T) { |
| 26 | |
| 27 | fhExec := &elf.FileHeader{ |
| 28 | Type: elf.ET_EXEC, |
| 29 | } |
| 30 | fhRel := &elf.FileHeader{ |
| 31 | Type: elf.ET_REL, |
| 32 | } |
| 33 | fhDyn := &elf.FileHeader{ |
| 34 | Type: elf.ET_DYN, |
| 35 | } |
| 36 | lsOffset := &elf.ProgHeader{ |
| 37 | Vaddr: 0x400000, |
| 38 | Off: 0x200000, |
| 39 | } |
| 40 | kernelHeader := &elf.ProgHeader{ |
| 41 | Vaddr: 0xffffffff81000000, |
| 42 | } |
| 43 | kernelAslrHeader := &elf.ProgHeader{ |
| 44 | Vaddr: 0xffffffff80200000, |
| 45 | Off: 0x1000, |
| 46 | } |
| 47 | // Kernel PIE header with vaddr aligned to a 4k boundary |
| 48 | kernelPieAlignedHeader := &elf.ProgHeader{ |
| 49 | Vaddr: 0xffff800010010000, |
| 50 | Off: 0x10000, |
| 51 | } |
| 52 | // Kernel PIE header with vaddr that doesn't fall on a 4k boundary |
| 53 | kernelPieUnalignedHeader := &elf.ProgHeader{ |
| 54 | Vaddr: 0xffffffc010080800, |
| 55 | Off: 0x10800, |
| 56 | } |
| 57 | ppc64KernelHeader := &elf.ProgHeader{ |
| 58 | Vaddr: 0xc000000000000000, |
| 59 | } |
| 60 | |
| 61 | testcases := []struct { |
| 62 | label string |
| 63 | fh *elf.FileHeader |
| 64 | loadSegment *elf.ProgHeader |
| 65 | stextOffset *uint64 |
| 66 | start, limit, offset uint64 |
| 67 | want uint64 |
| 68 | wanterr bool |
| 69 | }{ |
| 70 | {"exec", fhExec, nil, nil, 0x400000, 0, 0, 0, false}, |
| 71 | {"exec offset", fhExec, lsOffset, nil, 0x400000, 0x800000, 0, 0x200000, false}, |
| 72 | {"exec offset 2", fhExec, lsOffset, nil, 0x200000, 0x600000, 0, 0, false}, |
| 73 | {"exec nomap", fhExec, nil, nil, 0, 0, 0, 0, false}, |
| 74 | {"exec kernel", fhExec, kernelHeader, uint64p(0xffffffff81000198), 0xffffffff82000198, 0xffffffff83000198, 0, 0x1000000, false}, |
| 75 | {"exec kernel", fhExec, kernelHeader, uint64p(0xffffffff810002b8), 0xffffffff81000000, 0xffffffffa0000000, 0x0, 0x0, false}, |
| 76 | {"exec kernel ASLR", fhExec, kernelHeader, uint64p(0xffffffff810002b8), 0xffffffff81000000, 0xffffffffa0000000, 0xffffffff81000000, 0x0, false}, |
| 77 | // TODO(aalexand): Figure out where this test case exactly comes from and |
| 78 | // whether it's still relevant. |
| 79 | {"exec kernel ASLR 2", fhExec, kernelAslrHeader, nil, 0xffffffff83e00000, 0xfffffffffc3fffff, 0x3c00000, 0x3c00000, false}, |
| 80 | {"exec PPC64 kernel", fhExec, ppc64KernelHeader, uint64p(0xc000000000000000), 0xc000000000000000, 0xd00000001a730000, 0x0, 0x0, false}, |
| 81 | {"exec chromeos kernel", fhExec, kernelHeader, uint64p(0xffffffff81000198), 0, 0x10197, 0, 0x7efffe68, false}, |
| 82 | {"exec chromeos kernel 2", fhExec, kernelHeader, uint64p(0xffffffff81000198), 0, 0x10198, 0, 0x7efffe68, false}, |
| 83 | {"exec chromeos kernel 3", fhExec, kernelHeader, uint64p(0xffffffff81000198), 0x198, 0x100000, 0, 0x7f000000, false}, |
| 84 | {"exec chromeos kernel 4", fhExec, kernelHeader, uint64p(0xffffffff81200198), 0x198, 0x100000, 0, 0x7ee00000, false}, |
| 85 | {"exec chromeos kernel unremapped", fhExec, kernelHeader, uint64p(0xffffffff810001c8), 0xffffffff834001c8, 0xffffffffc0000000, 0xffffffff834001c8, 0x2400000, false}, |
| 86 | {"dyn", fhDyn, nil, nil, 0x200000, 0x300000, 0, 0x200000, false}, |
| 87 | {"dyn map", fhDyn, lsOffset, nil, 0x0, 0x300000, 0, 0xFFFFFFFFFFE00000, false}, |
| 88 | {"dyn nomap", fhDyn, nil, nil, 0x0, 0x0, 0, 0, false}, |
| 89 | {"dyn map+offset", fhDyn, lsOffset, nil, 0x900000, 0xa00000, 0x200000, 0x500000, false}, |
| 90 | {"dyn kernel", fhDyn, kernelPieAlignedHeader, uint64p(0xffff800010000000), 0xffff800010000000, 0xffff800012815c00, 0xffff800010000000, 0, false}, |
| 91 | {"dyn chromeos aslr kernel", fhDyn, kernelPieUnalignedHeader, uint64p(0xffffffc010080800), 0x800, 0xb7f800, 0, 0x3feff80000, false}, |
| 92 | {"dyn chromeos aslr kernel unremapped", fhDyn, kernelPieUnalignedHeader, uint64p(0xffffffc010080800), 0xffffffdb5d680800, 0xffffffdb5e200000, 0xffffffdb5d680800, 0x1b4d600000, false}, |
| 93 | {"rel", fhRel, nil, nil, 0x2000000, 0x3000000, 0, 0x2000000, false}, |
| 94 | {"rel nomap", fhRel, nil, nil, 0x0, ^uint64(0), 0, 0, false}, |
| 95 | {"rel offset", fhRel, nil, nil, 0x100000, 0x200000, 0x1, 0, true}, |
| 96 | } |
| 97 | |
| 98 | for _, tc := range testcases { |
| 99 | base, err := GetBase(tc.fh, tc.loadSegment, tc.stextOffset, tc.start, tc.limit, tc.offset) |
| 100 | if err != nil { |
| 101 | if !tc.wanterr { |
| 102 | t.Errorf("%s: want no error, got %v", tc.label, err) |
| 103 | } |
| 104 | continue |
| 105 | } |
| 106 | if tc.wanterr { |
| 107 | t.Errorf("%s: want error, got nil", tc.label) |
| 108 | continue |
| 109 | } |
| 110 | if base != tc.want { |
| 111 | t.Errorf("%s: want 0x%x, got 0x%x", tc.label, tc.want, base) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func uint64p(n uint64) *uint64 { |
| 117 | return &n |
| 118 | } |
| 119 | |
| 120 | func TestFindProgHeaderForMapping(t *testing.T) { |
| 121 | buildList := func(headers []*elf.ProgHeader) (result string) { |
| 122 | builder := strings.Builder{} |
| 123 | if err := builder.WriteByte('['); err != nil { |
| 124 | t.Error("Failed to append '[' to the builder") |
| 125 | } |
| 126 | defer func() { |
| 127 | if err := builder.WriteByte(']'); err != nil { |
| 128 | t.Error("Failed to append ']' to the builder") |
| 129 | } |
| 130 | result = builder.String() |
| 131 | }() |
| 132 | if len(headers) == 0 { |
| 133 | if _, err := builder.WriteString("nil"); err != nil { |
| 134 | t.Error("Failed to append 'nil' to the builder") |
| 135 | } |
| 136 | return |
| 137 | } |
| 138 | if _, err := builder.WriteString(fmt.Sprintf("%#v", *headers[0])); err != nil { |
| 139 | t.Error("Failed to append first header to the builder") |
| 140 | } |
| 141 | for i, h := range headers[1:] { |
| 142 | if _, err := builder.WriteString(fmt.Sprintf(", %#v", *h)); err != nil { |
| 143 | t.Errorf("Failed to append header %d to the builder", i+1) |
| 144 | } |
| 145 | } |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | // Various ELF program headers for unit tests. |
| 150 | tinyHeaders := []elf.ProgHeader{ |
| 151 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 152 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}, |
| 153 | } |
| 154 | tinyBadBSSHeaders := []elf.ProgHeader{ |
| 155 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 156 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x100, Memsz: 0x1f0, Align: 0x200000}, |
| 157 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xd80, Vaddr: 0x400d80, Paddr: 0x400d80, Filesz: 0x90, Memsz: 0x90, Align: 0x200000}, |
| 158 | } |
| 159 | smallHeaders := []elf.ProgHeader{ |
| 160 | {Type: elf.PT_PHDR, Flags: elf.PF_R | elf.PF_X, Off: 0x40, Vaddr: 0x400040, Paddr: 0x400040, Filesz: 0x1f8, Memsz: 0x1f8, Align: 8}, |
| 161 | {Type: elf.PT_INTERP, Flags: elf.PF_R, Off: 0x238, Vaddr: 0x400238, Paddr: 0x400238, Filesz: 0x1c, Memsz: 0x1c, Align: 1}, |
| 162 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0x400000, Paddr: 0x400000, Filesz: 0x6fc, Memsz: 0x6fc, Align: 0x200000}, |
| 163 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe10, Vaddr: 0x600e10, Paddr: 0x600e10, Filesz: 0x230, Memsz: 0x238, Align: 0x200000}, |
| 164 | {Type: elf.PT_DYNAMIC, Flags: elf.PF_R | elf.PF_W, Off: 0xe28, Vaddr: 0x600e28, Paddr: 0x600e28, Filesz: 0x1d0, Memsz: 0x1d0, Align: 8}, |
| 165 | } |
| 166 | smallBadBSSHeaders := []elf.ProgHeader{ |
| 167 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0x200000, Paddr: 0x200000, Filesz: 0x6fc, Memsz: 0x6fc, Align: 0x200000}, |
| 168 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x700, Vaddr: 0x400700, Paddr: 0x400700, Filesz: 0x500, Memsz: 0x710, Align: 0x200000}, |
| 169 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe10, Vaddr: 0x600e10, Paddr: 0x600e10, Filesz: 0x230, Memsz: 0x238, Align: 0x200000}, |
| 170 | } |
| 171 | mediumHeaders := []elf.ProgHeader{ |
| 172 | {Type: elf.PT_PHDR, Flags: elf.PF_R, Off: 0x40, Vaddr: 0x40, Paddr: 0x40, Filesz: 0x2d8, Memsz: 0x2d8, Align: 8}, |
| 173 | {Type: elf.PT_INTERP, Flags: elf.PF_R, Off: 0x318, Vaddr: 0x318, Paddr: 0x318, Filesz: 0x28, Memsz: 0x28, Align: 1}, |
| 174 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0x354ca0, Memsz: 0x354ca0, Align: 0x200000}, |
| 175 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x400000, Vaddr: 600000, Paddr: 600000, Filesz: 0x0083b8, Memsz: 0x009000, Align: 0x200000}, |
| 176 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x600000, Vaddr: 0xa00000, Paddr: 0xa00000, Filesz: 0x007030, Memsz: 0x9cf090, Align: 0x200000}, |
| 177 | {Type: elf.PT_LOAD, Flags: elf.PF_R, Off: 0x7cf090, Vaddr: 0x15cf090, Paddr: 0x15cf090, Filesz: 0x028460, Memsz: 0x028460, Align: 0x200000}, |
| 178 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x7f7500, Vaddr: 0x17f7500, Paddr: 0x17f7500, Filesz: 0x001f80, Memsz: 0x5bdd9a0, Align: 0x200000}, |
| 179 | {Type: elf.PT_TLS, Flags: elf.PF_R, Off: 0x407740, Vaddr: 0x607740, Paddr: 0x607740, Filesz: 0x98, Memsz: 0x350, Align: 0x40}, |
| 180 | {Type: elf.PT_DYNAMIC, Flags: elf.PF_R | elf.PF_W, Off: 0x407920, Vaddr: 0x607920, Paddr: 0x607920, Filesz: 0x230, Memsz: 0x230, Align: 8}, |
| 181 | } |
| 182 | largeHeaders := []elf.ProgHeader{ |
| 183 | {Type: elf.PT_PHDR, Flags: elf.PF_R, Off: 0x40, Vaddr: 0x40, Paddr: 0x40, Filesz: 0x268, Memsz: 0x268, Align: 8}, |
| 184 | {Type: elf.PT_INTERP, Flags: elf.PF_R, Off: 0x2a8, Vaddr: 0x2a8, Paddr: 0x2a8, Filesz: 0x28, Memsz: 0x28, Align: 1}, |
| 185 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0x2ec5d2c0, Memsz: 0x2ec5d2c0, Align: 0x200000}, |
| 186 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x2ec5d2c0, Vaddr: 0x2ee5d2c0, Paddr: 0x2ee5d2c0, Filesz: 0x1361118, Memsz: 0x1361150, Align: 0x200000}, |
| 187 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x2ffbe440, Vaddr: 0x303be440, Paddr: 0x303be440, Filesz: 0x4637c0, Memsz: 0xc91610, Align: 0x200000}, |
| 188 | {Type: elf.PT_TLS, Flags: elf.PF_R, Off: 0x2ec5d2c0, Vaddr: 0x2ee5d2c0, Paddr: 0x2ee5d2c0, Filesz: 0x120, Memsz: 0x103f8, Align: 0x40}, |
| 189 | {Type: elf.PT_DYNAMIC, Flags: elf.PF_R | elf.PF_W, Off: 0x2ffbc9e0, Vaddr: 0x301bc9e0, Paddr: 0x301bc9e0, Filesz: 0x1f0, Memsz: 0x1f0, Align: 8}, |
| 190 | } |
| 191 | largeHeadersWithRoSegment := []elf.ProgHeader{ |
| 192 | {Type: elf.PT_PHDR, Flags: elf.PF_R, Off: 0x40, Vaddr: 0x40, Paddr: 0x40, Filesz: 0x348, Memsz: 0x348, Align: 8}, |
| 193 | {Type: elf.PT_INTERP, Flags: elf.PF_R, Off: 0x388, Vaddr: 0x388, Paddr: 0x388, Filesz: 0x28, Memsz: 0x28, Align: 1}, |
| 194 | {Type: elf.PT_LOAD, Flags: elf.PF_R, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0x81c5628, Memsz: 0x81c5628, Align: 0x200000}, |
| 195 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x81c6000, Vaddr: 0x83c6000, Paddr: 0x83c6000, Filesz: 0x1af7fb0, Memsz: 0x1af7fb0, Align: 0x200000}, |
| 196 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0x9cbdfc0, Vaddr: 0xa0bdfc0, Paddr: 0xa0bdfc0, Filesz: 0x16e68fc0, Memsz: 0x16e68fc0, Align: 0x200000}, |
| 197 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x20c00000, Vaddr: 0x21200000, Paddr: 0x21200000, Filesz: 0x1c602f8, Memsz: 0x1c61000, Align: 0x200000}, |
| 198 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x22a00000, Vaddr: 0x23200000, Paddr: 0x23200000, Filesz: 0x120ed30, Memsz: 0x1d2c2e0, Align: 0x200000}, |
| 199 | {Type: elf.PT_LOAD, Flags: elf.PF_R, Off: 0x23d2c2e0, Vaddr: 0x2512c2e0, Paddr: 0x2512c2e0, Filesz: 0x2cbc4dc, Memsz: 0x2cbc4dc, Align: 0x200000}, |
| 200 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x269e87c0, Vaddr: 0x27fe87c0, Paddr: 0x27fe87c0, Filesz: 0x6f43c8, Memsz: 0x736e00, Align: 0x200000}, |
| 201 | {Type: elf.PT_TLS, Flags: elf.PF_R, Off: 0x22840200, Vaddr: 0x22e40200, Paddr: 0x22e40200, Filesz: 0x298, Memsz: 0x5428, Align: 0x40}, |
| 202 | {Type: elf.PT_DYNAMIC, Flags: elf.PF_R | elf.PF_W, Off: 0x2285daa0, Vaddr: 0x22e5daa0, Paddr: 0x22e5daa0, Filesz: 0x250, Memsz: 0x250, Align: 8}, |
| 203 | } |
| 204 | ffmpegHeaders := []elf.ProgHeader{ |
| 205 | {Type: elf.PT_PHDR, Flags: elf.PF_R, Off: 0x40, Vaddr: 0x200040, Paddr: 0x200040, Filesz: 0x1f8, Memsz: 0x1f8, Align: 8}, |
| 206 | {Type: elf.PT_INTERP, Flags: elf.PF_R, Off: 0x238, Vaddr: 0x200238, Paddr: 0x200238, Filesz: 0x28, Memsz: 0x28, Align: 1}, |
| 207 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0x200000, Paddr: 0x200000, Filesz: 0x48d8410, Memsz: 0x48d8410, Align: 0x200000}, |
| 208 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x48d8440, Vaddr: 0x4cd8440, Paddr: 0x4cd8440, Filesz: 0x18cbe0, Memsz: 0xd2fb70, Align: 0x200000}, |
| 209 | {Type: elf.PT_TLS, Flags: elf.PF_R, Off: 0x48d8440, Vaddr: 0x4cd8440, Paddr: 0x4cd8440, Filesz: 0xa8, Memsz: 0x468, Align: 0x40}, |
| 210 | {Type: elf.PT_DYNAMIC, Flags: elf.PF_R | elf.PF_W, Off: 0x4a63ad0, Vaddr: 0x4e63ad0, Paddr: 0x4e63ad0, Filesz: 0x200, Memsz: 0x200, Align: 8}, |
| 211 | } |
| 212 | sentryHeaders := []elf.ProgHeader{ |
| 213 | {Type: elf.PT_LOAD, Flags: elf.PF_X + elf.PF_R, Off: 0x0, Vaddr: 0x7f0000000000, Paddr: 0x7f0000000000, Filesz: 0xbc64d5, Memsz: 0xbc64d5, Align: 0x1000}, |
| 214 | {Type: elf.PT_LOAD, Flags: elf.PF_R, Off: 0xbc7000, Vaddr: 0x7f0000bc7000, Paddr: 0x7f0000bc7000, Filesz: 0xcd6b30, Memsz: 0xcd6b30, Align: 0x1000}, |
| 215 | {Type: elf.PT_LOAD, Flags: elf.PF_W + elf.PF_R, Off: 0x189e000, Vaddr: 0x7f000189e000, Paddr: 0x7f000189e000, Filesz: 0x58180, Memsz: 0x92d10, Align: 0x1000}, |
| 216 | } |
| 217 | |
| 218 | for _, tc := range []struct { |
| 219 | desc string |
| 220 | phdrs []elf.ProgHeader |
| 221 | pgoff uint64 |
| 222 | memsz uint64 |
| 223 | wantHeaders []*elf.ProgHeader |
| 224 | }{ |
| 225 | { |
| 226 | desc: "no prog headers", |
| 227 | phdrs: nil, |
| 228 | pgoff: 0, |
| 229 | memsz: 0x1000, |
| 230 | wantHeaders: nil, |
| 231 | }, |
| 232 | { |
| 233 | desc: "tiny file, 4KB at offset 0 matches both headers, b/178747588", |
| 234 | phdrs: tinyHeaders, |
| 235 | pgoff: 0, |
| 236 | memsz: 0x1000, |
| 237 | wantHeaders: []*elf.ProgHeader{ |
| 238 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 239 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}, |
| 240 | }, |
| 241 | }, |
| 242 | { |
| 243 | desc: "tiny file, file offset 4KB matches no headers", |
| 244 | phdrs: tinyHeaders, |
| 245 | pgoff: 0x1000, |
| 246 | memsz: 0x1000, |
| 247 | wantHeaders: nil, |
| 248 | }, |
| 249 | { |
| 250 | desc: "tiny file with unaligned memsz matches executable segment", |
| 251 | phdrs: tinyHeaders, |
| 252 | pgoff: 0, |
| 253 | memsz: 0xc80, |
| 254 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}}, |
| 255 | }, |
| 256 | { |
| 257 | desc: "tiny file with unaligned offset matches data segment", |
| 258 | phdrs: tinyHeaders, |
| 259 | pgoff: 0xc80, |
| 260 | memsz: 0x1000, |
| 261 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}}, |
| 262 | }, |
| 263 | { |
| 264 | desc: "tiny bad BSS file, 4KB at offset 0 matches all three headers", |
| 265 | phdrs: tinyBadBSSHeaders, |
| 266 | pgoff: 0, |
| 267 | memsz: 0x1000, |
| 268 | wantHeaders: []*elf.ProgHeader{ |
| 269 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 270 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x100, Memsz: 0x1f0, Align: 0x200000}, |
| 271 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xd80, Vaddr: 0x400d80, Paddr: 0x400d80, Filesz: 0x90, Memsz: 0x90, Align: 0x200000}, |
| 272 | }, |
| 273 | }, |
| 274 | { |
| 275 | desc: "small file, offset 0, memsz 4KB matches both segments", |
| 276 | phdrs: smallHeaders, |
| 277 | pgoff: 0, |
| 278 | memsz: 0x1000, |
| 279 | wantHeaders: []*elf.ProgHeader{ |
| 280 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0x400000, Paddr: 0x400000, Filesz: 0x6fc, Memsz: 0x6fc, Align: 0x200000}, |
| 281 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe10, Vaddr: 0x600e10, Paddr: 0x600e10, Filesz: 0x230, Memsz: 0x238, Align: 0x200000}, |
| 282 | }, |
| 283 | }, |
| 284 | { |
| 285 | desc: "small file, offset 0, memsz 8KB matches both segments", |
| 286 | phdrs: smallHeaders, |
| 287 | pgoff: 0, |
| 288 | memsz: 0x2000, |
| 289 | wantHeaders: []*elf.ProgHeader{ |
| 290 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0x400000, Paddr: 0x400000, Filesz: 0x6fc, Memsz: 0x6fc, Align: 0x200000}, |
| 291 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe10, Vaddr: 0x600e10, Paddr: 0x600e10, Filesz: 0x230, Memsz: 0x238, Align: 0x200000}, |
| 292 | }, |
| 293 | }, |
| 294 | { |
| 295 | desc: "small file, offset 4KB matches data segment", |
| 296 | phdrs: smallHeaders, |
| 297 | pgoff: 0x1000, |
| 298 | memsz: 0x1000, |
| 299 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe10, Vaddr: 0x600e10, Paddr: 0x600e10, Filesz: 0x230, Memsz: 0x238, Align: 0x200000}}, |
| 300 | }, |
| 301 | { |
| 302 | desc: "small file, offset 8KB matches no segment", |
| 303 | phdrs: smallHeaders, |
| 304 | pgoff: 0x2000, |
| 305 | memsz: 0x1000, |
| 306 | wantHeaders: nil, |
| 307 | }, |
| 308 | { |
| 309 | desc: "small bad BSS file, offset 0, memsz 4KB matches all three segments", |
| 310 | phdrs: smallBadBSSHeaders, |
| 311 | pgoff: 0, |
| 312 | memsz: 0x1000, |
| 313 | wantHeaders: []*elf.ProgHeader{ |
| 314 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0x200000, Paddr: 0x200000, Filesz: 0x6fc, Memsz: 0x6fc, Align: 0x200000}, |
| 315 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x700, Vaddr: 0x400700, Paddr: 0x400700, Filesz: 0x500, Memsz: 0x710, Align: 0x200000}, |
| 316 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe10, Vaddr: 0x600e10, Paddr: 0x600e10, Filesz: 0x230, Memsz: 0x238, Align: 0x200000}, |
| 317 | }, |
| 318 | }, |
| 319 | { |
| 320 | desc: "small bad BSS file, offset 0, memsz 8KB matches all three segments", |
| 321 | phdrs: smallBadBSSHeaders, |
| 322 | pgoff: 0, |
| 323 | memsz: 0x2000, |
| 324 | wantHeaders: []*elf.ProgHeader{ |
| 325 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0x200000, Paddr: 0x200000, Filesz: 0x6fc, Memsz: 0x6fc, Align: 0x200000}, |
| 326 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x700, Vaddr: 0x400700, Paddr: 0x400700, Filesz: 0x500, Memsz: 0x710, Align: 0x200000}, |
| 327 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe10, Vaddr: 0x600e10, Paddr: 0x600e10, Filesz: 0x230, Memsz: 0x238, Align: 0x200000}, |
| 328 | }, |
| 329 | }, |
| 330 | { |
| 331 | desc: "small bad BSS file, offset 4KB matches second data segment", |
| 332 | phdrs: smallBadBSSHeaders, |
| 333 | pgoff: 0x1000, |
| 334 | memsz: 0x1000, |
| 335 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe10, Vaddr: 0x600e10, Paddr: 0x600e10, Filesz: 0x230, Memsz: 0x238, Align: 0x200000}}, |
| 336 | }, |
| 337 | { |
| 338 | desc: "medium file large mapping that includes all address space matches executable segment, b/179920361", |
| 339 | phdrs: mediumHeaders, |
| 340 | pgoff: 0, |
| 341 | memsz: 0x73d5000, |
| 342 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0x354ca0, Memsz: 0x354ca0, Align: 0x200000}}, |
| 343 | }, |
| 344 | { |
| 345 | desc: "large file executable mapping matches executable segment", |
| 346 | phdrs: largeHeaders, |
| 347 | pgoff: 0, |
| 348 | memsz: 0x2ec5e000, |
| 349 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0x2ec5d2c0, Memsz: 0x2ec5d2c0, Align: 0x200000}}, |
| 350 | }, |
| 351 | { |
| 352 | desc: "large file first data mapping matches first data segment", |
| 353 | phdrs: largeHeaders, |
| 354 | pgoff: 0x2ec5d000, |
| 355 | memsz: 0x1362000, |
| 356 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x2ec5d2c0, Vaddr: 0x2ee5d2c0, Paddr: 0x2ee5d2c0, Filesz: 0x1361118, Memsz: 0x1361150, Align: 0x200000}}, |
| 357 | }, |
| 358 | { |
| 359 | desc: "large file, split second data mapping matches second data segment", |
| 360 | phdrs: largeHeaders, |
| 361 | pgoff: 0x2ffbe000, |
| 362 | memsz: 0xb11000, |
| 363 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x2ffbe440, Vaddr: 0x303be440, Paddr: 0x303be440, Filesz: 0x4637c0, Memsz: 0xc91610, Align: 0x200000}}, |
| 364 | }, |
| 365 | { |
| 366 | desc: "large file with RO segment executable mapping includes executable segment", |
| 367 | phdrs: largeHeadersWithRoSegment, |
| 368 | pgoff: 0x9c00000, |
| 369 | memsz: 0x16f27000, |
| 370 | wantHeaders: []*elf.ProgHeader{ |
| 371 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0x81c6000, Vaddr: 0x83c6000, Paddr: 0x83c6000, Filesz: 0x1af7fb0, Memsz: 0x1af7fb0, Align: 0x200000}, |
| 372 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0x9cbdfc0, Vaddr: 0xa0bdfc0, Paddr: 0xa0bdfc0, Filesz: 0x16e68fc0, Memsz: 0x16e68fc0, Align: 0x200000}, |
| 373 | }, |
| 374 | }, |
| 375 | { |
| 376 | desc: "sentry headers, mapping for last page of executable segment matches executable segment", |
| 377 | phdrs: sentryHeaders, |
| 378 | pgoff: 0xbc6000, |
| 379 | memsz: 0x1000, |
| 380 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_X + elf.PF_R, Off: 0x0, Vaddr: 0x7f0000000000, Paddr: 0x7f0000000000, Filesz: 0xbc64d5, Memsz: 0xbc64d5, Align: 0x1000}}, |
| 381 | }, |
| 382 | { |
| 383 | desc: "ffmpeg headers, split mapping for executable segment matches executable segment, b/193176694", |
| 384 | phdrs: ffmpegHeaders, |
| 385 | pgoff: 0, |
| 386 | memsz: 0x48d8000, |
| 387 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0x200000, Paddr: 0x200000, Filesz: 0x48d8410, Memsz: 0x48d8410, Align: 0x200000}}, |
| 388 | }, |
| 389 | { |
| 390 | desc: "segments with no file bits (b/195427553), mapping for executable segment matches executable segment", |
| 391 | phdrs: []elf.ProgHeader{ |
| 392 | {Type: elf.PT_LOAD, Flags: elf.PF_R, Off: 0x0, Vaddr: 0x0, Paddr: 0x0, Filesz: 0x115000, Memsz: 0x115000, Align: 0x1000}, |
| 393 | {Type: elf.PT_LOAD, Flags: elf.PF_X + elf.PF_R, Off: 0x115000, Vaddr: 0x115000, Paddr: 0x115000, Filesz: 0x361e15, Memsz: 0x361e15, Align: 0x1000}, |
| 394 | {Type: elf.PT_LOAD, Flags: elf.PF_W + elf.PF_R, Off: 0x0, Vaddr: 0x477000, Paddr: 0x477000, Filesz: 0x0, Memsz: 0x33c, Align: 0x1000}, |
| 395 | {Type: elf.PT_LOAD, Flags: elf.PF_R, Off: 0x0, Vaddr: 0x478000, Paddr: 0x478000, Filesz: 0x0, Memsz: 0x47dc28, Align: 0x1000}, |
| 396 | {Type: elf.PT_LOAD, Flags: elf.PF_R, Off: 0x477000, Vaddr: 0x8f6000, Paddr: 0x8f6000, Filesz: 0x140, Memsz: 0x140, Align: 0x1000}, |
| 397 | {Type: elf.PT_LOAD, Flags: elf.PF_W + elf.PF_R, Off: 0x478000, Vaddr: 0x8f7000, Paddr: 0x8f7000, Filesz: 0x38, Memsz: 0x38, Align: 0x1000}, |
| 398 | }, |
| 399 | pgoff: 0x115000, |
| 400 | memsz: 0x362000, |
| 401 | wantHeaders: []*elf.ProgHeader{{Type: elf.PT_LOAD, Flags: elf.PF_X + elf.PF_R, Off: 0x115000, Vaddr: 0x115000, Paddr: 0x115000, Filesz: 0x361e15, Memsz: 0x361e15, Align: 0x1000}}, |
| 402 | }, |
| 403 | } { |
| 404 | t.Run(tc.desc, func(t *testing.T) { |
| 405 | gotHeaders := ProgramHeadersForMapping(tc.phdrs, tc.pgoff, tc.memsz) |
| 406 | if !reflect.DeepEqual(gotHeaders, tc.wantHeaders) { |
| 407 | t.Errorf("got program headers %q; want %q", buildList(gotHeaders), buildList(tc.wantHeaders)) |
| 408 | } |
| 409 | }) |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | func TestHeaderForFileOffset(t *testing.T) { |
| 414 | for _, tc := range []struct { |
| 415 | desc string |
| 416 | headers []*elf.ProgHeader |
| 417 | fileOffset uint64 |
| 418 | wantError bool |
| 419 | want *elf.ProgHeader |
| 420 | }{ |
| 421 | { |
| 422 | desc: "no headers, want error", |
| 423 | headers: nil, |
| 424 | wantError: true, |
| 425 | }, |
| 426 | { |
| 427 | desc: "three headers, BSS in last segment, file offset selects first header", |
| 428 | headers: []*elf.ProgHeader{ |
| 429 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 430 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}, |
| 431 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe70, Vaddr: 0x400e70, Paddr: 0x400e70, Filesz: 0x90, Memsz: 0x100, Align: 0x200000}, |
| 432 | }, |
| 433 | fileOffset: 0xc79, |
| 434 | want: &elf.ProgHeader{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 435 | }, |
| 436 | { |
| 437 | desc: "three headers, BSS in last segment, file offset selects second header", |
| 438 | headers: []*elf.ProgHeader{ |
| 439 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 440 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}, |
| 441 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe70, Vaddr: 0x400e70, Paddr: 0x400e70, Filesz: 0x90, Memsz: 0x100, Align: 0x200000}, |
| 442 | }, |
| 443 | fileOffset: 0xc80, |
| 444 | want: &elf.ProgHeader{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}, |
| 445 | }, |
| 446 | { |
| 447 | desc: "three headers, BSS in last segment, file offset selects third header", |
| 448 | headers: []*elf.ProgHeader{ |
| 449 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 450 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}, |
| 451 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe70, Vaddr: 0x400e70, Paddr: 0x400e70, Filesz: 0x90, Memsz: 0x100, Align: 0x200000}, |
| 452 | }, |
| 453 | fileOffset: 0xef0, |
| 454 | want: &elf.ProgHeader{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe70, Vaddr: 0x400e70, Paddr: 0x400e70, Filesz: 0x90, Memsz: 0x100, Align: 0x200000}, |
| 455 | }, |
| 456 | { |
| 457 | desc: "three headers, BSS in last segment, file offset in uninitialized section selects third header", |
| 458 | headers: []*elf.ProgHeader{ |
| 459 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 460 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}, |
| 461 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe70, Vaddr: 0x400e70, Paddr: 0x400e70, Filesz: 0x90, Memsz: 0x100, Align: 0x200000}, |
| 462 | }, |
| 463 | fileOffset: 0xf40, |
| 464 | want: &elf.ProgHeader{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe70, Vaddr: 0x400e70, Paddr: 0x400e70, Filesz: 0x90, Memsz: 0x100, Align: 0x200000}, |
| 465 | }, |
| 466 | { |
| 467 | desc: "three headers, BSS in last segment, file offset past any segment gives error", |
| 468 | headers: []*elf.ProgHeader{ |
| 469 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 470 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x1f0, Memsz: 0x1f0, Align: 0x200000}, |
| 471 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xe70, Vaddr: 0x400e70, Paddr: 0x400e70, Filesz: 0x90, Memsz: 0x100, Align: 0x200000}, |
| 472 | }, |
| 473 | fileOffset: 0xf70, |
| 474 | wantError: true, |
| 475 | }, |
| 476 | { |
| 477 | desc: "three headers, BSS in second segment, file offset in mapped section selects second header", |
| 478 | headers: []*elf.ProgHeader{ |
| 479 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 480 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x100, Memsz: 0x1f0, Align: 0x200000}, |
| 481 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xd80, Vaddr: 0x400d80, Paddr: 0x400d80, Filesz: 0x100, Memsz: 0x100, Align: 0x200000}, |
| 482 | }, |
| 483 | fileOffset: 0xd79, |
| 484 | want: &elf.ProgHeader{Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x100, Memsz: 0x1f0, Align: 0x200000}, |
| 485 | }, |
| 486 | { |
| 487 | desc: "three headers, BSS in second segment, file offset in unmapped section gives error", |
| 488 | headers: []*elf.ProgHeader{ |
| 489 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_X, Off: 0, Vaddr: 0, Paddr: 0, Filesz: 0xc80, Memsz: 0xc80, Align: 0x200000}, |
| 490 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xc80, Vaddr: 0x200c80, Paddr: 0x200c80, Filesz: 0x100, Memsz: 0x1f0, Align: 0x200000}, |
| 491 | {Type: elf.PT_LOAD, Flags: elf.PF_R | elf.PF_W, Off: 0xd80, Vaddr: 0x400d80, Paddr: 0x400d80, Filesz: 0x100, Memsz: 0x100, Align: 0x200000}, |
| 492 | }, |
| 493 | fileOffset: 0xd80, |
| 494 | wantError: true, |
| 495 | }, |
| 496 | } { |
| 497 | t.Run(tc.desc, func(t *testing.T) { |
| 498 | got, err := HeaderForFileOffset(tc.headers, tc.fileOffset) |
| 499 | if (err != nil) != tc.wantError { |
| 500 | t.Errorf("got error %v, want any error=%v", err, tc.wantError) |
| 501 | } |
| 502 | if err != nil { |
| 503 | return |
| 504 | } |
| 505 | if !reflect.DeepEqual(got, tc.want) { |
| 506 | t.Errorf("got program header %#v, want %#v", got, tc.want) |
| 507 | } |
| 508 | }) |
| 509 | } |
| 510 | } |