Skip to content

[ELD] Diagnose tar archives given as input files - #2061

Open
Ricardo Alves de Souza (ricardoasouz) wants to merge 1 commit into
qualcomm:mainfrom
ricardoasouz:fix/2042-tar-archive-input
Open

Ricardo Alves de Souza (ricardoasouz) wants to merge 1 commit into
qualcomm:mainfrom
ricardoasouz:fix/2042-tar-archive-input

Conversation

@ricardoasouz

Copy link
Copy Markdown
Contributor

Fixes #2042

Inputs that aren't ELF, bitcode or ar archives are parsed as linker scripts, so a tar file went through the script lexer, which printed a note with the raw header bytes for every NUL it hit before the parser gave up.

This adds InputTarReader::isTarArchive(), which checks the checksum of the first header block. That also catches v7 archives, which have no magic, along with ustar, GNU and pax. The check is at the top of the linker script branch in ObjectLinker::readAndProcessInput(), mirroring how executables without --just-symbols are rejected, so the file is never recorded or lexed as a script:

Error: a.tar: tar archives cannot be used as input files

Only inputs whose type ELD infers are affected (command line, INPUT(), GROUP()). Files passed with -T, INCLUDE, --version-script, --dynamic-list or --extern-list behave as before, and so does -b binary. I can extend the check to those options, or add a hint to the message (e.g. --replay for reproduce tarballs), if you think it's worth it.

GNU ld and lld don't catch this either, both parse the file as a script and fail with a parse error.

Tests: unit tests in TarReaderTests (ustar, pax, v7 and non-tar data) and a lit test in Common/standalone/Sniffing covering the reported case, INPUT(), GROUP() and -b binary. The lit test fails without the change, and check-eld-extended shows no new failures compared to main.

Input files that are not ELF, bitcode or ar archive files are parsed as
linker scripts. A tar archive therefore reached the script lexer, which
printed a note echoing the binary contents for each NUL byte before the
parser failed.

Recognize tar archives by the checksum of their first header block,
which covers v7 headers (they have no magic) as well as ustar, GNU and
pax ones. Check it before an input is handled as a linker script, so
the archive gets a single error and is never recorded or lexed as one:

  Error: a.tar: tar archives cannot be used as input files

Scripts named with -T, INCLUDE, --version-script, --dynamic-list or
--extern-list are not sniffed and are unaffected, as are inputs read
with -b binary.

Fixes qualcomm#2042

Signed-off-by: Ricardo Alves de Souza <ricardoasouz@hotmail.com>
Comment thread lib/Support/InputTarReader.cpp

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gibberish output when linker is input a tar file

2 participants