Commit Graph

108 Commits

Author SHA1 Message Date
David Gow
7ff7a80eee Add a test for --clear-symbol-version
This test removes the __libc_start_main@GLIBC_2.34 symbol, which should
be the only GLIBC_2.34 symbol in 'main' when built with recent (i.e., >
2.34) glibc. Because it is the only symbol, the entry in .gnu.version_r
should also be removed.

Because this is a symbol version test, it's unlikely to work on musl or
anything else which doesn't use glibc symbol versions. In these cases
(or in the case that __libc_start_main is now at a different version),
the test should print a warning, but exit with "0" to report a pass.
2022-07-30 22:58:29 +08:00
deadwood
b92e442be0 Add test for --add-debug-tag argument 2022-02-23 16:42:51 +01:00
Farid Zakaria
b92a6e8951 Fix replace-add-needed.sh test for musl 2021-12-21 14:30:45 -08:00
Farid Zakaria
d02a3ecdd7 Code clarity and test working with musl
Made changes according to feedback from @Mic92 such as moving the
`rewriteSections` inline into every method.

Improved `replace-add-needed.sh` to work with musl libc
2021-12-21 06:05:13 -08:00
Farid Zakaria
e268662c32 Add a unit test 2021-12-20 17:34:26 -08:00
Klemens Nanni
5f6cc74be8 Avoid GNU seq(1)
MacOS, FreeBSD and Linux using GNU have it, but at least OpenBSD
does not (the non-compatible BSD equivalent jot(1) exists, though).

Counting up is easy enough to do in POSIX sh(1).

Looks like this after GNU make(1)'s escaping and produces the same
big-dynstr.c file:
```
cat main.c > big-dynstr.c
i=1; while [ $i -le 2000 ]; do echo "void f$i(void) { };"; i=$(($i + 1)); done >> big-dynstr.c
```

This is the last bit required to build and pass all tests on OpenBSD
without local patches/dependencies.
2021-12-07 20:41:26 +01:00
Jörg Thalheim
a4027142bf tests/grow-file.sh: fix compat with non-gnu systems 2021-12-07 19:44:37 +01:00
Jörg Thalheim
ba000786e9 tests/too-many-strtab: fix linking with lld
llvm's lld also tests the section type and fails otherwise

fixes https://github.com/NixOS/patchelf/issues/350
2021-12-07 19:44:37 +01:00
Jörg Thalheim
44fea0de2f tests/endianess.sh: fix out-of-tree build 2021-12-07 17:05:01 +01:00
Jörg Thalheim
ce99c93c57 Merge pull request #353 from klemensn/out-of-tree-build
Use absoloute paths to linker scripts
2021-12-07 15:44:42 +00:00
Jörg Thalheim
301aa6ae34 fix build-id test on openbsd 2021-12-06 21:45:26 +01:00
Klemens Nanni
fd82dfffab Use absoloute paths to linker scripts
As is common with autotools enabled software, building and testing
happens outside the source directory.

The introduction of `contiguous-note-sections` test in 0.13 broke this
by assuming linker scripts to exist in the current working directory.

Point at the absoloute source directory explicitly to unbreak linking in
such build environments.

Spotted on OpenBSD where source and build assets are commonly separated
iff possible.
2021-12-06 21:25:25 +01:00
Klemens Nanni
b0cdf15e26 Use correct double-dashes for ld(1)'s build-id option
(Old) linkers such as BFD's ld(1) aka. GNU ld (version 2.17 on OpenBSD)
don't know `--build-id`, but they know `-b input-format` which patchelf
triggers by ommitting the second dash.

LLD's ld(1) seems to cope with this, although it's incorrect usage.

