factorize link code so that TOC code can use it too