Re: util-linux-2.41 breaks static build of btrfs-progs

Karel Zak <[email protected]>
Newsgroups org.kernel.vger.util-linux
Message-ID <hjy6pccal442gk26tgjkol43aoicvkak4p2wy64ouk2ksfdhjb@7a2yap2jorf4>
On Mon, May 05, 2025 at 11:34:11AM +0200, Karel Zak wrote:
> On Sun, Apr 20, 2025 at 09:24:43PM +0200, Stanislav Brabec wrote:
> > Static build of btrfs-progs fails with util-linux-2.41 with a simple
> > problem:
> > Symbol parse_range becomes visible in libblkid.a, breaking parse-utils.c in
> > btrfs-progs, using the same symbol.
> > 
> > The question is:
> > Should be this fixed by util-linux by prefixing of ul_ to all symbols that
> > are not declared as static?
> 
> I think the ul_ prefix should be used for all generic names like
> parse_range(). I'm not sure if I want to use it strictly for all
> functions, as the set of functions is large and such conflicts are
> very rare.
> 
> ChatGPT has an interesting suggestion: add the prefix to the .a
> library using "objcopy --redefine-syms" for all non-API and non-static
> functions.
> 
>         #!/bin/bash
>         PREFIX=foo_
> 
>         # Extract all global (non-static) function symbols from the .a file
>         nm -g --defined-only libmylib.a | awk '{print $3}' | grep -v "^$PREFIX" | sort -u > tmp.syms

Note that the list of symbols should also be filtered according to
libblkid.sym to keep the API functions unmodified.

>         # Generate rename list: <old> <new>
>         awk -v pfx="$PREFIX" '{print $1 " " pfx $1}' tmp.syms > rename.syms
> 
>         # Apply symbol renaming
>         objcopy --redefine-syms=rename.syms libmylib.a libmylib_prefixed.a


Updated, not tested version:

#!/bin/bash

set -e

LIB=libfoo.a
VERSION_SCRIPT=libfoo.sym
PREFIX=foo_
TMPDIR=$(mktemp -d)
RENAME_SYMS="$TMPDIR/rename.syms"

# 1. Get all defined global symbols in the .a (non-static functions/vars)
nm -g --defined-only "$LIB" | awk '{print $3}' | sort -u > "$TMPDIR/all_syms.txt"

# 2. Extract exported (API) symbols from the version script
awk '/global:/{flag=1; next} /local:/{flag=0} flag' "$VERSION_SCRIPT" | \
    tr -d '; \t' | grep -v '^$' | sort -u > "$TMPDIR/exported_syms.txt"

# 3. Compute symbols to rename (non-exported global ones)
comm -23 "$TMPDIR/all_syms.txt" "$TMPDIR/exported_syms.txt" | \
    awk -v pfx="$PREFIX" '{print $1 " " pfx $1}' > "$RENAME_SYMS"

# 4. Apply renaming to the archive
cp "$LIB" "${LIB%.a}_prefixed.a"
objcopy --redefine-syms="$RENAME_SYMS" "${LIB%.a}_prefixed.a"

echo "Renamed library created: ${LIB%.a}_prefixed.a"
echo "Renamed symbols:"
cat "$RENAME_SYMS"

# 5. Optional cleanup
# rm -r "$TMPDIR"



-- 
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.