Files
takana/recipes/elfutils-libdw.toml
T
Sergio e7c2b0022c dwarves: CONSTRUYE — frontera libdw/musl cerrada
Cadena completa, cuatro recetas nuevas y una modificada:
  musl-obstack     -> libobstack (find_package(obstack REQUIRED))
  argp-standalone  -> libargp    (find_package(argp REQUIRED))
  musl-fts         -> fts(3), que usa libdwfl/linux-kernel-modules.c
  elfutils-libdw   -> libdw + libdwfl (VARIANTE: la canónica sólo hace libelf
                      porque de ella cuelgan los cuatro kernels)

Lo que costó, en orden de aparición:
· libdw a secas NO necesita argp/obstack/fts — la nota de la canónica es cierta
  de libdwfl y src/, no de libdw. El `obstack` de libdw estaba en un COMENTARIO.
· Pero dwarves SÍ usa libdwfl (15 dwfl_* en dwarf_loader.c) ⇒ vuelven argp y fts.
· libdwfl no compila suelto: hay que seguir el orden de SUBDIRS del raíz
  (lib → libelf → libcpu → backends → libebl → libdwelf → libdw → libdwfl →
  libdwfl_stacktrace).
· El compat/argp.h de la canónica TAPA el argp real y no declara argp_failure.
· libdw.a GORDA: FindDWARF.cmake pone DWARF_LIBRARIES = libdw + libelf y nada
  más, pero libdw necesita libdwfl/libebl/libdwelf/backends/libeu, que son libs
  internas. Se fusionan en un archive (patrón de la libgtk-4.a) junto con un
  `error()` real, que musl no trae y el shim de la receta sólo daba inline.
· Y al final, lzma/bz2: libdw descomprime .debug_* comprimidas.

Verificado de verdad, no por exit code: pahole v1.30 arranca (con el loader
musl del lab) y DECODIFICA DWARF — sobre un binario de prueba imprime los
offsets, los tamaños y hasta el agujero de 3 bytes de padding.
2026-08-27 20:47:54 +00:00

195 lines
14 KiB
TOML

