Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf: fix parsing of notes after patchelf mangling #3111

Merged
merged 2 commits into from May 11, 2020

Conversation

cjp256
Copy link
Contributor

@cjp256 cjp256 commented May 8, 2020

We cannot rely on iterating on the notes via the note segment.
Patchelf may move note sections into other segments, and worse
yet, possibly leave the note segment standing with invalid data.
Here we instead iterate over each section and (more) reliably
gather the note information.

There is some improvement that should be done on the patchelf
side, as readelf complains on these binaries, and other issues
have surfaced in the past (e.g. mesa in classic snaps). However,
this will hopefully address snapcraft build-time issues with not
only our patched files for classic snaps, but externally patched
binaries that are being packaged.

Add a couple of developer logs to indicate when we are parsing
an ELF, so future investigations are easier when something explodes.

Signed-off-by: Chris Patterson chris.patterson@canonical.com

  • Have you followed the guidelines for contributing?
  • Have you signed the CLA?
  • Have you successfully run ./runtests.sh static?
  • Have you successfully run ./runtests.sh tests/unit?

We cannot rely on iterating on the notes via the note segment.
Patchelf may move note sections into other segments, and worse
yet, possibly leave the note segment standing with invalid data.
Here we instead iterate over each section and (more) reliably
gather the note information.

There is some improvement that should be done on the patchelf
side, as `readelf` complains on these binaries, and other issues
have surfaced in the past (e.g. mesa in classic snaps).  However,
this will hopefully address snapcraft build-time issues with not
only our patched files for classic snaps, but externally patched
binaries that are being packaged.

Add a couple of developer logs to indicate when we are parsing
an ELF, so future investigations are easier when something explodes.

Signed-off-by: Chris Patterson <chris.patterson@canonical.com>
@cjp256 cjp256 requested a review from jhenstridge May 8, 2020 14:02
@cjp256
Copy link
Contributor Author

cjp256 commented May 9, 2020

Fixes SNAPCRAFT-1K4, SNAPCRAFT-1ET

Copy link
Contributor

@jhenstridge jhenstridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I think we can get away with scanning a single section though, rather than all sections though (assuming tests pass with that change).

# Here we instead iterate over each section and (more) reliably
# gather the note information.
for section in elf.iter_sections():
if isinstance(section, elftools.elf.sections.NoteSection):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was curious about what the best canonical way of determining the build ID is. I think this is the relevant part of the GDB source code:

https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=bfd/opncls.c;h=99097a9e39989e377af70e9450a970b009439e70;hb=HEAD#l1797

It seems to use the following process:

  1. Find the section named .note.gnu.build-id
  2. Read the contents of the section (this presumably makes sure its not a SHT_NOBITS section).
  3. Look for a note with name "GNU" and type "NT_GNU_BUILD_ID". The description field of that note is the build ID.

So you are right to switch over to checking sections, but I think we can switch this to section = elf.get_section_by_name('.note.gnu-build-id'), and work from there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, will do! Thanks!

Signed-off-by: Chris Patterson <chris.patterson@canonical.com>
@sergiusens sergiusens merged commit 3247432 into master May 11, 2020
@sergiusens sergiusens added the bug Actual bad behavior that don't fall into maintenance or documentation label May 12, 2020
@cjp256 cjp256 deleted the fix-patchelf-mangling branch May 12, 2021 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Actual bad behavior that don't fall into maintenance or documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants