#!/usr/bin/env python3 # # Copyright (c) 2012 Intel Corporation # # SPDX-License-Identifier: GPL-2.0-only # # AUTHORS # Laurentiu Palcu # # Copyright (C) 2019-2020, Xilinx, Inc. All rights reserved. # Copyright (C) 2023, Advanced Micro Devices, Inc. All rights reserved. # # DESCRIPTION # Runtime-relocation wrapper scripting based on YP relocation scripting # # AUTHORS # Mark Hatle import struct import sys import stat import os import re import errno if sys.version < '3': def b(x): return x else: def b(x): return x.encode(sys.getfilesystemencoding()) old_prefix = re.compile(b("##DEFAULT_INSTALL_DIR##")) def get_arch(): global endian_prefix f.seek(0) e_ident =f.read(16) ei_mag0,ei_mag1_3,ei_class,ei_data,ei_version = struct.unpack("' % sys.argv[0]) sys.exit(-1) # In python > 3, strings may also contain Unicode characters. So, convert # them to bytes if sys.version_info < (3,): process_path = sys.argv[1] else: process_path = sys.argv[1] process_path = os.path.realpath(process_path) for root, _, files in os.walk(process_path): for file in files: if file.endswith('.real'): continue e = os.path.join(root, file) if not os.path.isfile(e) or not os.access(e, os.X_OK) or os.path.islink(e): continue if os.path.dirname(e).endswith('/lib') and (os.path.basename(e).startswith('libc-') or os.path.basename(e).startswith('libc.so')): # Special case, don't wrap this... continue if os.path.dirname(e).endswith('/lib') and os.path.basename(e).startswith('libpthread-'): # Special case, don't wrap this... continue perms = os.stat(e)[stat.ST_MODE] if os.access(e, os.R_OK): perms = None else: os.chmod(e, perms|stat.S_IRWXU) try: f = open(e, "r+b") except IOError: exctype, ioex = sys.exc_info()[:2] if ioex.errno == errno.ETXTBSY: print("Could not open %s. File used by another process.\nPlease "\ "make sure you exit all processes that might use any SDK "\ "binaries." % e) else: print("Could not open %s: %s(%d)" % (e, ioex.strerror, ioex.errno)) sys.exit(-1) # Save old size and do a size check at the end. Just a safety measure. old_size = os.path.getsize(e) if old_size >= 64: arch = get_arch() if arch: parse_elf_header(f) if is_elf_executable(f): dirpath = os.path.dirname(e) destfile = os.path.join(dirpath, file + '.real') wrapper = os.path.join(dirpath, 'execwrapper.sh') if not os.path.exists(wrapper): #print('write %s' % wrapper) with open(wrapper, "w+") as wrapperf: ldso = os.path.basename(interp) libbasepath = os.path.dirname(interp) # should be /lib libdirname = os.path.basename(libbasepath) # lib or lib32 or lib64 or .... basepath = os.path.dirname(libbasepath) # should be / libpath = os.path.join(basepath, 'usr', libdirname) # Generate relative names to the path of the execwrapper libbasepath = os.path.relpath(libbasepath, dirpath) libpath = os.path.relpath(libpath, dirpath) print('') print('wrapper: %s' % wrapper) print('ldso = %s' % ldso) print('lib = %s' % libdirname) print('libpath = %s' % libbasepath) print('usrlibpath = %s' % libpath) print('') wrapperf.write('#!/bin/bash\n') wrapperf.write('# Written by Mark Hatle \n') wrapperf.write('# Copyright (C) 2019-2020, Xilinx, Inc. All rights reserved\n') wrapperf.write('# Copyright (C) 2023-2024, Advanced Micro Devices, Inc. All rights reserved\n') wrapperf.write('#\n') wrapperf.write('# SPDX-License-Identifier: GPL-2.0-only\n') wrapperf.write('LDSO=%s\n' % ldso) wrapperf.write('LIBBASEPATH=%s\n' % libbasepath) wrapperf.write('LIBPATH=%s\n' % libpath) wrapperf.write('executable=$(basename $0)\n') wrapperf.write('wrapper=$0\n') wrapperf.write('BASEPATH=$(dirname ${wrapper})\n') wrapperf.write('if [ ! -x $0 ]; then\n') wrapperf.write(' wrapper=$(which $0)\n') wrapperf.write('fi\n') wrapperf.write('if [ -h $0 ]; then\n') wrapperf.write(' executable=$(basename "$(readlink $0)" )\n') wrapperf.write(' BASEPATH=$(dirname "$(realpath $0)")\n') wrapperf.write('fi\n') wrapperf.write('LIBBASEPATH=$(realpath ${BASEPATH}/${LIBBASEPATH})\n') wrapperf.write('LIBPATH=$(realpath ${BASEPATH}/${LIBPATH})\n') wrapperf.write('export COLLECT_GCC=${COLLECT_GCC%%.real}\n') wrapperf.write('exec ${LIBBASEPATH}/${LDSO} --library-path ${LIBPATH}:${LIBBASEPATH} ${BASEPATH}/${executable}.real $@\n') #print('chmod %s 0775' % wrapper) os.chmod(wrapper, 0o775) print('%s -> %s' % (e, destfile)) #print('mv %s %s' % (e, destfile)) os.rename(e, destfile) #print('ln %s %s' % (wrapper, e)) os.link(wrapper, e) """ change permissions back """ if perms: os.chmod(e, perms) f.close()