#!/usr/bin/env python3 """Lowers a shared library's glibc requirement for symbols lwe_glibc_compat.c provides. usage: retarget_glibc_versions.py ... Every GLIBC_x version a library needs that is newer than (e.g. 2.35) is rewritten in its version needs (.gnu.version_r) to GLIBC_2.2.5, which every x86_64 glibc defines, so the loader no longer refuses it on an older glibc. The symbols bound to that version then can't match libc's own versioned copies and bind to the next unversioned definition in the global scope, the compat library (add it as a dependency with patchelf). Refuses when an undefined symbol of such a version isn't in the provided list. Prints the libraries it changed. """ import struct import sys SHT_DYNSYM = 11 SHT_GNU_VERNEED = 0x6FFFFFFE SHT_GNU_VERSYM = 0x6FFFFFFF BASE_VERSION = b"GLIBC_2.2.5" def elf_hash(name): h = 0 for c in name: h = (h << 4) + c g = h & 0xF0000000 if g: h ^= g >> 24 h &= ~g & 0xFFFFFFFF return h def version_tuple(name): return tuple(int(p) for p in name[len("GLIBC_"):].split(".")) def cstr(data, offset): return data[offset:data.index(b"\0", offset)] def retarget(path, floor, provided): with open(path, "rb") as f: data = bytearray(f.read()) if data[:4] != b"\x7fELF" or data[4] != 2 or data[5] != 1: return False shoff, = struct.unpack_from(" floor: newer[vna_other] = (aux, name.decode()) aux += vna_next offset += vn_next if not newer: return False if base_name is None: raise SystemExit(f"{path}: needs {BASE_VERSION.decode()} for the rewrite but doesn't reference it") dynsym = next(s for s in sections if s["type"] == SHT_DYNSYM) versym = next(s for s in sections if s["type"] == SHT_GNU_VERSYM) symstr = sections[dynsym["link"]]["offset"] for i in range(dynsym["size"] // dynsym["entsize"]): st_name, st_info, st_other, st_shndx = struct.unpack_from("