Use the double dash for correctness to get accurate error messages with
such incompatible linkers:

	-/usr/bin/ld: invalid BFD target `uild-id'
	+/usr/bin/ld: unrecognized option '--build-id'
	+/usr/bin/ld: use the --help option for usage information
2021-12-06 21:04:26 +01:00
Rolf Eike Beer
b4cb6cac78 fix corrupted library names when using --replace-needed multiple times
When it happens that the .gnu.version_r stores the strings in .dynstr it can
come to corruption of the library names written into DT_NEEDED:

-the library names in DT_NEEDED are replaced, new entries are written to the end
 of .dynstr
-the version library names are replaced, and written to the end of the string
 section.

If the section for the version strings is also ".dynstr", the previous
modifications were _not_ taken into account and things were written from the old
end of .dynstr again. The order in which these strings were written is not the
same as the previous replacement, so things would end up with the same size, but
different offsets. The .gnu.version_r table is correct, the file contents are
fine, but the offsets in the DT_NEEDED entries are wrong. Since they are printed
as 0-terminated strings the first one replaced will always be shown correct,
which also is the case if the argument is only used once as the string is
replaced with itself afterwards.
2021-11-27 11:06:49 +01:00
Domen Kožar
18a389ba35 Merge pull request #324 from ovpanait/strip
configure.ac: add check for strip
2021-09-26 11:54:19 -05:00
Ovidiu Panait
424d3f5083 tests: add testcase for PT_PHDR VirtAddr corruption
A PT_PHDR corruption was previously reported and fixed in [1]: the issue was
that the VirtAddr field of the PT_PHDR program header would get overwritten
with the file offset of the program header table rather than the virtual
address. A testcase for this was also added in [2]. However, the tescase is not
included in the Makefile.am regression testsuite and also tries to run a x86_64
prebuilt binary unconditionally, which would not work on other architectures.

To fix this, create a standalone testcase for the PT_PHDR VirtAddr field
corruption and include it in Makefile.am. In order to reproduce [1], a binary
with the following characteristics is needed:
- the ELF file type must be ET_DYN
- the ELF file must contain a PT_PHDR program header
- the file offset and the VirtAddr field of the PT_PHDR program header must be
  different

[1] https://github.com/NixOS/patchelf/pull/243
[2] 8f94e116f3

Signed-off-by: Ovidiu Panait <ovpanait@gmail.com>
2021-09-26 19:09:55 +03:00
Ovidiu Panait
512c0f3c60 configure.ac: add check for strip
Currently, "make check" fails on Ubuntu 20.04 because the STRIP variable is not
set in the resulting tests/Makefile:
"""
git clone https://github.com/NixOS/patchelf.git
./bootstrap.sh
./configure
make VERBOSE=1 check
...
gcc -fPIC -shared -o libbig-dynstr.so big-dynstr.c
only-keep-debug libbig-dynstr.so -o libbig-dynstr.debug
/bin/bash: only-keep-debug: command not found
make[2]: [Makefile:1526: libbig-dynstr.debug] Error 127 (ignored)
...
FAIL: no-dynamic-section.sh
===========================

patchelf: getting info about 'libbig-dynstr.debug': No such file or directory
FAIL no-dynamic-section.sh (exit status: 1)
"""

strip is used by the regression testsuite, so add a check for it configure.ac.

Signed-off-by: Ovidiu Panait <ovpanait@gmail.com>
2021-09-26 19:03:14 +03:00
Jörg Thalheim
90da38b8cf grow-test: fix on non-gcc systems 2021-09-13 17:55:05 +02:00
Andrew Laucius
a4ae03d638 New test for growing the file 2021-09-13 15:00:44 +00:00
Jörg Thalheim
de3901853d fix setting empty rpath 2021-09-12 10:27:01 +02:00
Jörg Thalheim
e9ccf62867 rename test: external.sh -> args-from-file.sh 2021-09-11 07:34:42 +02:00
Jörg Thalheim
742d772a7f also test if external file does not exists 2021-09-11 07:33:00 +02:00
Andrew Laucius
d69bafc3b3 Allow string arguments to be read from files.
If a string argument to an option is prefixed with @, then a file with the
following name will be read into that string.
2021-09-10 19:28:32 +00:00
Jörg Thalheim
51ecac17c2 add flag tests
let's start with the easy targets from coverage reports
2021-08-25 21:43:41 +00:00
Jörg Thalheim
a71334dc2e add failing test for .dynamic == NOBITS 2021-08-23 20:57:47 +02:00
Ivan A. Melnikov
b240bb8dcf Adjust DT_MIPS_RLD_MAP_REL dynamic section entry if present
`patchelf --set-rpath` corrupted executables on mips32el: the dynamic
liker crushed with Segmentation fault when loading any executable with
RPATH added that way.

The problem was around the MIPS-specific mechanism of setting up the
debug map pointer. When DT_MIPS_RLD_MAP_REL entry in the dynamic section
is present, it holds the relative address of __RLD_MAP -- an offset
relative to this dynamic section entry. Dynamic linker puts the
pointer to the `r_debug` structure there.

When patchelf updates the executable RPATH, it moves the .dynamic
section both in the binary and in memory, while __RLD_MAP is not moved
in memory, since it belongs to special .rld_map section that has type
PROGBITS. So, the offset stored in DT_MIPS_RLD_MAP_REL entry is not
valid anymore and should be updated.

This commit adds the necessary update.

In the corner case when DT_MIPS_RLD_MAP_REL is present, but
.rld_map section is not, the dynamic loader writes the debug
pointer to some arbitrary bytes in memory. To avoid crushes
on otherwise "working" binaries, we set offset to zero
so that the dynamic loader would just overwrite the dynamic
section.

Here we also import DT_MIPS_RLD_MAP_REL definition in elf.h form
glibc commit a2057c984e4314c3740f04cf54e36c824e4c8f32.

Refs: #82
Signed-off-by: Ivan A. Melnikov <iv@altlinux.org>
2021-08-11 17:49:41 +04:00
Jörg Thalheim
9e364e5fca tests/contiguous-note-sections: rename for consistency 2021-08-10 14:36:25 +02:00
Jörg Thalheim
2f88e6f312 Merge pull request #296 from Mic92/gnu-hash
fix binaries without .gnu.hash section
2021-08-10 11:57:19 +01:00
Jörg Thalheim
57fe1d3835 fix binaries without .gnu.hash section 2021-08-10 07:54:50 +02:00
Joerie de Gram
094022f06c tests/contiguous_note_sections.s: use alternate section type prefix
fixes #294
2021-08-09 19:16:47 +02:00
Domen Kožar
eed0a903c5 Merge branch 'master' into multiple 2021-08-05 17:59:01 +02:00
Domen Kožar
dab44118d7 Merge pull request #246 from xavierabellan/add-rpath
Added option --add-rpath
2021-08-05 12:10:59 +02:00
Domen Kožar
4e1b46a334 fix tests DIST 2021-08-05 12:03:39 +02:00
Domen Kožar
f823fa3143 Merge branch 'master' into endianness-fix 2021-08-05 11:55:53 +02:00
Ovidiu Panait
d2bb23badf tests: add testcase for alignment issues with contiguous note sections
Add a testcase for the following reported alignment issue with contiguous note
sections (#275):

"""
If a binary has multiple SHT_NOTE sections and corresponding PT_NOTE
headers, we can see the error:

patchelf: cannot normalize PT_NOTE segment: non-contiguous SHT_NOTE sections

if the SHT_NOTE sections aren't sized to end on aligned boundaries. An example
would be a binary with:

[ 2] .note.ABI-tag NOTE 00000000000002f4 000002f4
0000000000000020 0000000000000000 A 0 0 4
[ 3] .note.gnu.propert NOTE 0000000000000318 00000318
0000000000000030 0000000000000000 A 0 0 8
[ 4] .note.gnu.build-i NOTE 0000000000000348 00000348
0000000000000024 0000000000000000 A 0 0 4

NOTE 0x0000000000000318 0x0000000000000318 0x0000000000000318
0x0000000000000030 0x0000000000000030 R 0x8
NOTE 0x00000000000002f4 0x00000000000002f4 0x00000000000002f4
0x0000000000000078 0x0000000000000074 R 0x4

since the PT_NOTE section at 2f4 covers [2] and [3] but the code
calclates curr_off should be 314, not the 318 in the binary. This
is an alignment issue.
"""

Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
2021-08-04 12:31:15 +03:00
Domen Kožar
4d03718b17 Revert "tests: add testcase for alignment issues with contiguous note sections"
This reverts commit fccd901efe.
2021-08-03 16:30:13 +02:00
Domen Kožar
83fac7e0ce Merge pull request #289 from ovpanait/master
tests: add testcase for alignment issues with contiguous note sections
2021-08-03 15:53:42 +02:00
Ovidiu Panait
3300999317 tests: plain-needed.sh: add tescases for --add-needed-/--remove-needed
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
2021-07-24 19:25:16 +03:00
Joerie de Gram
4052850d22 tests: add e_machine endianness test (#269) 2021-07-24 01:32:37 +02:00
Ovidiu Panait
fccd901efe tests: add testcase for alignment issues with contiguous note sections
Add a testcase for the following reported alignment issue with contiguous note
sections (#275):

"""
If a binary has multiple SHT_NOTE sections and corresponding PT_NOTE
headers, we can see the error:

patchelf: cannot normalize PT_NOTE segment: non-contiguous SHT_NOTE sections

if the SHT_NOTE sections aren't sized to end on aligned boundaries. An example
would be a binary with:

[ 2] .note.ABI-tag NOTE 00000000000002f4 000002f4
0000000000000020 0000000000000000 A 0 0 4
[ 3] .note.gnu.propert NOTE 0000000000000318 00000318
0000000000000030 0000000000000000 A 0 0 8
[ 4] .note.gnu.build-i NOTE 0000000000000348 00000348
0000000000000024 0000000000000000 A 0 0 4

NOTE 0x0000000000000318 0x0000000000000318 0x0000000000000318
0x0000000000000030 0x0000000000000030 R 0x8
NOTE 0x00000000000002f4 0x00000000000002f4 0x00000000000002f4
0x0000000000000078 0x0000000000000074 R 0x4

since the PT_NOTE section at 2f4 covers [2] and [3] but the code
calclates curr_off should be 314, not the 318 in the binary. This
is an alignment issue.
"""

Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
2021-07-21 22:41:36 +03:00
Satadru Pramanik
8f94e116f3 add PR243 reproducer 2021-05-20 17:10:38 -04:00
Satadru Pramanik
d75ad358f4 Add testfile from PR270 2021-05-20 13:40:54 -04:00
Julian Stecklina
c105b1c837 Add test case with invalid phdr offset
Parsing this file results in patchelf segfaulting at:

Program received signal SIGSEGV, Segmentation fault.
std::vector<Elf32_Phdr, std::allocator<Elf32_Phdr> >::_M_realloc_insert<Elf32_Phdr const&> (this=0x7fffffff80a8, __position=..., __args=...)
    at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/vector.tcc:449
449		  _Alloc_traits::construct(this->_M_impl,
(gdb) bt
    at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/vector.tcc:449
    at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1195
    this=0x7fffffff8088, fileContents=...) at patchelf.cc:421
2020-11-15 16:46:53 +01:00
Julian Stecklina
de9fa4def4 Add test case with invalid section header sh_name
Parsing this file results in patchelf segfaulting at:

    at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/char_traits.h:335
    at /nix/store/h31cy7jm6g7cfqbhc5pm4rf9c53i3qfb-gcc-9.3.0/include/c++/9.3.0/bits/basic_string.h:527
    this=0x7fffffff8028, fileContents=...) at patchelf.cc:449
2020-11-15 16:46:53 +01:00
Julian Stecklina
e57205fec9 Add test case with non-terminated string table
Parsing this file results in patchelf triggering an assertion:

patchelf: patchelf.cc:442: ElfFile<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, unsigned long, unsigned long, Elf64_Dyn, Elf64_Sym, Elf64_Verneed, unsigned short>::ElfFile(FileContents) [Elf_Ehdr = Elf64_Ehdr, Elf_Phdr = Elf64_Phdr, Elf_Shdr = Elf64_Shdr, Elf_Addr = unsigned long, Elf_Off = unsigned long, Elf_Dyn = Elf64_Dyn, Elf_Sym = Elf64_Sym, Elf_Verneed = Elf64_Verneed, Elf_Versym = unsigned short]: Assertion `shstrtab[shstrtabSize - 1] == 0' failed.
Aborted (core dumped)
2020-11-15 16:46:53 +01:00
Julian Stecklina
62e39e05dc Add ELF test case with empty string table
Parsing this file results in patchelf triggering an assertion:

