#!/bin/bash

# get a list of all symbols - YOU MUST RUN THIS SCRIPT WITH THE LATEST VERSIONS OF GLIBC
readelf -s --wide /lib/* >syms
objdump -T /lib/* | grep "GLIBC_" | sed 's/\(.*\)GLIBC_//; s/)//' | grep -v PRIVATE | column -t >allsym

# get a list of all symbol versions only available in 2.2+
grep @@GLIBC_ syms | awk ' { print $8 } ' | sed 's/@@/ /; /GLIBC_P/d; s/GLIBC_//' | awk ' { if ($2 > 2.2) print $1 " " $2 } ' | column -t >glibc2.3.syms

# select the symbols that already existed, but were obsoleted by 2.2+ versions
cat glibc2.3.syms | awk '{print $1}' | while read; do grep $REPLY allsym; done | sed '/2\.3/d' >syms-to-header

# select the latest symbols of that set
# build a header from them
cat syms-to-header | awk '{print $2 "  " $1 }' | sort | uniq >output
cat glibc2.3.syms | sort | uniq  >> output

cat output | sort | uniq | awk '{print $2 "   " $1}' | sort -k2,1 | awk '{ if ($1 <= 2.2) print $1 "  " $2 }' | column -t | sort -k2 | uniq -f1 >output2

# output the symbols that are brand new to 2.2+, ie not the ones that had earlier versions
cat glibc2.3.syms | awk '{ print $1 }' | while read; do if ! grep "$REPLY" output2 >/dev/null; then echo "DONT_USE_THIS_SYMBOL  $REPLY" >>output2; fi; done;

cat output2 | column -t | awk '{ print "__asm__(\".symver " $2 "," $2 "@GLIBC_" $1 "\");" }' > output
mv output apsymbols.h
