cbfstool with musl libc: segmentation fault

cbfstool is a coreboot utility for manipulating CBFS. Apparently nobody uses it on musl system because it doesn't work:

$ ./cbfstool 
Segmentation fault

Let's check it with gdb:

$ gdb ./cbfstool                                                                                                                                                    [8/1250]
GNU gdb (GDB) 10.1                                                                                                                                                                               
Copyright (C) 2020 Free Software Foundation, Inc.                                          
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.                                                                                                                               
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-musl".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./cbfstool...
(gdb) run
Starting program: /home/ch1p_/coreboot/util/cbfstool/cbfstool 

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7fbeb24 in do_relocs (dso=dso@entry=0x7ffff7ffd860 <app>, rel=0x555555554a80, rel_size=6288, stride=stride@entry=3) at ldso/dynlink.c:423
423     ldso/dynlink.c: No such file or directory.
(gdb) bt
#0  0x00007ffff7fbeb24 in do_relocs (dso=dso@entry=0x7ffff7ffd860 <app>, rel=0x555555554a80, rel_size=6288, stride=stride@entry=3) at ldso/dynlink.c:423
#1  0x00007ffff7fbfaed in reloc_all (p=p@entry=0x7ffff7ffd860 <app>) at ldso/dynlink.c:1328
#2  0x00007ffff7fc15b2 in __dls3 (sp=<optimized out>) at ldso/dynlink.c:1906
#3  0x00007ffff7fc0f51 in __dls2 (base=<optimized out>, sp=0x7fffffffc570) at ldso/dynlink.c:1650
#4  0x00007ffff7fc3c0a in _dlstart ()
#5  0x0000000000000001 in ?? ()
#6  0x00007fffffffc8d8 in ?? ()
#7  0x0000000000000000 in ?? ()
(gdb) 

Looks like some incorrect relocation during linking. There are a few more similar reports, all seem to be related to musl. I don't know how to fix it but I know a workaround: just build the program statically.

There is TOOLLDFLAGS variable in cbfstool's Makefile.inc, it defines linker flags, so do this:

$ export TOOLLDFLAGS="-static"

and then

$ make clean
$ make

Verify that it was built statically:

$ ldd ./cbfstool
ldd: ./cbfstool: Not a valid dynamic program

Now it should work.

If you have any comments, contact me by email.