patchelf: patchelf.cc:439: ElfFile<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, unsigned long, unsigned long, Elf64_Dyn, Elf64_Sym, Elf64_Verneed, unsigned short>::ElfFile(FileContents) [Elf_Ehdr = Elf64_Ehdr, Elf_Phdr = Elf64_Phdr, Elf_Shdr = Elf64_Shdr, Elf_Addr = unsigned long, Elf_Off = unsigned long, Elf_Dyn = Elf64_Dyn, Elf_Sym = Elf64_Sym, Elf_Verneed = Elf64_Verneed, Elf_Versym = unsigned short]: Assertion `shstrtabSize > 0' failed.
Aborted (core dumped)
2020-11-15 16:46:53 +01:00
Julian Stecklina
1162de53b5 Add test case with invalid shrstab size
Parsing this file results in patchelf triggering an assertion:

patchelf: patchelf.cc:384: void checkPointer(const FileContents &, void *, unsigned int): Assertion `q >= contents->data() && q + size <= contents->data() + contents->size()' failed.
Aborted (core dumped)
2020-11-15 16:46:53 +01:00
Julian Stecklina
1457195875 Add ELF test case with invalid string table index
Parsing this file results in patchelf triggering an assertion:

patchelf: patchelf.cc:431: ElfFile<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, unsigned long, unsigned long, Elf64_Dyn, Elf64_Sym, Elf64_Verneed, unsigned short>::ElfFile(FileContents) [Elf_Ehdr = Elf64_Ehdr, Elf_Phdr = Elf64_Phdr, Elf_Shdr = Elf64_Shdr, Elf_Addr = unsigned long, Elf_Off = unsigned long, Elf_Dyn = Elf64_Dyn, Elf_Sym = Elf64_Sym, Elf_Verneed = Elf64_Verneed, Elf_Versym = unsigned short]: Assertion `shstrtabIndex < shdrs.size()' failed.
Program received signal SIGABRT, Aborted.
2020-11-15 16:46:53 +01:00
Xavier Abellan Ecija
055802e636 Added option --add-rpath 2020-10-05 17:06:15 +01:00
Xavier Abellan Ecija
62e8db4343 Added option --add-rpath 2020-10-05 16:59:13 +01:00