xref: /freebsd/crypto/openssh/freebsd-namespace.sh (revision 3cbb4cc200f8a0ad7ed08233425ea54524a21f1c)
1#!/bin/sh
2#
3# Namespace munging inspired by an equivalent hack in NetBSD's tree: add
4# the "Fssh_" prefix to every symbol in libssh which doesn't already have
5# it.  This prevents collisions between symbols in libssh and symbols in
6# other libraries or applications which link with libssh, either directly
7# or indirectly (e.g. through PAM loading pam_ssh).
8#
9# $FreeBSD$
10#
11
12set -e
13
14eval "unset $(env | sed -nE 's/^(LC_[A-Z]+)=.*$/\1/p')"
15export LANG=C
16
17error() {
18	echo "$@" >&2
19	exit 1
20}
21
22# Locate the source directories
23self=$(realpath ${0})
24srcdir=${self%/*}
25header=${srcdir}/ssh_namespace.h
26top_srcdir=${srcdir%/crypto/openssh}
27libssh_srcdir=${top_srcdir}/secure/lib/libssh
28
29if [ ! -d ${srcdir} -o \
30     ! -f ${header} -o \
31     ! -d ${libssh_srcdir} -o \
32     ! -f ${libssh_srcdir}/Makefile ] ; then
33	error "Where is the libssh Makefile?"
34fi
35
36ncpu=$(sysctl -n hw.ncpu)
37ssh_make() {
38	make -C${libssh_srcdir} -j$((ncpu + 1)) "$@"
39}
40
41# Clear out, recreate and locate the libssh build directory
42ssh_make cleandir
43ssh_make cleandir
44ssh_make obj
45libssh_builddir=$(realpath $(ssh_make -V.OBJDIR))
46libssh=libprivatessh.a
47
48# Clear the existing header
49cat >${header} <<EOF
50/*
51 * This file was machine-generated.  Do not edit manually.
52 * Run crypto/openssh/freebsd-namespace.sh to regenerate.
53 */
54EOF
55
56# Build libssh
57ssh_make depend
58ssh_make ${libssh}
59if [ ! -f ${libssh_builddir}/${libssh} ] ; then
60	error "Where is ${libssh}?"
61fi
62
63# Extract symbols
64nm ${libssh_builddir}/${libssh} | awk '
65     /^[0-9a-z]+ [Tt] [A-Za-z_][0-9A-Za-z_]*$/ && $3 !~ /^Fssh_/ {
66         printf("#define %-39s Fssh_%s\n", $3, $3)
67     }
68' | unexpand -a | sort -u >>${header}
69
70# Clean and rebuild the library
71ssh_make clean
72ssh_make ${libssh}
73
74# Double-check
75nm ${libssh_builddir}/${libssh} | awk '
76    /^[0-9a-z]+ [Tt] [A-Za-z_][0-9A-Za-z_]*$/ && $3 !~ /^Fssh_/ {
77         printf("ERROR: %s was not renamed!\n", $3);
78         err++;
79    }
80    END {
81        if (err > 0)
82            exit(1);
83    }
84'
85