| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. -e |
| 2 | SCRATCH=scratch/$(basenamescip-shell system . . basename. "$0" .sh) |
| 3 | PATCHELF=$(readlinkscip-shell system . . readlink. -f "../src/patchelf") |
| 4 | |
| 5 | rmscip-shell system . . rm. -rf "${SCRATCH}" |
| 6 | mkdirscip-shell system . . mkdir. -p "${SCRATCH}" |
| 7 | |
| 8 | full_main_name="${PWD}/many-syms-main" |
| 9 | full_lib_name="${PWD}/libmany-syms.so" |
| 10 | chmodscip-shell system . . chmod. -w "$full_lib_name" "$full_main_name" |
| 11 | |
| 12 | suffix="_special_suffix" |
| 13 | |
| 14 | cd "${SCRATCH}" |
| 15 | |
| 16 | ############################################################################### |
| 17 | # Test that all symbols in the dynamic symbol table will have the expected |
| 18 | # names after renaming. |
| 19 | # Also test that if we rename all symbols back, the symbols are as expected |
| 20 | ############################################################################### |
| 21 | |
| 22 | list_symbolsscip-shell shell patchelf 0.18.0-1.4 list_symbols().() { |
| 23 | nmscip-shell system . . nm. -D "$@" | awkscip-shell system . . awk. '{ print $NF }' | sedscip-shell system . . sed. '/^ *$/dscip-shell filesystem . . `/^ *$/d`.' |
| 24 | } |
| 25 | |
| 26 | list_symbolsscip-shell shell patchelf 0.18.0-1.4 list_symbols(). "$full_lib_name" | cutscip-shell system . . cut. -d@ -f1 | sortscip-shell system . . sort. -u | awkscip-shell system . . awk. "{printf \"%s %s${suffix}\n\",\$1,\$1}" > map |
| 27 | list_symbolsscip-shell shell patchelf 0.18.0-1.4 list_symbols(). "$full_lib_name" | cutscip-shell system . . cut. -d@ -f1 | sortscip-shell system . . sort. -u | awkscip-shell system . . awk. "{printf \"%s${suffix} %s\n\",\$1,\$1}" > rmap |
| 28 | |
| 29 | ${PATCHELF} --rename-dynamic-symbols map --output libmapped.so "$full_lib_name" |
| 30 | ${PATCHELF} --rename-dynamic-symbols rmap --output libreversed.so libmapped.so |
| 31 | |
| 32 | list_symbolsscip-shell shell patchelf 0.18.0-1.4 list_symbols(). "$full_lib_name" | sortscip-shell system . . sort. > orig_syms |
| 33 | list_symbolsscip-shell shell patchelf 0.18.0-1.4 list_symbols(). libmapped.so | sortscip-shell system . . sort. > map_syms |
| 34 | list_symbolsscip-shell shell patchelf 0.18.0-1.4 list_symbols(). libreversed.so | sortscip-shell system . . sort. > rev_syms |
| 35 | |
| 36 | diffscip-shell system . . diff. orig_syms rev_syms > diff_orig_syms_rev_syms || exit 1 |
| 37 | |
| 38 | # Renamed symbols that match version numbers will be printed with version instead of them being ommited |
| 39 | # CXXABI10 is printed as CXXABI10 |
| 40 | # but CXXABI10_renamed is printed as CXXABI10_renamed@@CXXABI10 |
| 41 | # awk is used to remove these cases so that we can match the "mapped" symbols to original symbols |
| 42 | sedscip-shell system . . sed. "s/${suffix}//" map_syms | awkscip-shell system . . awk. -F @ '{ if ($1 == $2 || $1 == $3) { print $1; } else { print $0; }}' | sortscip-shell system . . sort. > map_syms_r |
| 43 | diffscip-shell system . . diff. orig_syms map_syms_r > diff_orig_syms_map_syms_r || exit 1 |
| 44 | |
| 45 | ############################################################################### |
| 46 | # Check the relocation tables after renaming |
| 47 | ############################################################################### |
| 48 | |
| 49 | print_relocation_tablescip-shell shell patchelf 0.18.0-1.4 print_relocation_table().() { |
| 50 | readelfscip-shell system . . readelf. -W -r "$1" | awkscip-shell system . . awk. '{ printf "%s\n",$5 }' | cutscip-shell system . . cut. -f1 -d@ |
| 51 | } |
| 52 | |
| 53 | print_relocation_tablescip-shell shell patchelf 0.18.0-1.4 print_relocation_table(). "$full_lib_name" > orig_rel |
| 54 | print_relocation_tablescip-shell shell patchelf 0.18.0-1.4 print_relocation_table(). libmapped.so > map_rel |
| 55 | print_relocation_tablescip-shell shell patchelf 0.18.0-1.4 print_relocation_table(). libreversed.so > rev_rel |
| 56 | |
| 57 | diffscip-shell system . . diff. orig_rel rev_rel > diff_orig_rel_rev_rel || exit 1 |
| 58 | sedscip-shell system . . sed. "s/${suffix}//" map_rel > map_rel_r |
| 59 | diffscip-shell system . . diff. orig_rel map_rel_r > diff_orig_rel_map_rel_r || exit 1 |
| 60 | |
| 61 | ############################################################################### |
| 62 | # Test that the hash table is correctly updated. |
| 63 | # For this to work, we need to rename symbols and actually use the library |
| 64 | # Here we: |
| 65 | # 1. Create a map from all symbols in libstdc++.so as "sym sym_special_suffix" |
| 66 | # 2. Copy Patchelf and all of its transitive library dependencies into a new directory |
| 67 | # 3. Rename symbols in Patchelf and all dependencies according to the map |
| 68 | # 4. Run patchelf with the modified dependencies |
| 69 | ############################################################################### |
| 70 | |
| 71 | echoscip-shell system . . echo. "# Create the map" |
| 72 | list_symbolsscip-shell shell patchelf 0.18.0-1.4 list_symbols(). --defined-only "$full_lib_name" | cutscip-shell system . . cut. -d@ -f1 | sortscip-shell system . . sort. -u | awkscip-shell system . . awk. "{printf \"%s %s${suffix}\n\",\$1,\$1}" > map |
| 73 | |
| 74 | echoscip-shell system . . echo. "# Copy all dependencies" |
| 75 | mkdirscip-shell system . . mkdir. env |
| 76 | cd env |
| 77 | cpscip-shell system . . cp. "$full_lib_name" "$full_main_name" . |
| 78 | |
| 79 | echoscip-shell system . . echo. "# Apply renaming" |
| 80 | chmodscip-shell system . . chmod. +w ./* |
| 81 | ${PATCHELF} --rename-dynamic-symbols ../map ./* |
| 82 | |
| 83 | echoscip-shell system . . echo. "# Run the patched tool and libraries" |
| 84 | envscip-shell system . . env. LD_BIND_NOW=1 LD_LIBRARY_PATH="${PWD}" ./many-syms-main |
| 85 | |
| 86 | # Test that other switches still work when --rename-dynamic-symbols has no effect |
| 87 | echoscip-shell system . . echo. "SYMBOL_THAT_DOESNT_EXIST ANOTHER_NAME" > map |
| 88 | ${PATCHELF} --set-rpath changed_rpath --rename-dynamic-symbols map --output libnewrpath.so "$full_lib_name" |
| 89 | [scip-shell system . . `[`. "$(${PATCHELF} --print-rpath libnewrpath.so)" = changed_rpath ] || exit 1 |
| 90 |