| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import os |
| 5 |
import struct |
| 6 |
from typing import List, NamedTuple, Optional, Sequence, Tuple, Union |
| 7 |
import zlib |
| 8 |
|
| 9 |
from _drgn_util.elf import ET, PT, SHF, SHN, SHT, STB, STT, STV |
| 10 |
|
| 11 |
|
| 12 |
class ElfSection: |
| 13 |
def __init__( |
| 14 |
self, |
| 15 |
*, |
| 16 |
data: bytes = b"", |
| 17 |
name: Optional[str] = None, |
| 18 |
sh_type: Optional[SHT] = None, |
| 19 |
p_type: Optional[PT] = None, |
| 20 |
vaddr: int = 0, |
| 21 |
paddr: int = 0, |
| 22 |
memsz: Optional[int] = None, |
| 23 |
p_align: int = 0, |
| 24 |
sh_link: int = 0, |
| 25 |
sh_info: int = 0, |
| 26 |
sh_entsize: int = 0, |
| 27 |
sh_flags: SHF = SHF(0), |
| 28 |
): |
| 29 |
self.data = data |
| 30 |
self.name = name |
| 31 |
self.sh_type = sh_type |
| 32 |
self.sh_flags = sh_flags |
| 33 |
self.p_type = p_type |
| 34 |
self.vaddr = vaddr |
| 35 |
self.paddr = paddr |
| 36 |
self.memsz = memsz |
| 37 |
self.p_align = p_align |
| 38 |
self.sh_link = sh_link |
| 39 |
self.sh_info = sh_info |
| 40 |
self.sh_entsize = sh_entsize |
| 41 |
|
| 42 |
assert (self.name is not None) or (self.p_type is not None) |
| 43 |
assert (self.name is None) == (self.sh_type is None) |
| 44 |
assert self.p_type is None or not (sh_flags & SHF.COMPRESSED) |
| 45 |
|
| 46 |
|
| 47 |
class ElfSymbol(NamedTuple): |
| 48 |
name: str |
| 49 |
value: int |
| 50 |
size: int |
| 51 |
type: STT |
| 52 |
binding: STB |
| 53 |
shindex: Optional[int] = None |
| 54 |
visibility: STV = STV.DEFAULT |
| 55 |
|
| 56 |
def st_info(self) -> int: |
| 57 |
return (self.binding << 4) + (self.type & 0xF) |
| 58 |
|
| 59 |
|
| 60 |
def _create_symtab( |
| 61 |
sections: List[ElfSection], |
| 62 |
symbols: Sequence[ElfSymbol], |
| 63 |
*, |
| 64 |
dynamic: bool = False, |
| 65 |
little_endian: bool, |
| 66 |
bits: int, |
| 67 |
): |
| 68 |
symtab_name = ".dynsym" if dynamic else ".symtab" |
| 69 |
strtab_name = ".dynstr" if dynamic else ".strtab" |
| 70 |
assert not any(section.name in (symtab_name, strtab_name) for section in sections) |
| 71 |
|
| 72 |
# An empty symbol name is a placeholder for the implicit 0-index entry in |
| 73 |
# the symbol table. It's used to create a valid, but empty symbol table. |
| 74 |
if symbols and symbols[0].name == "": |
| 75 |
symbols = symbols[1:] |
| 76 |
|
| 77 |
endian = "<" if little_endian else ">" |
| 78 |
if bits == 64: |
| 79 |
symbol_struct = struct.Struct(endian + "IBBHQQ") |
| 80 |
|
| 81 |
def symbol_fields(sym: ElfSymbol): |
| 82 |
return ( |
| 83 |
sym.st_info(), |
| 84 |
sym.visibility, |
| 85 |
SHN.UNDEF if sym.shindex is None else sym.shindex, |
| 86 |
sym.value, |
| 87 |
sym.size, |
| 88 |
) |
| 89 |
|
| 90 |
else: |
| 91 |
symbol_struct = struct.Struct(endian + "IIIBBH") |
| 92 |
|
| 93 |
def symbol_fields(sym: ElfSymbol): |
| 94 |
return ( |
| 95 |
sym.value, |
| 96 |
sym.size, |
| 97 |
sym.st_info(), |
| 98 |
sym.visibility, |
| 99 |
SHN.UNDEF if sym.shindex is None else sym.shindex, |
| 100 |
) |
| 101 |
|
| 102 |
symtab_data = bytearray((len(symbols) + 1) * symbol_struct.size) |
| 103 |
strtab_data = bytearray(1) |
| 104 |
sh_info = 1 |
| 105 |
for i, sym in enumerate(symbols, 1): |
| 106 |
symbol_struct.pack_into( |
| 107 |
symtab_data, i * symbol_struct.size, len(strtab_data), *symbol_fields(sym) |
| 108 |
) |
| 109 |
strtab_data.extend(sym.name.encode()) |
| 110 |
strtab_data.append(0) |
| 111 |
if sym.binding == STB.LOCAL: |
| 112 |
assert sh_info == i, "local symbol after non-local symbol" |
| 113 |
sh_info = i + 1 |
| 114 |
|
| 115 |
sections.append( |
| 116 |
ElfSection( |
| 117 |
name=symtab_name, |
| 118 |
sh_type=SHT.DYNSYM if dynamic else SHT.SYMTAB, |
| 119 |
data=symtab_data, |
| 120 |
sh_link=sum((1 for section in sections if section.name is not None), 2), |
| 121 |
sh_info=sh_info, |
| 122 |
sh_entsize=symbol_struct.size, |
| 123 |
) |
| 124 |
) |
| 125 |
sections.append(ElfSection(name=strtab_name, sh_type=SHT.STRTAB, data=strtab_data)) |
| 126 |
|
| 127 |
|
| 128 |
def create_elf_file( |
| 129 |
type: ET, |
| 130 |
sections: Sequence[ElfSection] = (), |
| 131 |
symbols: Sequence[ElfSymbol] = (), |
| 132 |
*, |
| 133 |
dynamic_symbols: Sequence[ElfSymbol] = (), |
| 134 |
build_id: Optional[bytes] = None, |
| 135 |
gnu_debuglink: Optional[ |
| 136 |
Tuple[Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"], int] |
| 137 |
] = None, |
| 138 |
gnu_debugaltlink: Optional[ |
| 139 |
Tuple[Union[str, bytes, "os.PathLike[str]", "os.PathLike[bytes]"], bytes] |
| 140 |
] = None, |
| 141 |
little_endian: bool = True, |
| 142 |
bits: int = 64, |
| 143 |
): |
| 144 |
endian = "<" if little_endian else ">" |
| 145 |
if bits == 64: |
| 146 |
ehdr_struct = struct.Struct(endian + "16BHHIQQQIHHHHHH") |
| 147 |
shdr_struct = struct.Struct(endian + "IIQQQQIIQQ") |
| 148 |
phdr_struct = struct.Struct(endian + "IIQQQQQQ") |
| 149 |
chdr_struct = struct.Struct(endian + "IIQQ") |
| 150 |
e_machine = 62 if little_endian else 43 # EM_X86_64 or EM_SPARCV9 |
| 151 |
else: |
| 152 |
assert bits == 32 |
| 153 |
ehdr_struct = struct.Struct(endian + "16BHHIIIIIHHHHHH") |
| 154 |
shdr_struct = struct.Struct(endian + "10I") |
| 155 |
phdr_struct = struct.Struct(endian + "8I") |
| 156 |
chdr_struct = struct.Struct(endian + "III") |
| 157 |
e_machine = 3 if little_endian else 8 # EM_386 or EM_MIPS |
| 158 |
nhdr_struct = struct.Struct(endian + "3I") |
| 159 |
|
| 160 |
sections = list(sections) |
| 161 |
if dynamic_symbols: |
| 162 |
_create_symtab( |
| 163 |
sections, |
| 164 |
dynamic_symbols, |
| 165 |
dynamic=True, |
| 166 |
little_endian=little_endian, |
| 167 |
bits=bits, |
| 168 |
) |
| 169 |
if symbols: |
| 170 |
_create_symtab(sections, symbols, little_endian=little_endian, bits=bits) |
| 171 |
if build_id is not None: |
| 172 |
build_id_note = ( |
| 173 |
nhdr_struct.pack( |
| 174 |
4, # n_namesz, |
| 175 |
len(build_id), # n_namesz, |
| 176 |
3, # n_type = NT_GNU_BUILD_ID |
| 177 |
) |
| 178 |
+ b"GNU\0" |
| 179 |
+ build_id |
| 180 |
+ bytes(-len(build_id) % 4) |
| 181 |
) |
| 182 |
sections.append( |
| 183 |
ElfSection(name=".note.gnu.build-id", sh_type=SHT.NOTE, data=build_id_note) |
| 184 |
) |
| 185 |
|
| 186 |
if gnu_debuglink is not None: |
| 187 |
gnu_debuglink_path, gnu_debuglink_crc = gnu_debuglink |
| 188 |
gnu_debuglink_path = os.fsencode(gnu_debuglink_path) |
| 189 |
sections.append( |
| 190 |
ElfSection( |
| 191 |
name=".gnu_debuglink", |
| 192 |
sh_type=SHT.PROGBITS, |
| 193 |
data=gnu_debuglink_path |
| 194 |
+ bytes(4 - len(gnu_debuglink_path) % 4) |
| 195 |
+ gnu_debuglink_crc.to_bytes(4, "little"), |
| 196 |
) |
| 197 |
) |
| 198 |
|
| 199 |
if gnu_debugaltlink is not None: |
| 200 |
gnu_debugaltlink_path, gnu_debugaltlink_build_id = gnu_debugaltlink |
| 201 |
sections.append( |
| 202 |
ElfSection( |
| 203 |
name=".gnu_debugaltlink", |
| 204 |
sh_type=SHT.PROGBITS, |
| 205 |
data=os.fsencode(gnu_debugaltlink_path) |
| 206 |
+ b"\0" |
| 207 |
+ gnu_debugaltlink_build_id, |
| 208 |
) |
| 209 |
) |
| 210 |
|
| 211 |
shnum = 0 |
| 212 |
phnum = 0 |
| 213 |
shstrtab = bytearray(1) |
| 214 |
for section in sections: |
| 215 |
if section.name is not None: |
| 216 |
shstrtab.extend(section.name.encode()) |
| 217 |
shstrtab.append(0) |
| 218 |
shnum += 1 |
| 219 |
if section.p_type is not None: |
| 220 |
phnum += 1 |
| 221 |
if shnum > 0: |
| 222 |
shnum += 2 # One for the SHT_NULL section, one for .shstrtab. |
| 223 |
shstrtab.extend(b".shstrtab\0") |
| 224 |
sections.append(ElfSection(name=".shstrtab", sh_type=SHT.STRTAB, data=shstrtab)) |
| 225 |
|
| 226 |
shdr_offset = ehdr_struct.size |
| 227 |
phdr_offset = shdr_offset + shdr_struct.size * shnum |
| 228 |
headers_size = phdr_offset + phdr_struct.size * phnum |
| 229 |
buf = bytearray(headers_size) |
| 230 |
ehdr_struct.pack_into( |
| 231 |
buf, |
| 232 |
0, |
| 233 |
0x7F, # ELFMAG0 |
| 234 |
ord("E"), # ELFMAG1 |
| 235 |
ord("L"), # ELFMAG2 |
| 236 |
ord("F"), # ELFMAG3 |
| 237 |
2 if bits == 64 else 1, # EI_CLASS = ELFCLASS64 or ELFCLASS32 |
| 238 |
1 if little_endian else 2, # EI_DATA = ELFDATA2LSB or ELFDATA2MSB |
| 239 |
1, # EI_VERSION = EV_CURRENT |
| 240 |
0, # EI_OSABI = ELFOSABI_NONE |
| 241 |
0, # EI_ABIVERSION |
| 242 |
0, |
| 243 |
0, |
| 244 |
0, |
| 245 |
0, |
| 246 |
0, |
| 247 |
0, |
| 248 |
0, # EI_PAD |
| 249 |
type, # e_type |
| 250 |
e_machine, |
| 251 |
1, # e_version = EV_CURRENT |
| 252 |
0, # e_entry |
| 253 |
phdr_offset if phnum else 0, # e_phoff |
| 254 |
shdr_offset if shnum else 0, # e_shoff |
| 255 |
0, # e_flags |
| 256 |
ehdr_struct.size, # e_ehsize |
| 257 |
phdr_struct.size, # e_phentsize |
| 258 |
phnum, # e_phnum |
| 259 |
shdr_struct.size, # e_shentsize |
| 260 |
shnum, # e_shnum |
| 261 |
shnum - 1 if shnum else 0, # e_shstrndx |
| 262 |
) |
| 263 |
|
| 264 |
shdr_offset += shdr_struct.size |
| 265 |
for section in sections: |
| 266 |
ch_addralign = 1 if section.p_type is None else bits // 8 |
| 267 |
memsz = len(section.data) if section.memsz is None else section.memsz |
| 268 |
if section.sh_flags & SHF.COMPRESSED: |
| 269 |
sh_addralign = bits // 8 |
| 270 |
compressed_data = zlib.compress(section.data) |
| 271 |
sh_size = chdr_struct.size + len(compressed_data) |
| 272 |
else: |
| 273 |
sh_addralign = ch_addralign |
| 274 |
sh_size = memsz |
| 275 |
if section.p_align: |
| 276 |
padding = section.vaddr % section.p_align - len(buf) % section.p_align |
| 277 |
buf.extend(bytes(padding)) |
| 278 |
if section.name is not None: |
| 279 |
shdr_struct.pack_into( |
| 280 |
buf, |
| 281 |
shdr_offset, |
| 282 |
shstrtab.index(section.name.encode()), # sh_name |
| 283 |
section.sh_type, # sh_type |
| 284 |
section.sh_flags, # sh_flags |
| 285 |
section.vaddr, # sh_addr |
| 286 |
len(buf), # sh_offset |
| 287 |
sh_size, # sh_size |
| 288 |
section.sh_link, # sh_link |
| 289 |
section.sh_info, # sh_info |
| 290 |
sh_addralign, # sh_addralign |
| 291 |
section.sh_entsize, # sh_entsize |
| 292 |
) |
| 293 |
shdr_offset += shdr_struct.size |
| 294 |
if section.p_type is not None: |
| 295 |
flags = 7 # PF_R | PF_W | PF_X |
| 296 |
if bits == 64: |
| 297 |
phdr_struct.pack_into( |
| 298 |
buf, |
| 299 |
phdr_offset, |
| 300 |
section.p_type, # p_type |
| 301 |
flags, # p_flags |
| 302 |
len(buf), # p_offset |
| 303 |
section.vaddr, # p_vaddr |
| 304 |
section.paddr, # p_paddr |
| 305 |
len(section.data), # p_filesz |
| 306 |
memsz, # p_memsz |
| 307 |
section.p_align, # p_align |
| 308 |
) |
| 309 |
else: |
| 310 |
phdr_struct.pack_into( |
| 311 |
buf, |
| 312 |
phdr_offset, |
| 313 |
section.p_type, # p_type |
| 314 |
len(buf), # p_offset |
| 315 |
section.vaddr, # p_vaddr |
| 316 |
section.paddr, # p_paddr |
| 317 |
len(section.data), # p_filesz |
| 318 |
memsz, # p_memsz |
| 319 |
flags, # p_flags |
| 320 |
section.p_align, # p_align |
| 321 |
) |
| 322 |
phdr_offset += phdr_struct.size |
| 323 |
if section.sh_flags & SHF.COMPRESSED: |
| 324 |
ELFCOMPRESS_ZLIB = 1 |
| 325 |
if bits == 64: |
| 326 |
buf.extend( |
| 327 |
chdr_struct.pack( |
| 328 |
ELFCOMPRESS_ZLIB, # ch_type |
| 329 |
0, # ch_reserved |
| 330 |
memsz, # ch_size |
| 331 |
ch_addralign, # ch_addralign |
| 332 |
) |
| 333 |
) |
| 334 |
else: |
| 335 |
buf.extend( |
| 336 |
chdr_struct.pack( |
| 337 |
ELFCOMPRESS_ZLIB, # ch_type |
| 338 |
memsz, # ch_size |
| 339 |
ch_addralign, # ch_addralign |
| 340 |
) |
| 341 |
) |
| 342 |
buf.extend(compressed_data) |
| 343 |
else: |
| 344 |
buf.extend(section.data) |
| 345 |
|
| 346 |
return buf |