File: elf.go

package info (click to toggle)
podman 5.4.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,124 kB
  • sloc: sh: 6,119; perl: 2,710; python: 2,258; ansic: 1,556; makefile: 1,022; xml: 121; ruby: 42; awk: 12; csh: 8
file content (220 lines) | stat: -rw-r--r-- 6,235 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//go:build !remote

package emulation

import (
	"debug/elf"
	"encoding/binary"
	"fmt"
	"sync"

	"github.com/sirupsen/logrus"
)

type elfPlatform struct {
	platform string
	osabi    []elf.OSABI
	class    elf.Class
	data     elf.Data
	alsoNone bool // also try with data=none,version=0
	machine  elf.Machine
	flags    []uint32
}

var (
	// knownELFPlatformHeaders is a mapping from target platform names and
	// plausible headers for the binaries built for those platforms.  Call
	// getKnownELFPlatformHeaders() instead of reading this map directly.
	knownELFPlatformHeaders     = make(map[string][][]byte)
	knownELFPlatformHeadersOnce sync.Once
	// knownELFPlatforms is a table of target platforms that we built a
	// trivial program for, and the other fields are filled in based on
	// what we got when we ran eu-readelf -h against the results.
	knownELFPlatforms = []elfPlatform{
		{
			platform: "linux/386",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS32,
			data:     elf.ELFDATA2LSB,
			alsoNone: true,
			machine:  elf.EM_386,
		},
		{
			platform: "linux/amd64",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2LSB,
			alsoNone: true,
			machine:  elf.EM_X86_64,
		},
		{
			platform: "linux/arm",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS32,
			data:     elf.ELFDATA2LSB,
			machine:  elf.EM_ARM,
		},
		{
			platform: "linux/arm64",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2LSB,
			machine:  elf.EM_AARCH64,
		},
		{
			platform: "linux/arm64be",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2MSB,
			machine:  elf.EM_AARCH64,
		},
		{
			platform: "linux/loong64",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2LSB,
			machine:  elf.EM_LOONGARCH,
		},
		{
			platform: "linux/mips",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS32,
			data:     elf.ELFDATA2MSB,
			machine:  elf.EM_MIPS,
			flags:    []uint32{0, 2}, // elf.EF_MIPS_PIC set, or not
		},
		{
			platform: "linux/mipsle",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS32,
			data:     elf.ELFDATA2LSB,
			machine:  elf.EM_MIPS_RS3_LE,
			flags:    []uint32{0, 2}, // elf.EF_MIPS_PIC set, or not
		},
		{
			platform: "linux/mips64",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2MSB,
			machine:  elf.EM_MIPS,
			flags:    []uint32{0, 2}, // elf.EF_MIPS_PIC set, or not
		},
		{
			platform: "linux/mips64le",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2LSB,
			machine:  elf.EM_MIPS_RS3_LE,
			flags:    []uint32{0, 2}, // elf.EF_MIPS_PIC set, or not
		},
		{
			platform: "linux/ppc",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS32,
			data:     elf.ELFDATA2MSB,
			machine:  elf.EM_PPC,
		},
		{
			platform: "linux/ppc64",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2MSB,
			machine:  elf.EM_PPC64,
		},
		{
			platform: "linux/ppc64le",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2LSB,
			machine:  elf.EM_PPC64,
		},
		{
			platform: "linux/riscv32",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS32,
			data:     elf.ELFDATA2LSB,
			machine:  elf.EM_RISCV,
		},
		{
			platform: "linux/riscv64",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2LSB,
			machine:  elf.EM_RISCV,
		},
		{
			platform: "linux/s390x",
			osabi:    []elf.OSABI{elf.ELFOSABI_NONE, elf.ELFOSABI_LINUX},
			class:    elf.ELFCLASS64,
			data:     elf.ELFDATA2MSB,
			machine:  elf.EM_S390,
		},
	}
)

// header generates an approximation of what the initial N bytes of a binary
// built for a given target looks like
func (e *elfPlatform) header() ([][]byte, error) {
	var headers [][]byte
	osabi := e.osabi
	if len(osabi) == 0 {
		osabi = []elf.OSABI{elf.ELFOSABI_NONE}
	}
	for i := range osabi {
		flags := e.flags
		if len(flags) == 0 {
			flags = []uint32{0}
		}
		for f := range flags {
			var endian binary.ByteOrder
			var entrySize, phoffSize, shoffSize int
			header := make([]byte, 40)
			copy(header, elf.ELFMAG)
			switch e.class {
			case elf.ELFCLASS32:
				entrySize, phoffSize, shoffSize = 2, 2, 2
			case elf.ELFCLASS64:
				entrySize, phoffSize, shoffSize = 4, 4, 4
			}
			switch e.data {
			case elf.ELFDATA2LSB:
				endian = binary.LittleEndian
			case elf.ELFDATA2MSB:
				endian = binary.BigEndian
			default:
				return nil, fmt.Errorf("internal error in entry for %q", e.platform)
			}
			header[elf.EI_OSABI] = byte(osabi[i])
			header[elf.EI_CLASS] = byte(e.class)
			header[elf.EI_DATA] = byte(e.data)
			header[elf.EI_VERSION] = byte(elf.EV_CURRENT)
			header[elf.EI_ABIVERSION] = 0
			endian.PutUint16(header[16:], uint16(elf.ET_EXEC))
			endian.PutUint16(header[18:], uint16(e.machine))
			endian.PutUint32(header[20:], uint32(elf.EV_CURRENT))
			endian.PutUint32(header[24+entrySize+phoffSize+shoffSize:], flags[f])
			headers = append(headers, append([]byte{}, header...))
			if e.alsoNone {
				header[elf.EI_DATA] = byte(elf.ELFDATANONE)
				header[elf.EI_VERSION] = byte(elf.EV_NONE)
				endian.PutUint32(header[20:], uint32(elf.EV_NONE))
				headers = append(headers, append([]byte{}, header...))
			}
		}
	}
	return headers, nil
}

func getKnownELFPlatformHeaders() map[string][][]byte {
	knownELFPlatformHeadersOnce.Do(func() {
		for _, p := range knownELFPlatforms {
			headerList, err := p.header()
			if err != nil {
				logrus.Errorf("generating headers for %q: %v\n", p.platform, err)
				continue
			}
			knownELFPlatformHeaders[p.platform] = headerList
		}
	})
	return knownELFPlatformHeaders
}