diff --git a/.fmf/version b/.fmf/version new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/.fmf/version @@ -0,0 +1 @@ +1 diff --git a/.gitignore b/.gitignore index 95c2ed8..2e1918a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /package-notes-0.4.tar.gz +/redhat-package-notes diff --git a/changelog b/changelog new file mode 100644 index 0000000..5545385 --- /dev/null +++ b/changelog @@ -0,0 +1,57 @@ +* Tue Aug 16 2022 Zbigniew Jędrzejewski-Szmek - 0.5-3 +- Switch to --package-metadata also with lld (#2051597) + +* Tue Aug 09 2022 Luca Boccassi - 0.5-1 +- Switch to --package-metadata (#2055863, #2083878, #2099999, #2059858, #2055458) + +* Sat Aug 06 2022 Tom Stellard - 0.4-17 +- Add CI test + +* Fri Jul 22 2022 Fedora Release Engineering - 0.4-16 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_37_Mass_Rebuild + +* Sat Mar 26 2022 Luca Boccassi - 0.4-15 +- Fix wrong binary fields on big endian + +* Tue Jan 25 2022 Zbigniew Jędrzejewski-Szmek - 0.4-14 +- Disable notes when clang toolchain is used on arm + +* Mon Jan 24 2022 Zbigniew Jędrzejewski-Szmek - 0.4-13 +- Disable notes with linkers other than bfd + +* Mon Jan 24 2022 Zbigniew Jędrzejewski-Szmek - 0.4-12 +- Add --insert-after param to the note generation script + +* Sat Jan 22 2022 Zbigniew Jędrzejewski-Szmek - 0.4-11 +- Add %%_package_note_linker and document everything (rhbz#2043178, + rhbz#2043368) + +* Sat Jan 22 2022 Zbigniew Jędrzejewski-Szmek - 0.4-10 +- Allow unsetting %%_package_note_readonly to drop the READONLY attribute + +* Fri Jan 21 2022 Zbigniew Jędrzejewski-Szmek - 0.4-9 +- Use %%{buildsubdir} in %%_package_note_file if defined + +* Fri Jan 21 2022 Zbigniew Jędrzejewski-Szmek - 0.4-8 +- Also voidify the macros if we're on a noarch build + +* Fri Jan 21 2022 Zbigniew Jędrzejewski-Szmek - 0.4-7 +- Use $RPM_PACKAGE_VERSION variable to refer to the package version + +* Fri Jan 21 2022 Zbigniew Jędrzejewski-Szmek - 0.4-6 +- Make _generate_package_note_file always recreate the file + +* Fri Jan 21 2022 Zbigniew Jędrzejewski-Szmek - 0.4-5 +- Conditionalize all macros on %%_package_note_file being defined + +* Thu Jan 20 2022 Fedora Release Engineering - 0.4-4 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_36_Mass_Rebuild + +* Fri Jan 14 2022 Zbigniew Jędrzejewski-Szmek - 0.4-3 +- Rename srpm macros file + +* Fri Jan 14 2022 Zbigniew Jędrzejewski-Szmek - 0.4-2 +- Add package-notes-srpm-macros subpackage to configure rpm builds + +* Tue Nov 16 2021 Zbigniew Jędrzejewski-Szmek - 0.4-1 +- Version 0.4 diff --git a/generate-rpm-note.sh b/generate-rpm-note.sh deleted file mode 100755 index 08e9a25..0000000 --- a/generate-rpm-note.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/bin/bash - -readonly=1 -insert_after=1 - -pad_string() { - for _ in $(seq "$1"); do - printf ' BYTE(0x00)' - done -} - -write_string() { - text="$1" - prefix="$2" - label="$3" - total="$4" - - printf "%s/* %s: '%s' */" "$prefix" "$label" "$text" - for i in $(seq ${#text}); do - if (( i % 4 == 1 )); then - printf '\n%s' "$prefix" - else - printf ' ' - fi - printf 'BYTE(0x%02x)' "'${text:i-1:1}" - done - - pad_string $(( total - ${#text} )) - printf '\n' -} - -write_script() { - value_len=$(( (${#1} + 3) / 4 * 4 )) - [ -n "$readonly" ] && readonly_attr='(READONLY) ' - - printf 'SECTIONS\n{\n' - printf ' .note.package %s: ALIGN(4) {\n' "$readonly_attr" - printf ' BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of Owner including NUL */\n' - printf ' BYTE(0x%02x) BYTE(0x%02x) BYTE(0x00) BYTE(0x00) /* Length of Value including NUL */\n' \ - $((value_len % 256)) $((value_len / 256)) - - printf ' BYTE(0x7e) BYTE(0x1a) BYTE(0xfe) BYTE(0xca) /* Note ID */\n' - printf " BYTE(0x46) BYTE(0x44) BYTE(0x4f) BYTE(0x00) /* Owner: 'FDO' */\n" - - write_string "$1" ' ' 'Value' "$value_len" - - printf ' }\n}\n' - [ -n "$insert_after" ] && printf 'INSERT AFTER .note.gnu.build-id;\n' - : -} - -if [ "$1" == "--readonly=no" ]; then - shift - readonly= -fi - -if [ "$1" == "--insert-after=no" ]; then - shift - insert_after= -fi - -cpe="$(cat /usr/lib/system-release-cpe)" -json="$(printf '{"type":"rpm","name":"%s","version":"%s","architecture":"%s","osCpe":"%s"}' "$1" "$2" "$3" "$cpe")" -write_script "$json" diff --git a/macros.package-notes-srpm b/macros.package-notes-srpm index 213d051..3724897 100644 --- a/macros.package-notes-srpm +++ b/macros.package-notes-srpm @@ -4,52 +4,26 @@ # See https://fedoraproject.org/wiki/Changes/Package_information_on_ELF_objects # for details. # -# To opt out of the use of this feature completely, include this in -# the spec file: +# To opt out of the use of this feature completely, include this in the spec +# file: # -# %undefine _package_note_file +# %undefine _package_note_flags # -# The other macros can be undefined too to replace parts of the -# functionality. If %_generate_package_note_file is undefined, the -# linker script will not be generated, but the link flags may still -# refer to it. This may be useful if the default generation method is -# insufficient and a different mechanism will be used to generate -# %_package_note_file. If %_package_note_flags is undefined, the -# linker argument that injects the script will not be added to -# %build_ldfags, but the linker script would still be generated. - -# The name of the file with the linker script. If %{buildsubdir} is -# defined, the file will be placed therein. Otherwise, one level up, -# directly in %{_builddir}. -# -# Note that %{version}-%{release} used here might be redefined from -# the "primary" values when subpackages with different version-release -# are specified. The contents of the script use the shell variable -# $RPM_PACKAGE_NAME, $RPM_PACKAGE_VERSION, $RPM_PACKAGE_RELEASE, -# and $RPM_ARCH that are set early and seem to always contain the "primary" -# values for the main package. -%_package_note_file %{_builddir}%{?buildsubdir:/%{buildsubdir}}/.package_note-%{name}-%{version}-%{release}.%{_arch}.ld - -# Which linker will be used? This should be either "bfd", "gold", or -# "lld". Unfortunately linkers other than bfd do not support some of -# the options that we'd like to use, so if this is set to anything -# other than "bfd", note insertion is disabled. +# Which linker will be used? This should be either "bfd", "gold", "mold", or "lld". # # (The default linker for clang on armv7hl is lld.) %_package_note_linker %["%_target_cpu" == "armv7hl" && "%{toolchain}" == "clang" ? "lld" : "bfd"] -# Whether to specify the READONLY attribute for the inserted -# section. We generally want this, but binutils <= 2.37 and other -# linkers do not support it. -%_package_note_readonly %["%_package_note_linker" == "bfd"?"1":"0"] +# These are defined for backwards compatibility. Do not use. +%_package_note_file 1 +%_generate_package_note_file %{nil} # Overall status: 1 if looks like we can insert the note, 0 otherwise -%_package_note_status %[0%{?_package_note_file:1} && 0%{?name:1} && "%_target_cpu" != "noarch" && "%_package_note_linker" == "bfd" ? 1 : 0] +# Unfortunately "clang" does not support specs files so the note insertion is disabled when using it. +%_package_note_status %{!?_package_note_flags:0}%{?_package_note_flags:%[0%{?_package_note_file:1} && 0%{?name:1} && "%_target_cpu" != "noarch" && "%{toolchain}" != "clang" ? 1 : 0]} -# The linker flags to be passed to the compiler to insert the notes section. -%_package_note_flags %[%_package_note_status?"-Wl,%["%_package_note_linker" != "lld"?"-dT":"-T"],%{_package_note_file}":""] -# The command to actually generate the linker script that inserts the -# notes file. This command is automatically used as part of the build -# preamble. -%_generate_package_note_file %[%_package_note_status?"if [ -f %{_rpmconfigdir}/generate-rpm-note.sh ]; then %{_rpmconfigdir}/generate-rpm-note.sh %[0%{?_package_note_readonly}?"":"--readonly=no "]${RPM_PACKAGE_NAME:?} ${RPM_PACKAGE_VERSION:?}-${RPM_PACKAGE_RELEASE:?} ${RPM_ARCH:?} >%{_package_note_file}; fi":""] +# The linker flags to be passed to the compiler to insert the notes section will +# be created by the spec file, to avoid issues with quoting and escaping across +# different build systems and shells. +%_package_note_flags %[%_package_note_status ? "-specs=/usr/lib/rpm/redhat/redhat-package-notes" : ""] diff --git a/package-notes.spec b/package-notes.spec index e7f26a1..dd0b59d 100644 --- a/package-notes.spec +++ b/package-notes.spec @@ -1,21 +1,14 @@ Name: package-notes -Version: 0.4 +Version: 0.5 Release: %autorelease -Summary: Generate a linker script to insert .note.package section +Summary: Generate LDFLAGS to insert .note.package section +License: 0BSD +URL: https://github.com/systemd/package-notes -%global forgeurl https://github.com/systemd/package-notes -%forgemeta - -License: CC0 -URL: %{forgeurl} -Source0: %{forgesource} - -Source1: generate-rpm-note.sh -Source2: macros.package-notes-srpm +Source0: redhat-package-notes.in +Source1: macros.package-notes-srpm BuildArch: noarch -BuildRequires: python3-devel -Requires: python3dist(simplejson) %description This package provides a generator of linker scripts that insert a section with @@ -24,32 +17,29 @@ for. %package srpm-macros Summary: %{summary} +Obsoletes: package-notes < 0.5 +# Those are minimum versions that implement --package-metadata +Conflicts: binutils < 2.37-34 +Conflicts: binutils-gold < 2.37-34 +Conflicts: mold < 1.3.0 +Conflicts: lld < 14.0.5-4 %description srpm-macros -RPM macros to inject a linker script into link flags and a helper to generate -a script that inserts a section with an ELF note with a JSON payload that -describes the package the binary was built for. +RPM macros to insert a section with an ELF note with a JSON payload that +describes the package the binary was built for via a compiler spec file. %prep -%autosetup - -%build # nothing to do +%build +sed "s|@OSCPE@|$(cat /usr/lib/system-release-cpe)|" %{SOURCE0} >redhat-package-notes + %install -install -Dt %{buildroot}%{_bindir}/ generate-package-notes -install -m0644 -Dt %{buildroot}%{_mandir}/man1/ debian/generate-package-notes.1 - -# A partial reimplementation without Python -install -Dt %{buildroot}%{_rpmconfigdir}/ %{SOURCE1} -install -m0644 -Dt %{buildroot}%{_rpmmacrodir}/ %{SOURCE2} - -%files -%{_bindir}/generate-package-notes -%{_mandir}/man1/generate-package-notes.1* +install -Dt %{buildroot}%{_rpmconfigdir}/redhat/ redhat-package-notes +install -m0644 -Dt %{buildroot}%{_rpmmacrodir}/ %{SOURCE1} %files srpm-macros -%{_rpmconfigdir}/generate-rpm-note.sh +%{_rpmconfigdir}/redhat/redhat-package-notes %{_rpmmacrodir}/macros.package-notes-srpm %changelog diff --git a/plans/basic.fmf b/plans/basic.fmf new file mode 100644 index 0000000..c1627f9 --- /dev/null +++ b/plans/basic.fmf @@ -0,0 +1,5 @@ +summary: Basic smoke test +discover: + how: fmf +execute: + how: tmt diff --git a/redhat-package-notes.in b/redhat-package-notes.in new file mode 100644 index 0000000..3a19b1b --- /dev/null +++ b/redhat-package-notes.in @@ -0,0 +1,2 @@ +*link: ++ --package-metadata={\"type\":\"rpm\",\"name\":\"%:getenv(RPM_PACKAGE_NAME \",\"version\":\"%:getenv(RPM_PACKAGE_VERSION -%:getenv(RPM_PACKAGE_RELEASE \",\"architecture\":\"%:getenv(RPM_ARCH \",\"osCpe\":\"@OSCPE@\"})))) diff --git a/sources b/sources index 180591a..e69de29 100644 --- a/sources +++ b/sources @@ -1 +0,0 @@ -SHA512 (package-notes-0.4.tar.gz) = 97a36e4125db99bca02e02eac5d2f823ae38e4c8a083a624f5f72f8d4a4c80fcfb24c8d15bbb922e0f28f5d6da97a87789a881a26aa02e2c1034c485a8866735 diff --git a/tests/build-gating.fmf b/tests/build-gating.fmf new file mode 100644 index 0000000..a40b6a8 --- /dev/null +++ b/tests/build-gating.fmf @@ -0,0 +1,4 @@ +discover: + how: fmf +exexute: + how: tmt diff --git a/tests/simple-rpm/main.fmf b/tests/simple-rpm/main.fmf new file mode 100644 index 0000000..25ad4f8 --- /dev/null +++ b/tests/simple-rpm/main.fmf @@ -0,0 +1,6 @@ +summary: Test build a simple RPM package to ensure package notes are working +require: + - dnf + - dnf-plugins-core + - rpm-build +test: ./runtest.sh diff --git a/tests/simple-rpm/runtest.sh b/tests/simple-rpm/runtest.sh new file mode 100755 index 0000000..d911a2b --- /dev/null +++ b/tests/simple-rpm/runtest.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -ex + +dnf -y build-dep test.spec +rpmbuild --define '_sourcedir .' --define '_builddir .' -bb test.spec +dnf -y build-dep test.spec -D "_with_ld_lld 1" +rpmbuild --with ld_lld --define '_sourcedir .' --define '_builddir .' -bb test.spec +dnf -y build-dep test.spec -D "_with_clang 1" +rpmbuild --with clang --define '_sourcedir .' --define '_builddir .' -bb test.spec diff --git a/tests/simple-rpm/test.spec b/tests/simple-rpm/test.spec new file mode 100644 index 0000000..155a352 --- /dev/null +++ b/tests/simple-rpm/test.spec @@ -0,0 +1,50 @@ +%bcond_with ld_lld +%bcond_with clang + +%if %{with ld_lld} +%global extra_ldflags -fuse-ld=lld +%global _package_note_linker lld +%endif +%if %{with clang} +%global toolchain clang +%endif + +Name: test +Version: 1 +Release: 1 +Summary: Test package for checking package notes +License: MIT + +%if %{with clang} +BuildRequires: clang +%else +BuildRequires: gcc +%endif +# For %check +BuildRequires: binutils +BuildRequires: jq +%if %{with ld_lld} +BuildRequires: lld +%endif + +%description +Test package for checking package notes + +%build +echo 'int main(int argc, char **argv) { return 0; }' | %{build_cc} ${CFLAGS} -x c -c - -o main.o +%{build_cc} -Werror ${LDFLAGS} %{?extra_ldflags} main.o -o main + +# Package notes not supported with clang, so the the build succeeding is enough. +%if %{without clang} +%check +# avoid any attempt to access the network by readelf +export DEBUGINFOD_URLS= + +readelf --notes ./main | sed -r -n 's/.*Packaging Metadata: (.*)/\1/p' | tee package-note.text + +test "`cat package-note.text | jq -r '[.type,.name,.version,.architecture]|join(" ")'`" == "rpm %{name} %{version}-%{release} %{_arch}" +%endif + +%changelog +* Sat Aug 6 2022 Jane Doe - 1-1 +- Dummy