# elfutils 0.194 (sólo libelf) — de-Alpinización del último build-dep del kernel.
#
# `tools/objtool` del kernel enlaza -lelf (libelf + gelf.h) — en x86_64 las mitigaciones fuerzan
# CONFIG_OBJTOOL. Venía de `apk add elfutils-dev` (bootstrap-lib). Esta receta construye libelf desde
# fuente. Misma versión que Alpine 3.23 (0.194). Build-tool de build-time (objtool corre durante la
# compilación del kernel; libelf NO se enlaza en el bzImage) ⇒ no afecta el of_tree del 4/4.
#
# Construimos SÓLO libelf (+ lib/libeu.a, su helper) — objtool no necesita libdw/libdwfl/src, que son
# los que arrastran argp/obstack/fts (lo verdaderamente difícil en musl). elfutils es glibc-céntrico;
# musl no trae <error.h>, <argp.h>, <libintl.h> ni rawmemchr. Estrategia de shims (en compat/, vía
# CPPFLAGS/-include) en vez de los parches completos de Alpine:
# - <libintl.h>: lo elimina --disable-nls (eu-config.h sólo lo incluye con ENABLE_NLS).
# - <error.h>: shim con error()/error_at_line() (musl ya exporta program_invocation_short_name).
# - <argp.h>: shim de tipos/decls — sólo lo usan lib/color.c y lib/printversion.c, cuyos .o van en
# libeu.a pero objtool NO referencia (sólo necesita el core de libelf) ⇒ basta que
# COMPILEN; las funcs argp quedan como undefined inertes en el .a.
# - rawmemchr: shim inline (lo usa libelf/elf_getarsym.c), force-include global.
# zig 0.13.0 (NO gcc): la regresión de zig 0.14 en C/musl (bisect con flex) miscompila; 0.13.0 la
# esquiva, consistente con binutils/openssl/flex. $CC (= zig cc del lab) para los stubs y las fases.
# elfutils 0.194 — VARIANTE que además construye `libdw`. NO sustituye a `recipes/elfutils.toml`.
#
# ── POR QUÉ VARIANTE ────────────────────────────────────────────────────────────────────────────
# De la canónica cuelgan los CUATRO kernels (linux, linux-generic, linux-metal, linux-metal-dual):
# su `tools/objtool` enlaza -lelf. Moverle el hash los invalidaría a todos. Esta variante existe
# sólo para `dwarves`, que necesita `libdw` + `dwarf.h`. Mismo patrón que `openssl-threads`.
#
# ── LO QUE LA CANÓNICA DICE Y HAY QUE MATIZAR ───────────────────────────────────────────────────
# La canónica avisa: «libdw/libdwfl son los que arrastran argp/obstack/fts (lo verdaderamente
# difícil en musl)». Es cierto de `libdwfl` y de `src/`, **pero NO de `libdw` a secas**, que es lo
# único que pide `dwarves`. Verificado sobre el fuente de 0.194:
# · `argp_`/`fts_open` → sólo en `libdwfl/`, cero en `libdw/`.
# · `obstack` → en `libdw/` aparece UNA vez y es **dentro de un COMENTARIO** de `libdwP.h`:
# elfutils lleva su propia «simplified thread-local reimplementation of obstacks» y NO incluye
# `<obstack.h>` en ningún sitio de libdw.
# ⇒ `libdw` a secas no necesita ni argp ni fts ni obstack de verdad.
#
# ⚠ PERO `dwarves` SÍ necesita `libdwfl` (llama a 15 `dwfl_*` distintas desde `dwarf_loader.c`), y
# libdwfl sí arrastra los dos: `argp-std.c` usa argp y `linux-kernel-modules.c` usa fts. Por eso
# esta receta declara `argp-standalone` y `musl-fts`: no por libdw, por libdwfl.
# En la práctica esos objetos casi nunca se ENLAZAN (dwarves no usa `dwfl_standard_argp` ni
# `dwfl_linux_kernel_*`), pero tienen que COMPILAR, y para eso hacen falta `<argp.h>` y `<fts.h>`
# de verdad — el `compat/` de la canónica trae un argp.h de mentira y NINGÚN fts.h.
#
name = "elfutils-libdw"
version = "0.194"
[source]
tarball = "https://sourceware.org/elfutils/ftp/0.194/elfutils-0.194.tar.bz2"
sha256 = "09e2ff033d39baa8b388a2d7fbc5390bfde99ae3b7c67c7daaf7433fbcf0f01e"
[build]
compiler = "zig-cc"
target = "x86_64-linux-musl"
link = "dynamic"
zig_version = "0.13.0"
[deps]
build = ["binutils", "argp-standalone", "musl-fts"]
[build.phases]
configure = """
mkdir -p compat && \
printf '%s\\n' \
'#ifndef _COMPAT_ERROR_H' '#define _COMPAT_ERROR_H' \
'#include <stdio.h>' '#include <stdlib.h>' '#include <stdarg.h>' '#include <string.h>' \
'extern char *program_invocation_short_name;' \
'static inline void error(int s,int e,const char*f,...){va_list a;fflush(stdout);' \
' fprintf(stderr,"%s: ",program_invocation_short_name);va_start(a,f);vfprintf(stderr,f,a);va_end(a);' \
' if(e)fprintf(stderr,": %s",strerror(e));fputc(0x0a,stderr);if(s)exit(s);}' \
'static inline void error_at_line(int s,int e,const char*fn,unsigned ln,const char*f,...){va_list a;' \
' fflush(stdout);fprintf(stderr,"%s:%s:%u: ",program_invocation_short_name,fn,ln);' \
' va_start(a,f);vfprintf(stderr,f,a);va_end(a);if(e)fprintf(stderr,": %s",strerror(e));' \
' fputc(0x0a,stderr);if(s)exit(s);}' \
'#endif' > compat/error.h && \
printf '%s\\n' \
'#ifndef _COMPAT_ARGP_H' '#define _COMPAT_ARGP_H' '#include <stdio.h>' '#include <errno.h>' \
'typedef int error_t;' \
'struct argp_option{const char*name;int key;const char*arg;int flags;const char*doc;int group;};' \
'struct argp;' \
'struct argp_state{const struct argp*root_argp;int argc;char**argv;int next;unsigned flags;' \
' unsigned arg_num;int quoted;void*input;void**child_inputs;void*hook;char*name;FILE*err_stream;' \
' FILE*out_stream;void*pstate;};' \
'typedef error_t(*argp_parser_t)(int,char*,struct argp_state*);' \
'struct argp_child;' \
'struct argp{const struct argp_option*options;argp_parser_t parser;const char*args_doc;' \
' const char*doc;const struct argp_child*children;char*(*help_filter)(int,const char*,void*);' \
' const char*argp_domain;};' \
'struct argp_child{const struct argp*argp;int flags;const char*header;int group;};' \
'#define ARGP_ERR_UNKNOWN E2BIG' '#define ARGP_KEY_ARG 0' '#define ARGP_KEY_END 0x1000001' \
'#define ARGP_KEY_INIT 0x1000003' '#define ARGP_KEY_FINI 0x1000007' '#define ARGP_KEY_SUCCESS 0x1000002' \
'#define ARGP_HELP_SEE 0x04' '#define ARGP_HELP_STD_ERR 0x2a' \
'#define OPTION_ARG_OPTIONAL 0x1' '#define OPTION_HIDDEN 0x2' '#define OPTION_ALIAS 0x4' \
'#define OPTION_DOC 0x8' '#define OPTION_NO_USAGE 0x10' '#define ARGP_IN_ORDER 0x08' \
'#define ARGP_NO_HELP 0x10' '#define ARGP_NO_EXIT 0x20' '#define ARGP_LONG_ONLY 0x40' \
'extern const char*argp_program_version;extern const char*argp_program_bug_address;' \
'extern void(*argp_program_version_hook)(FILE*,struct argp_state*);' \
'extern error_t argp_parse(const struct argp*,int,char**,unsigned,int*,void*);' \
'extern void argp_help(const struct argp*,FILE*,unsigned,char*);' \
'extern void argp_error(const struct argp_state*,const char*,...);' \
'#endif' > compat/argp.h && \
printf '%s\\n' \
'#ifndef _COMPAT_MUSL_EXTRA_H' '#define _COMPAT_MUSL_EXTRA_H' '#include <string.h>' \
'#ifndef __GLIBC__' \
'static inline void*rawmemchr(const void*s,int c){const unsigned char*p=s;' \
' for(;*p!=(unsigned char)c;p++){}' ' return(void*)p;}' \
'#endif' '#endif' > compat/musl-extra.h && \
printf '%s\\n' \
'#ifndef _COMPAT_LIBINTL_H' '#define _COMPAT_LIBINTL_H' \
'#define gettext(s) (s)' '#define dgettext(d,s) (s)' '#define dcgettext(d,s,c) (s)' \
'#define ngettext(s,p,n) ((n)==1?(s):(p))' '#define dngettext(d,s,p,n) ((n)==1?(s):(p))' \
'#define bindtextdomain(d,x) ((char*)0)' '#define textdomain(d) ((char*)0)' \
'#define bind_textdomain_codeset(d,c) ((char*)0)' \
'#endif' > compat/libintl.h && \
printf '%s\\n' \
'#include <stdio.h>' 'struct argp;struct argp_state;typedef int error_t;' \
'const char*argp_program_version;const char*argp_program_bug_address;' \
'void(*argp_program_version_hook)(FILE*,struct argp_state*);' \
'error_t argp_parse(const struct argp*a,int c,char**v,unsigned f,int*i,void*n){return 0;}' \
'void argp_help(const struct argp*a,FILE*s,unsigned f,char*n){}' \
'void argp_error(const struct argp_state*s,const char*f,...){}' \
'void argp_state_help(const struct argp_state*s,FILE*t,unsigned f){}' \
'void argp_usage(const struct argp_state*s){}' > compat/argp-stub.c && \
$CC -c compat/argp-stub.c -o compat/argp-stub.o && ar rcs compat/libargp.a compat/argp-stub.o && \
printf '%s\\n' \
'typedef struct{int x;}FTS;typedef struct{int y;}FTSENT;' \
'FTS*fts_open(char*const*p,int o,int(*c)(const FTSENT**,const FTSENT**)){return 0;}' \
'FTSENT*fts_read(FTS*f){return 0;}' 'FTSENT*fts_children(FTS*f,int o){return 0;}' \
'int fts_set(FTS*f,FTSENT*e,int o){return 0;}' 'int fts_close(FTS*f){return 0;}' > compat/fts-stub.c && \
$CC -c compat/fts-stub.c -o compat/fts-stub.o && ar rcs compat/libfts.a compat/fts-stub.o && \
printf '%s\\n' \
'struct obstack;void(*obstack_alloc_failed_handler)(void);int obstack_exit_failure;' \
'int _obstack_begin(struct obstack*o,int s,int a,void*(*c)(long),void(*f)(void*)){return 1;}' \
'int _obstack_begin_1(struct obstack*o,int s,int a,void*(*c)(void*,long),void(*f)(void*,void*),void*x){return 1;}' \
'void _obstack_newchunk(struct obstack*o,int l){}' 'int _obstack_allocated_p(struct obstack*o,void*p){return 0;}' \
'void _obstack_free(struct obstack*o,void*p){}' 'int _obstack_memory_used(struct obstack*o){return 0;}' \
> compat/obstack-stub.c && \
$CC -c compat/obstack-stub.c -o compat/obstack-stub.o && ar rcs compat/libobstack.a compat/obstack-stub.o && \
CFLAGS="-O2 -g -I$PWD/compat" CPPFLAGS="-I$PWD/compat" LDFLAGS="-L$PWD/compat" \
./configure --prefix=/usr --libdir=/usr/lib --disable-nls --disable-demangler \
--disable-libdebuginfod --disable-debuginfod --program-prefix=eu-
"""
# El -include del shim de rawmemchr va SÓLO en compile (en configure rompe el test "undeclared builtins"
# de autoconf). CFLAGS aquí reemplaza el de configure (mismo valor + el -include); AM_CFLAGS (warnings/
# -Werror) se preserva.
# ⚠ `rm -f compat/argp.h`: el shim de la canónica es sólo tipos/decls y NO declara
# `argp_failure`, que usa `libdwfl/argp-std.c`. Esta variante SÍ trae argp de verdad
# (`argp-standalone`), así que se borra el shim para que gane el `/usr/include/argp.h` real —
# `-I$PWD/compat` va primero y si no lo taparía.
compile = "rm -f compat/argp.h && make -C lib CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\" && make -C libelf libelf.a libelf.so CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\" && make -C libcpu CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\" && make -C backends CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\" && make -C libebl CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\" && make -C libdwelf CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\" && make -C libdw libdw.a CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\" && make -C libdwfl libdwfl.a CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\" && make -C libdwfl_stacktrace libdwfl_stacktrace.a CFLAGS=\"-O2 -g -I$PWD/compat -include $PWD/compat/musl-extra.h\""
install = """
mkdir -p /out/usr/lib/pkgconfig /out/usr/include && \
cp -a libelf/libelf.a libelf/libelf.so* /out/usr/lib/ && \
cp -a libelf/libelf.h libelf/gelf.h /out/usr/include/ && \
cp -a config/libelf.pc /out/usr/lib/pkgconfig/ 2>/dev/null || true
mkdir -p /out/usr/include/elfutils
cp -a libdw/libdw.a /out/usr/lib/
cp -a libdw/dwarf.h libdw/libdw.h libdw/known-dwarf.h /out/usr/include/
cp -a libdwfl/libdwfl.h /out/usr/include/
# libdw.a GORDA: dwarves enlaza sólo `-ldw -lelf` (su FindDWARF.cmake pone
# DWARF_LIBRARIES = libdw + libelf y nada más), pero libdw necesita libdwfl, libebl, libdwelf,
# los backends y libeu, que en elfutils son libs INTERNAS. En el .so de una distro van dentro;
# acá hay que fusionarlas a mano en un solo archive o salen `undefined symbol: dwfl_begin`,
# `eu_tsearch`, `eu_search_tree_init`… Mismo patrón que la libgtk-4.a gorda de `gtk4`.
#
# `error()` va también: es de <error.h> de glibc y musl no lo trae. El compat/error.h de la receta
# es `static inline`, así que sólo sirve a las TUs que lo incluyen; los objetos que lo referencian
# desde otra unidad necesitan un símbolo REAL. Se compila uno y se mete en el archive.
printf '%s\n' '#include <stdio.h>' '#include <stdlib.h>' '#include <stdarg.h>' '#include <string.h>' \
'extern char *program_invocation_short_name;' \
'void error(int s,int e,const char*f,...){va_list a;fflush(stdout);' \
' fprintf(stderr,"%s: ",program_invocation_short_name);va_start(a,f);vfprintf(stderr,f,a);va_end(a);' \
' if(e)fprintf(stderr,": %s",strerror(e));fputc(0x0a,stderr);if(s)exit(s);}' \
'void error_at_line(int s,int e,const char*fn,unsigned ln,const char*f,...){va_list a;fflush(stdout);' \
' fprintf(stderr,"%s:%s:%u: ",program_invocation_short_name,fn,ln);' \
' va_start(a,f);vfprintf(stderr,f,a);va_end(a);if(e)fprintf(stderr,": %s",strerror(e));' \
' fputc(0x0a,stderr);if(s)exit(s);}' > /tmp/error-impl.c
$CC -c -O2 /tmp/error-impl.c -o /tmp/error-impl.o
rm -rf /tmp/fat && mkdir -p /tmp/fat && cd /tmp/fat
for A in $OLDPWD/libdw/libdw.a $OLDPWD/libdwfl/libdwfl.a $OLDPWD/libebl/libebl.a \
$OLDPWD/libdwelf/libdwelf.a $OLDPWD/backends/libebl_backends.a $OLDPWD/lib/libeu.a \\
$OLDPWD/libdwfl_stacktrace/libdwfl_stacktrace.a; do
[ -f "$A" ] || continue
n=$(basename "$A" .a); mkdir -p $n && (cd $n && ar x "$A")
done
cd $OLDPWD
ar rcs /out/usr/lib/libdw.a $(find /tmp/fat -name '*.o') /tmp/error-impl.o
cp -a libdwfl/libdwfl.a /out/usr/lib/
cp -a libdw/libdw.h libdw/known-dwarf.h /out/usr/include/elfutils/
cp -a libdwfl/libdwfl.h /out/usr/include/elfutils/
cp -a version.h /out/usr/include/elfutils/ 2>/dev/null || true
cp -a config/libdw.pc /out/usr/lib/pkgconfig/ 2>/dev/null || true
test -f /out/usr/lib/libdw.a && test -f /out/usr/include/dwarf.h && test -f /out/usr/include/libdw.h && test -f /out/usr/lib/libdwfl.a && test -f /out/usr/include/elfutils/libdwfl.h
"""