From: Luca Boccassi Date: Fri, 5 Aug 2022 00:19:52 +0000 (-0700) Subject: Add gold support for --package-metadata option. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=701821154b110230f52a0367e120ecc51f490e56;p=binutils-gdb.git Add gold support for --package-metadata option. Following the same format as the implementation in ld: 9e2bb0cb5e74aed4158f08495534922d7108f928 Generate a .note.package FDO package metadata ELF note, following the spec: https://systemd.io/ELF_PACKAGE_METADATA/ If the jansson library is available at build time (and it is explicitly enabled), link ld to it, and use it to validate that the input is correct JSON, to avoid writing garbage to the file. The configure option --enable-jansson has to be used to explicitly enable it (error out when not found). This allows bootstrappers (or others who are not interested) to seamlessly skip it without issues. elfcpp/ * elfcpp.h: Add FDO_PACKAGING_METADATA note type. gold/ * Makefile.am: Add jansson flags and libraries. * configure.ac: Check for jansson library. * layout.cc (Layout::create_notes): Call create_package_metadata(). (Layout::create_package_metadata): New function. * layout.h (Layout::create_package_metadata): New function. (Layout::package_metadata_note_): New data member. * options.h (class General_options): Add --package-metadata option. * testsuite/Makefile.am (object_unittest): Add jansson libraries. (binary_unittest): Likewise. (leb128_unittest): Likewise. (overflow_unittest): Likewise. (package_metadata_test): New test. * testsuite/package_metadata_main.c: New test source. --- diff --git a/elfcpp/elfcpp.h b/elfcpp/elfcpp.h index 4b0aff0da7f..3ca2d614947 100644 --- a/elfcpp/elfcpp.h +++ b/elfcpp/elfcpp.h @@ -999,7 +999,9 @@ enum // string. NT_GNU_GOLD_VERSION = 4, // Program property note, as described in "Linux Extensions to the gABI". - NT_GNU_PROPERTY_TYPE_0 = 5 + NT_GNU_PROPERTY_TYPE_0 = 5, + // FDO .note.package notes as defined on https://systemd.io/ELF_PACKAGE_METADATA/ + FDO_PACKAGING_METADATA = 0xcafe1a7e }; // The OS values which may appear in word 0 of a NT_GNU_ABI_TAG note. diff --git a/gold/Makefile.am b/gold/Makefile.am index 2e406716f29..934e3669977 100644 --- a/gold/Makefile.am +++ b/gold/Makefile.am @@ -35,7 +35,7 @@ THREADFLAGS = @PTHREAD_CFLAGS@ THREADLIBS = @PTHREAD_LIBS@ AM_CFLAGS = $(WARN_CFLAGS) $(LFS_CFLAGS) $(RANDOM_SEED_CFLAGS) $(ZLIBINC) $(THREADFLAGS) -AM_CXXFLAGS = $(WARN_CXXFLAGS) $(LFS_CFLAGS) $(RANDOM_SEED_CFLAGS) $(ZLIBINC) $(THREADFLAGS) +AM_CXXFLAGS = $(WARN_CXXFLAGS) $(LFS_CFLAGS) $(RANDOM_SEED_CFLAGS) $(ZLIBINC) $(THREADFLAGS) $(JANSSON_CFLAGS) AM_LDFLAGS = $(THREADFLAGS) AM_CPPFLAGS = \ @@ -187,7 +187,7 @@ libgold_a_LIBADD = $(LIBOBJS) sources_var = main.cc deps_var = $(TARGETOBJS) libgold.a $(LIBIBERTY) $(LIBINTL_DEP) ldadd_var = $(TARGETOBJS) libgold.a $(LIBIBERTY) $(GOLD_LDADD) $(LIBINTL) \ - $(THREADLIBS) $(LIBDL) $(ZLIB) + $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) ldflags_var = $(GOLD_LDFLAGS) ld_new_SOURCES = $(sources_var) @@ -201,12 +201,12 @@ incremental_dump_SOURCES = incremental-dump.cc incremental_dump_DEPENDENCIES = $(TARGETOBJS) libgold.a $(LIBIBERTY) \ $(LIBINTL_DEP) incremental_dump_LDADD = $(TARGETOBJS) libgold.a $(LIBIBERTY) $(LIBINTL) \ - $(THREADLIBS) $(LIBDL) $(ZLIB) + $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) dwp_SOURCES = dwp.cc dwp_DEPENDENCIES = libgold.a $(LIBIBERTY) $(LIBINTL_DEP) dwp_LDADD = libgold.a $(LIBIBERTY) $(GOLD_LDADD) $(LIBINTL) $(THREADLIBS) \ - $(LIBDL) $(ZLIB) + $(LIBDL) $(ZLIB) $(JANSSON_LIBS) dwp_LDFLAGS = $(GOLD_LDFLAGS) CONFIG_STATUS_DEPENDENCIES = $(srcdir)/../bfd/development.sh diff --git a/gold/Makefile.in b/gold/Makefile.in index 516eace5aa5..b7b7ad570ff 100644 --- a/gold/Makefile.in +++ b/gold/Makefile.in @@ -130,6 +130,7 @@ am__aclocal_m4_deps = $(top_srcdir)/../config/ax_pthread.m4 \ $(top_srcdir)/../config/lead-dot.m4 \ $(top_srcdir)/../config/nls.m4 \ $(top_srcdir)/../config/override.m4 \ + $(top_srcdir)/../config/pkg.m4 \ $(top_srcdir)/../config/plugins.m4 \ $(top_srcdir)/../config/po.m4 \ $(top_srcdir)/../config/progtest.m4 \ @@ -193,7 +194,7 @@ ld_new_OBJECTS = $(am_ld_new_OBJECTS) am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) libgold.a $(LIBIBERTY) \ $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ - $(am__DEPENDENCIES_1) + $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) ld_new_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(ld_new_LDFLAGS) \ $(LDFLAGS) -o $@ @GCC_TRUE@@NATIVE_LINKER_TRUE@am_ld1_OBJECTS = $(am__objects_4) @@ -549,6 +550,8 @@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_SCRIPT = @INSTALL_SCRIPT@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ INSTOBJEXT = @INSTOBJEXT@ +JANSSON_CFLAGS = @JANSSON_CFLAGS@ +JANSSON_LIBS = @JANSSON_LIBS@ LDFLAGS = @LDFLAGS@ LFS_CFLAGS = @LFS_CFLAGS@ LIBINTL = @LIBINTL@ @@ -575,6 +578,9 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_URL = @PACKAGE_URL@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PKG_CONFIG = @PKG_CONFIG@ +PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ +PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ POSUB = @POSUB@ PTHREAD_CC = @PTHREAD_CC@ PTHREAD_CFLAGS = @PTHREAD_CFLAGS@ @@ -668,7 +674,7 @@ ZLIBINC = @zlibinc@ THREADFLAGS = @PTHREAD_CFLAGS@ THREADLIBS = @PTHREAD_LIBS@ AM_CFLAGS = $(WARN_CFLAGS) $(LFS_CFLAGS) $(RANDOM_SEED_CFLAGS) $(ZLIBINC) $(THREADFLAGS) -AM_CXXFLAGS = $(WARN_CXXFLAGS) $(LFS_CFLAGS) $(RANDOM_SEED_CFLAGS) $(ZLIBINC) $(THREADFLAGS) +AM_CXXFLAGS = $(WARN_CXXFLAGS) $(LFS_CFLAGS) $(RANDOM_SEED_CFLAGS) $(ZLIBINC) $(THREADFLAGS) $(JANSSON_CFLAGS) AM_LDFLAGS = $(THREADFLAGS) AM_CPPFLAGS = \ -I$(srcdir) -I$(srcdir)/../include -I$(srcdir)/../elfcpp \ @@ -805,7 +811,7 @@ libgold_a_LIBADD = $(LIBOBJS) sources_var = main.cc deps_var = $(TARGETOBJS) libgold.a $(LIBIBERTY) $(LIBINTL_DEP) ldadd_var = $(TARGETOBJS) libgold.a $(LIBIBERTY) $(GOLD_LDADD) $(LIBINTL) \ - $(THREADLIBS) $(LIBDL) $(ZLIB) + $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) ldflags_var = $(GOLD_LDFLAGS) ld_new_SOURCES = $(sources_var) @@ -818,12 +824,12 @@ incremental_dump_DEPENDENCIES = $(TARGETOBJS) libgold.a $(LIBIBERTY) \ $(LIBINTL_DEP) incremental_dump_LDADD = $(TARGETOBJS) libgold.a $(LIBIBERTY) $(LIBINTL) \ - $(THREADLIBS) $(LIBDL) $(ZLIB) + $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) dwp_SOURCES = dwp.cc dwp_DEPENDENCIES = libgold.a $(LIBIBERTY) $(LIBINTL_DEP) dwp_LDADD = libgold.a $(LIBIBERTY) $(GOLD_LDADD) $(LIBINTL) $(THREADLIBS) \ - $(LIBDL) $(ZLIB) + $(LIBDL) $(ZLIB) $(JANSSON_LIBS) dwp_LDFLAGS = $(GOLD_LDFLAGS) CONFIG_STATUS_DEPENDENCIES = $(srcdir)/../bfd/development.sh diff --git a/gold/aclocal.m4 b/gold/aclocal.m4 index 303c4cc5d56..6a0cd72170c 100644 --- a/gold/aclocal.m4 +++ b/gold/aclocal.m4 @@ -1203,6 +1203,7 @@ m4_include([../config/lcmessage.m4]) m4_include([../config/lead-dot.m4]) m4_include([../config/nls.m4]) m4_include([../config/override.m4]) +m4_include([../config/pkg.m4]) m4_include([../config/plugins.m4]) m4_include([../config/po.m4]) m4_include([../config/progtest.m4]) diff --git a/gold/config.in b/gold/config.in index ca0b9d2e2f9..a2000d3be6d 100644 --- a/gold/config.in +++ b/gold/config.in @@ -112,6 +112,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_INTTYPES_H +/* The jansson library is to be used */ +#undef HAVE_JANSSON + /* Define if your file defines LC_MESSAGES. */ #undef HAVE_LC_MESSAGES diff --git a/gold/configure b/gold/configure index 5f5b8c3602b..84c413fecd2 100755 --- a/gold/configure +++ b/gold/configure @@ -631,6 +631,11 @@ HAVE_NO_USE_LINKER_PLUGIN_FALSE HAVE_NO_USE_LINKER_PLUGIN_TRUE HAVE_PUBNAMES_FALSE HAVE_PUBNAMES_TRUE +JANSSON_LIBS +JANSSON_CFLAGS +PKG_CONFIG_LIBDIR +PKG_CONFIG_PATH +PKG_CONFIG THREADS_FALSE THREADS_TRUE PTHREAD_CFLAGS @@ -851,6 +856,7 @@ with_gold_ldflags with_gold_ldadd with_system_zlib enable_threads +enable_jansson enable_maintainer_mode ' ac_precious_vars='build_alias @@ -867,6 +873,11 @@ CXXFLAGS CCC YACC YFLAGS +PKG_CONFIG +PKG_CONFIG_PATH +PKG_CONFIG_LIBDIR +JANSSON_CFLAGS +JANSSON_LIBS CXXCPP' @@ -1503,6 +1514,7 @@ Optional Features: --enable-werror treat compile warnings as errors --enable-build-warnings enable build-time compiler warnings --enable-threads[=ARG] multi-threaded linking [ARG={auto,yes,no}] + --enable-jansson enable jansson [default=no] --enable-maintainer-mode enable make rules and dependencies not useful (and sometimes confusing) to the casual installer @@ -1533,6 +1545,15 @@ Some influential environment variables: YFLAGS The list of arguments that will be passed by default to $YACC. This script will default YFLAGS to the empty string to avoid a default value of `-d' given by some make applications. + PKG_CONFIG path to pkg-config utility + PKG_CONFIG_PATH + directories to add to pkg-config's search path + PKG_CONFIG_LIBDIR + path overriding pkg-config's built-in search path + JANSSON_CFLAGS + C compiler flags for JANSSON, overriding pkg-config + JANSSON_LIBS + linker flags for JANSSON, overriding pkg-config CXXCPP C++ preprocessor Use these variables to override the choices made by `configure' or to help @@ -9575,6 +9596,251 @@ else fi +# Used to validate --package-metadata= input. Disabled by default. +# Check whether --enable-jansson was given. +if test "${enable_jansson+set}" = set; then : + enableval=$enable_jansson; enable_jansson=$enableval +else + enable_jansson="no" +fi + + +if test "x$enable_jansson" != "xno"; then + + + + + + + +if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then + if test -n "$ac_tool_prefix"; then + # Extract the first word of "${ac_tool_prefix}pkg-config", so it can be a program name with args. +set dummy ${ac_tool_prefix}pkg-config; ac_word=$2 +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 +$as_echo_n "checking for $ac_word... " >&6; } +if ${ac_cv_path_PKG_CONFIG+:} false; then : + $as_echo_n "(cached) " >&6 +else + case $PKG_CONFIG in + [\\/]* | ?:[\\/]*) + ac_cv_path_PKG_CONFIG="$PKG_CONFIG" # Let the user override the test with a path. + ;; + *) + as_save_IFS=$IFS; IFS=$PATH_SEPARATOR +for as_dir in $PATH +do + IFS=$as_save_IFS + test -z "$as_dir" && as_dir=. + for ac_exec_ext in '' $ac_executable_extensions; do + if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then + ac_cv_path_PKG_CONFIG="$as_dir/$ac_word$ac_exec_ext" + $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 + break 2 + fi +done + done +IFS=$as_save_IFS + + ;; +esac +fi +PKG_CONFIG=$ac_cv_path_PKG_CONFIG +if test -n "$PKG_CONFIG"; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $PKG_CONFIG" >&5 +$as_echo "$PKG_CONFIG" >&6; } +else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } +fi + + +fi +if test -z "$ac_cv_path_PKG_CONFIG"; then + ac_pt_PKG_CONFIG=$PKG_CONFIG + # Extract the first word of "pkg-config", so it can be a program name with args. +set dummy pkg-config; ac_word=$2 +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 +$as_echo_n "checking for $ac_word... " >&6; } +if ${ac_cv_path_ac_pt_PKG_CONFIG+:} false; then : + $as_echo_n "(cached) " >&6 +else + case $ac_pt_PKG_CONFIG in + [\\/]* | ?:[\\/]*) + ac_cv_path_ac_pt_PKG_CONFIG="$ac_pt_PKG_CONFIG" # Let the user override the test with a path. + ;; + *) + as_save_IFS=$IFS; IFS=$PATH_SEPARATOR +for as_dir in $PATH +do + IFS=$as_save_IFS + test -z "$as_dir" && as_dir=. + for ac_exec_ext in '' $ac_executable_extensions; do + if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then + ac_cv_path_ac_pt_PKG_CONFIG="$as_dir/$ac_word$ac_exec_ext" + $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 + break 2 + fi +done + done +IFS=$as_save_IFS + + ;; +esac +fi +ac_pt_PKG_CONFIG=$ac_cv_path_ac_pt_PKG_CONFIG +if test -n "$ac_pt_PKG_CONFIG"; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_pt_PKG_CONFIG" >&5 +$as_echo "$ac_pt_PKG_CONFIG" >&6; } +else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } +fi + + if test "x$ac_pt_PKG_CONFIG" = x; then + PKG_CONFIG="" + else + case $cross_compiling:$ac_tool_warned in +yes:) +{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 +$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} +ac_tool_warned=yes ;; +esac + PKG_CONFIG=$ac_pt_PKG_CONFIG + fi +else + PKG_CONFIG="$ac_cv_path_PKG_CONFIG" +fi + +fi +if test -n "$PKG_CONFIG"; then + _pkg_min_version=0.9.0 + { $as_echo "$as_me:${as_lineno-$LINENO}: checking pkg-config is at least version $_pkg_min_version" >&5 +$as_echo_n "checking pkg-config is at least version $_pkg_min_version... " >&6; } + if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + PKG_CONFIG="" + fi +fi + if test -n "$PKG_CONFIG"; then : + + +pkg_failed=no +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for jansson" >&5 +$as_echo_n "checking for jansson... " >&6; } + +if test -n "$JANSSON_CFLAGS"; then + pkg_cv_JANSSON_CFLAGS="$JANSSON_CFLAGS" + elif test -n "$PKG_CONFIG"; then + if test -n "$PKG_CONFIG" && \ + { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"jansson\""; } >&5 + ($PKG_CONFIG --exists --print-errors "jansson") 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; then + pkg_cv_JANSSON_CFLAGS=`$PKG_CONFIG --cflags "jansson" 2>/dev/null` + test "x$?" != "x0" && pkg_failed=yes +else + pkg_failed=yes +fi + else + pkg_failed=untried +fi +if test -n "$JANSSON_LIBS"; then + pkg_cv_JANSSON_LIBS="$JANSSON_LIBS" + elif test -n "$PKG_CONFIG"; then + if test -n "$PKG_CONFIG" && \ + { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"jansson\""; } >&5 + ($PKG_CONFIG --exists --print-errors "jansson") 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; then + pkg_cv_JANSSON_LIBS=`$PKG_CONFIG --libs "jansson" 2>/dev/null` + test "x$?" != "x0" && pkg_failed=yes +else + pkg_failed=yes +fi + else + pkg_failed=untried +fi + +if test $pkg_failed = no; then + pkg_save_LDFLAGS="$LDFLAGS" + LDFLAGS="$LDFLAGS $pkg_cv_JANSSON_LIBS" + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +int +main () +{ + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO"; then : + +else + pkg_failed=yes +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext + LDFLAGS=$pkg_save_LDFLAGS +fi + + + +if test $pkg_failed = yes; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then + _pkg_short_errors_supported=yes +else + _pkg_short_errors_supported=no +fi + if test $_pkg_short_errors_supported = yes; then + JANSSON_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "jansson" 2>&1` + else + JANSSON_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "jansson" 2>&1` + fi + # Put the nasty error message in config.log where it belongs + echo "$JANSSON_PKG_ERRORS" >&5 + + + as_fn_error $? "Cannot find jansson library" "$LINENO" 5 + +elif test $pkg_failed = untried; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + + as_fn_error $? "Cannot find jansson library" "$LINENO" 5 + +else + JANSSON_CFLAGS=$pkg_cv_JANSSON_CFLAGS + JANSSON_LIBS=$pkg_cv_JANSSON_LIBS + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + + +$as_echo "#define HAVE_JANSSON 1" >>confdefs.h + + + + +fi + +else + + as_fn_error $? "Cannot find pkg-config" "$LINENO" 5 + +fi +fi + ac_fn_c_check_decl "$LINENO" "basename" "ac_cv_have_decl_basename" "$ac_includes_default" if test "x$ac_cv_have_decl_basename" = xyes; then : ac_have_decl=1 diff --git a/gold/configure.ac b/gold/configure.ac index 4f432809b37..25fae6b998b 100644 --- a/gold/configure.ac +++ b/gold/configure.ac @@ -591,6 +591,32 @@ if test "$threads" = "yes"; then fi AM_CONDITIONAL(THREADS, test "$threads" = "yes") +# Used to validate --package-metadata= input. Disabled by default. +AC_ARG_ENABLE([jansson], + [AS_HELP_STRING([--enable-jansson], + [enable jansson [default=no]])], + [enable_jansson=$enableval], + [enable_jansson="no"]) + +if test "x$enable_jansson" != "xno"; then + PKG_PROG_PKG_CONFIG + AS_IF([test -n "$PKG_CONFIG"], + [ + PKG_CHECK_MODULES(JANSSON, [jansson], + [ + AC_DEFINE(HAVE_JANSSON, 1, [The jansson library is to be used]) + AC_SUBST([JANSSON_CFLAGS]) + AC_SUBST([JANSSON_LIBS]) + ], + [ + AC_MSG_ERROR([Cannot find jansson library]) + ]) + ], + [ + AC_MSG_ERROR([Cannot find pkg-config]) + ]) +fi + dnl We have to check these in C, not C++, because autoconf generates dnl tests which have no type information, and current glibc provides dnl multiple declarations of functions like basename when compiling diff --git a/gold/layout.cc b/gold/layout.cc index 3efe8d98ae9..184caa2c8ad 100644 --- a/gold/layout.cc +++ b/gold/layout.cc @@ -38,6 +38,9 @@ #include #include #endif +#ifdef HAVE_JANSSON +#include +#endif #include "parameters.h" #include "options.h" @@ -2437,6 +2440,7 @@ Layout::create_notes() this->create_gold_note(); this->create_stack_segment(); this->create_build_id(); + this->create_package_metadata(); } // Create the dynamic sections which are needed before we read the @@ -3534,6 +3538,52 @@ Layout::create_build_id() } } +// If --package-metadata was used, set up the package metadata note. +// https://systemd.io/ELF_PACKAGE_METADATA/ + +void +Layout::create_package_metadata() +{ + if (!parameters->options().user_set_package_metadata()) + return; + + const char* desc = parameters->options().package_metadata(); + if (strcmp(desc, "") == 0) + return; + +#ifdef HAVE_JANSSON + json_error_t json_error; + json_t *json = json_loads(desc, 0, &json_error); + if (json) + json_decref(json); + else + { + gold_fatal(_("error: --package-metadata=%s does not contain valid " + "JSON: %s\n"), + desc, json_error.text); + } +#endif + + // Create the note. + size_t trailing_padding; + // Ensure the trailing NULL byte is always included, as per specification. + size_t descsz = strlen(desc) + 1; + Output_section* os = this->create_note("FDO", elfcpp::FDO_PACKAGING_METADATA, + ".note.package", descsz, true, + &trailing_padding); + if (os == NULL) + return; + + Output_section_data* posd = new Output_data_const(desc, descsz, 4); + os->add_output_section_data(posd); + + if (trailing_padding != 0) + { + posd = new Output_data_zero_fill(trailing_padding, 0); + os->add_output_section_data(posd); + } +} + // If we have both .stabXX and .stabXXstr sections, then the sh_link // field of the former should point to the latter. I'm not sure who // started this, but the GNU linker does it, and some tools depend diff --git a/gold/layout.h b/gold/layout.h index 98c03b93f5f..ee4f78c3c78 100644 --- a/gold/layout.h +++ b/gold/layout.h @@ -1107,6 +1107,10 @@ class Layout void create_build_id(); + // Create a package metadata note if needed. + void + create_package_metadata(); + // Link .stab and .stabstr sections. void link_stabs_sections(); @@ -1453,6 +1457,8 @@ class Layout Gdb_index* gdb_index_data_; // The space for the build ID checksum if there is one. Output_section_data* build_id_note_; + // The space for the package metadata JSON if there is one. + Output_section_data* package_metadata_note_; // The output section containing dwarf abbreviations Output_reduced_debug_abbrev_section* debug_abbrev_; // The output section containing the dwarf debug info tree diff --git a/gold/options.h b/gold/options.h index 9509a445e8e..17236eb9cb9 100644 --- a/gold/options.h +++ b/gold/options.h @@ -1102,6 +1102,10 @@ class General_options DEFINE_bool(p, options::ONE_DASH, 'p', false, N_("Ignored for ARM compatibility"), NULL); + DEFINE_optional_string(package_metadata, options::TWO_DASHES, '\0', NULL, + N_("Generate package metadata note"), + N_("[=JSON]")); + DEFINE_bool(pie, options::ONE_DASH, '\0', false, N_("Create a position independent executable"), N_("Do not create a position independent executable")); diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am index 38e54818f48..b15000ee7f3 100644 --- a/gold/testsuite/Makefile.am +++ b/gold/testsuite/Makefile.am @@ -149,25 +149,25 @@ check_PROGRAMS += object_unittest object_unittest_SOURCES = object_unittest.cc object_unittest_LDFLAGS = $(THREADFLAGS) object_unittest_LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \ - $(THREADLIBS) $(LIBDL) $(ZLIB) + $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) check_PROGRAMS += binary_unittest binary_unittest_SOURCES = binary_unittest.cc binary_unittest_LDFLAGS = $(THREADFLAGS) binary_unittest_LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \ - $(THREADLIBS) $(LIBDL) $(ZLIB) + $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) check_PROGRAMS += leb128_unittest leb128_unittest_SOURCES = leb128_unittest.cc leb128_unittest_LDFLAGS = $(THREADFLAGS) leb128_unittest_LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \ - $(THREADLIBS) $(LIBDL) $(ZLIB) + $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) check_PROGRAMS += overflow_unittest overflow_unittest_SOURCES = overflow_unittest.cc overflow_unittest_LDFLAGS = $(THREADFLAGS) overflow_unittest_LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \ - $(THREADLIBS) $(LIBDL) $(ZLIB) + $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) overflow_unittest.o: overflow_unittest.cc $(CXXCOMPILE) -O3 -c -o $@ $< @@ -4435,3 +4435,10 @@ retain_2.o: retain_2.s $(TEST_AS) -o $@ $< endif DEFAULT_TARGET_X86_64 + +check_PROGRAMS += package_metadata_test +package_metadata_test.o: package_metadata_main.c + $(COMPILE) -c -o $@ $< +package_metadata_test$(EXEEXT): package_metadata_test.o gcctestdir/ld + $(CXXLINK) package_metadata_test.o -Wl,--package-metadata='{"foo":"bar"}' + $(TEST_READELF) --notes $@ | grep -q '{"foo":"bar"}' diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in index 7b4b7832d38..78bb0cc7a26 100644 --- a/gold/testsuite/Makefile.in +++ b/gold/testsuite/Makefile.in @@ -109,7 +109,8 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \ $(am__EXEEXT_37) $(am__EXEEXT_38) $(am__EXEEXT_39) \ $(am__EXEEXT_40) $(am__EXEEXT_41) $(am__EXEEXT_42) \ $(am__EXEEXT_43) $(am__EXEEXT_44) $(am__EXEEXT_45) \ - $(am__EXEEXT_46) $(am__EXEEXT_47) + $(am__EXEEXT_46) $(am__EXEEXT_47) \ + package_metadata_test$(EXEEXT) @NATIVE_OR_CROSS_LINKER_TRUE@am__append_1 = object_unittest \ @NATIVE_OR_CROSS_LINKER_TRUE@ binary_unittest leb128_unittest \ @NATIVE_OR_CROSS_LINKER_TRUE@ overflow_unittest @@ -1146,6 +1147,7 @@ am__aclocal_m4_deps = $(top_srcdir)/../config/ax_pthread.m4 \ $(top_srcdir)/../config/lead-dot.m4 \ $(top_srcdir)/../config/nls.m4 \ $(top_srcdir)/../config/override.m4 \ + $(top_srcdir)/../config/pkg.m4 \ $(top_srcdir)/../config/plugins.m4 \ $(top_srcdir)/../config/po.m4 \ $(top_srcdir)/../config/progtest.m4 \ @@ -1407,6 +1409,7 @@ am__DEPENDENCIES_1 = @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ +@NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) binary_unittest_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \ $(binary_unittest_LDFLAGS) $(LDFLAGS) -o $@ @@ -1732,6 +1735,7 @@ leb128_unittest_OBJECTS = $(am_leb128_unittest_OBJECTS) @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ +@NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) leb128_unittest_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \ $(leb128_unittest_LDFLAGS) $(LDFLAGS) -o $@ @@ -1755,6 +1759,7 @@ object_unittest_OBJECTS = $(am_object_unittest_OBJECTS) @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ +@NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) object_unittest_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \ $(object_unittest_LDFLAGS) $(LDFLAGS) -o $@ @@ -1767,9 +1772,13 @@ overflow_unittest_OBJECTS = $(am_overflow_unittest_OBJECTS) @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ +@NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) \ @NATIVE_OR_CROSS_LINKER_TRUE@ $(am__DEPENDENCIES_1) overflow_unittest_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \ $(overflow_unittest_LDFLAGS) $(LDFLAGS) -o $@ +package_metadata_test_SOURCES = package_metadata_test.c +package_metadata_test_OBJECTS = package_metadata_test.$(OBJEXT) +package_metadata_test_LDADD = $(LDADD) permission_test_SOURCES = permission_test.c permission_test_OBJECTS = permission_test.$(OBJEXT) permission_test_LDADD = $(LDADD) @@ -2313,13 +2322,14 @@ SOURCES = $(libgoldtest_a_SOURCES) $(aarch64_pr23870_SOURCES) \ $(large_symbol_alignment_SOURCES) $(leb128_unittest_SOURCES) \ local_labels_test.c many_sections_r_test.c \ $(many_sections_test_SOURCES) $(object_unittest_SOURCES) \ - $(overflow_unittest_SOURCES) permission_test.c \ - $(pie_copyrelocs_test_SOURCES) plugin_test_1.c \ - plugin_test_10.c plugin_test_11.c plugin_test_12.c \ - plugin_test_2.c plugin_test_3.c plugin_test_4.c \ - plugin_test_5.c plugin_test_6.c plugin_test_7.c \ - plugin_test_8.c plugin_test_defsym.c plugin_test_start_lib.c \ - plugin_test_tls.c plugin_test_wrap_symbols.c pr17704a_test.c \ + $(overflow_unittest_SOURCES) package_metadata_test.c \ + permission_test.c $(pie_copyrelocs_test_SOURCES) \ + plugin_test_1.c plugin_test_10.c plugin_test_11.c \ + plugin_test_12.c plugin_test_2.c plugin_test_3.c \ + plugin_test_4.c plugin_test_5.c plugin_test_6.c \ + plugin_test_7.c plugin_test_8.c plugin_test_defsym.c \ + plugin_test_start_lib.c plugin_test_tls.c \ + plugin_test_wrap_symbols.c pr17704a_test.c \ $(pr20216a_test_SOURCES) $(pr20216b_test_SOURCES) \ $(pr20216c_test_SOURCES) $(pr20216d_test_SOURCES) \ $(pr20216e_test_SOURCES) $(pr20308a_test_SOURCES) \ @@ -2635,6 +2645,8 @@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_SCRIPT = @INSTALL_SCRIPT@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ INSTOBJEXT = @INSTOBJEXT@ +JANSSON_CFLAGS = @JANSSON_CFLAGS@ +JANSSON_LIBS = @JANSSON_LIBS@ LDFLAGS = @LDFLAGS@ LFS_CFLAGS = @LFS_CFLAGS@ LIBINTL = @LIBINTL@ @@ -2661,6 +2673,9 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_URL = @PACKAGE_URL@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PKG_CONFIG = @PKG_CONFIG@ +PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ +PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ POSUB = @POSUB@ PTHREAD_CC = @PTHREAD_CC@ PTHREAD_CFLAGS = @PTHREAD_CFLAGS@ @@ -2866,22 +2881,22 @@ DEPENDENCIES = \ @NATIVE_OR_CROSS_LINKER_TRUE@object_unittest_SOURCES = object_unittest.cc @NATIVE_OR_CROSS_LINKER_TRUE@object_unittest_LDFLAGS = $(THREADFLAGS) @NATIVE_OR_CROSS_LINKER_TRUE@object_unittest_LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \ -@NATIVE_OR_CROSS_LINKER_TRUE@ $(THREADLIBS) $(LIBDL) $(ZLIB) +@NATIVE_OR_CROSS_LINKER_TRUE@ $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) @NATIVE_OR_CROSS_LINKER_TRUE@binary_unittest_SOURCES = binary_unittest.cc @NATIVE_OR_CROSS_LINKER_TRUE@binary_unittest_LDFLAGS = $(THREADFLAGS) @NATIVE_OR_CROSS_LINKER_TRUE@binary_unittest_LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \ -@NATIVE_OR_CROSS_LINKER_TRUE@ $(THREADLIBS) $(LIBDL) $(ZLIB) +@NATIVE_OR_CROSS_LINKER_TRUE@ $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) @NATIVE_OR_CROSS_LINKER_TRUE@leb128_unittest_SOURCES = leb128_unittest.cc @NATIVE_OR_CROSS_LINKER_TRUE@leb128_unittest_LDFLAGS = $(THREADFLAGS) @NATIVE_OR_CROSS_LINKER_TRUE@leb128_unittest_LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \ -@NATIVE_OR_CROSS_LINKER_TRUE@ $(THREADLIBS) $(LIBDL) $(ZLIB) +@NATIVE_OR_CROSS_LINKER_TRUE@ $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) @NATIVE_OR_CROSS_LINKER_TRUE@overflow_unittest_SOURCES = overflow_unittest.cc @NATIVE_OR_CROSS_LINKER_TRUE@overflow_unittest_LDFLAGS = $(THREADFLAGS) @NATIVE_OR_CROSS_LINKER_TRUE@overflow_unittest_LDADD = libgoldtest.a ../libgold.a ../../libiberty/libiberty.a $(LIBINTL) \ -@NATIVE_OR_CROSS_LINKER_TRUE@ $(THREADLIBS) $(LIBDL) $(ZLIB) +@NATIVE_OR_CROSS_LINKER_TRUE@ $(THREADLIBS) $(LIBDL) $(ZLIB) $(JANSSON_LIBS) @GCC_TRUE@@NATIVE_LINKER_TRUE@large_symbol_alignment_SOURCES = large_symbol_alignment.cc @GCC_TRUE@@NATIVE_LINKER_TRUE@large_symbol_alignment_DEPENDENCIES = gcctestdir/ld @@ -4828,6 +4843,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/many_sections_test.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/object_unittest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/overflow_unittest.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/package_metadata_test.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/permission_test.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pie_copyrelocs_test-pie_copyrelocs_test.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/plugin_test_1.Po@am__quote@ @@ -7775,6 +7791,13 @@ aarch64_pr23870.log: aarch64_pr23870$(EXEEXT) --log-file $$b.log --trs-file $$b.trs \ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ "$$tst" $(AM_TESTS_FD_REDIRECT) +package_metadata_test.log: package_metadata_test$(EXEEXT) + @p='package_metadata_test$(EXEEXT)'; \ + b='package_metadata_test'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) .test.log: @p='$<'; \ $(am__set_b); \ @@ -10414,6 +10437,11 @@ uninstall-am: @DEFAULT_TARGET_X86_64_TRUE@ ../ld-new -pie -e _start --gc-sections -o $@ retain_2.o @DEFAULT_TARGET_X86_64_TRUE@retain_2.o: retain_2.s @DEFAULT_TARGET_X86_64_TRUE@ $(TEST_AS) -o $@ $< +package_metadata_test.o: package_metadata_main.c + $(COMPILE) -c -o $@ $< +package_metadata_test$(EXEEXT): package_metadata_test.o gcctestdir/ld + $(CXXLINK) package_metadata_test.o -Wl,--package-metadata='{"foo":"bar"}' + $(TEST_READELF) --notes $@ | grep -q '{"foo":"bar"}' # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. diff --git a/gold/testsuite/package_metadata_main.c b/gold/testsuite/package_metadata_main.c new file mode 100644 index 00000000000..77bc677e8eb --- /dev/null +++ b/gold/testsuite/package_metadata_main.c @@ -0,0 +1,5 @@ +int +main(void) +{ + return 0; +}