aboutsummaryrefslogtreecommitdiff
path: root/elf
AgeCommit message (Collapse)AuthorFilesLines
2025-04-09Handle abort call for -fexceptions callAdhemerval Zanella1-0/+5
clang might generate an abort call when cleanup functions (set by __attribute__ ((cleanup)) calls functions not marked as nothrow. We can mitigate by marking some internal functions as __THROW, but it is not possible for functions that issue used-provided callbacks (for instance pthread_once).
2025-04-09Enable --enable-fortify-source with clangAdhemerval Zanella1-0/+8
clang generates internal calls for some _chk symbol, so add internal aliases for them, and stub some with rtld-stubbed-symbols to avoid ld.so linker issues.
2025-04-09elf: Fix tunable handing with clangAdhemerval Zanella2-2/+12
Recent clang version optimizes some loops contructions to strlen [1], which might generate function calls when self-relocation is not already done (on tunable parsing). Use an out-of-line function with __attribute_optimization_barrier__ to avoid this. [1] https://github.com/llvm/llvm-project/pull/132572/commits/facd7dfc80d655fe49baf4bf27e144a4c890a149
2025-04-09Add fall-through between switch labels annotationsAdhemerval Zanella4-6/+6
The clang default to warning for missing fall-through and it does not support all comment-like annotation that gcc does. Use a proper attribute instead.
2025-04-09elf: Only defines _dl_tls_allocate_active for SHAREDAdhemerval Zanella1-0/+2
clang issues: dl-tls.c:108:1: error: unused function '_dl_tls_allocate_active' [-Werror,-Wunused-function] 108 | _dl_tls_allocate_active (void) | ^~~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
2025-04-09elf: Suppress clang -Wstring-plus-int on rtld.cAdhemerval Zanella1-0/+6
clang issues an warning adding 'const unsigned char' to a string does not append to the string, however it is exactly what code means here.
2025-04-09elf: Supress clang -Wsometimes-uninitialized on _dl_debug_initializeAdhemerval Zanella1-1/+11
clang issues an warning where variable 'r' is used uninitialized whenever 'if' condition is false. The _dl_debug_initialize is called for static case always with LM_ID_BASE (csu/libc-start.c and elf/dl-reloc-static-pie.c) and DL_NSS will be always larger than 1 for shared case.
2025-04-09Suppress -Wmaybe-uninitialized only for gccAdhemerval Zanella1-1/+1
The warning is not supported by clang.
2025-04-08elf: Extend glibc.rtld.execstack tunable to force executable stack (BZ 32653)Adhemerval Zanella8-9/+59
From the bug report [1], multiple programs still require to dlopen shared libraries with either missing PT_GNU_STACK or with the executable bit set. Although, in some cases, it seems to be a hard-craft assembly source without the required .note.GNU-stack marking (so the static linker is forced to set the stack executable if the ABI requires it), other cases seem that the library uses trampolines [2]. Unfortunately, READ_IMPLIES_EXEC is not an option since on some ABIs (x86_64), the kernel clears the bit, making it unsupported. To avoid reinstating the broken code that changes stack permission on dlopen (0ca8785a28), this patch extends the glibc.rtld.execstack tunable to allow an option to force an executable stack at the program startup. The tunable is a security issue because it defeats the PT_GNU_STACK hardening. It has the slight advantage of making it explicit by the caller, and, as for other tunables, this is disabled for setuid binaries. A tunable also allows us to eventually remove it, but from previous experiences, it would require some time. Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu. [1] https://sourceware.org/bugzilla/show_bug.cgi?id=32653 [2] https://github.com/conda-forge/ctng-compiler-activation-feedstock/issues/143 Reviewed-by: Sam James <sam@gentoo.org>
2025-03-31elf: Fix tst-origin build when toolchain defaults to --as-needed (BZ 32823)John David Anglin1-1/+2
Checked on aarch64-linux-gnu. Reviewed-by: Florian Weimer <fweimer@redhat.com>
2025-03-21elf: Use +nolink-deps to add make-only dependency for tst-originFlorian Weimer1-1/+4
The tst-origin test must link against liborigin-mod.so. Correct build order depends on a makefile rule dependency on $(objpfx)liborigin-mod.so. Use +nolink-deps to remvoe this dependency from the linker command line. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-03-21debug: Improve '%n' fortify detection (BZ 30932)Adhemerval Zanella3-0/+88
The 7bb8045ec0 path made the '%n' fortify check ignore EMFILE errors while trying to open /proc/self/maps, and this added a security issue where EMFILE can be attacker-controlled thus making it ineffective for some cases. The EMFILE failure is reinstated but with a different error message. Also, to improve the false positive of the hardening for the cases where no new files can be opened, the _dl_readonly_area now uses _dl_find_object to check if the memory area is within a writable ELF segment. The procfs method is still used as fallback. Checked on x86_64-linux-gnu and i686-linux-gnu. Reviewed-by: Arjun Shankar <arjun@redhat.com>
2025-03-21Remove eloop-threshold.hAdhemerval Zanella1-3/+2
On both Linux and Hurd the __eloop_threshold() is always a constant (40 and 32 respectively), so there is no need to always call __sysconf (_SC_SYMLOOP_MAX) for Linux case (!SYMLOOP_MAX). To avoid a name clash with gnulib, rename the new file min-eloop-threshold.h. Checked on x86_64-linux-gnu and with a build for x86_64-gnu. Reviewed-by: DJ Delorie <dj@redhat.com>
2025-03-18elf: Fix tst-origin make rulesAdhemerval Zanella1-10/+3
The tst-origin build can fail with: /usr/bin/ld: [...]libc.so: undefined reference to `__tunable_is_initialized@GLIBC_PRIVATE' Since the custom link invocation links against system glibc instead of the built one. The only requirement is to avoid liborigin.so linked with a full path, which is the default for --enable-hardcoded-path-in-tests. There is no need to use a custom rule. Checked on x86_64-linux-gnu.
2025-03-17elf: Fix tst-origin make rulesAdhemerval Zanella1-3/+7
Add tests-special before include Rules and compile liborigin.os with MODULE_NAME set to testsuite instead of libc.
2025-03-13elf: Canonicalize $ORIGIN in an explicit ld.so invocation [BZ 25263]Adhemerval Zanella6-0/+123
When an executable is invoked directly, we calculate $ORIGIN by calling readlink on /proc/self/exe, which the Linux kernel resolves to the target of any symlinks. However, if an executable is run through ld.so, we cannot use /proc/self/exe and instead use the path given as an argument. This leads to a different calculation of $ORIGIN, which is most notable in that it causes ldd to behave differently (e.g., by not finding a library) from directly running the program. To make the behavior consistent, take advantage of the fact that the kernel also resolves /proc/self/fd/ symlinks to the target of any symlinks in the same manner, so once we have opened the main executable in order to load it, replace the user-provided path with the result of calling readlink("/proc/self/fd/N"). (On non-Linux platforms this resolution does not happen and so no behavior change is needed.) The __fd_to_filename requires _fitoa_word and _itoa_word, which for 32-bits pulls a lot of definitions from _itoa.c (due _ITOA_NEEDED being defined). To simplify the build move the required function to a new file, _fitoa_word.c. Checked on x86_64-linux-gnu and i686-linux-gnu. Co-authored-by: Geoffrey Thomas <geofft@ldpreload.com> Reviewed-by: Geoffrey Thomas <geofft@ldpreload.com> Tested-by: Geoffrey Thomas <geofft@ldpreload.com>
2025-03-11elf: Test dlopen (NULL, RTLD_LAZY) from an ELF constructorFlorian Weimer5-0/+149
This call must not complete initialization of all shared objects in the global scope because the ELF constructor which makes the call likely has not finished initialization. Calling more constructors at this point would expose those to a partially constructed dependency. This completes the revert of commit 9897ced8e78db5d813166a7ccccfd5a ("elf: Run constructors on cyclic recursive dlopen (bug 31986)").
2025-03-07elf: Fix handling of symbol versions which hash to zero (bug 29190)Florian Weimer9-6/+185
This was found through code inspection. No application impact is known. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-03-07elf: Check if __attribute__ ((aligned (65536))) is supportedH.J. Lu1-4/+8
The BZ #32763 tests fail to build for MicroBlaze (which defines MAX_OFILE_ALIGNMENT to (32768*8) in GCC, so __attribute__ ((aligned (65536))) is unsupported). Add a configure-time check to enable BZ #32763 tests only if __attribute__ ((aligned (65536))) is supported. Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Sam James <sam@gentoo.org>
2025-03-06static-pie: Skip the empty PT_LOAD segment at offset 0 [BZ #32763]H.J. Lu4-1/+56
As shown in https://sourceware.org/bugzilla/show_bug.cgi?id=25237 linker may generate an empty PT_LOAD segments at offset 0: Elf file type is EXEC (Executable file) Entry point 0x4000e8 There are 3 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000 0x00000000000000f0 0x00000000000000f0 R E 0x1000 LOAD 0x0000000000000000 0x0000000000410000 0x0000000000410000 0x0000000000000000 0x0000000000b5dce8 RW 0x10000 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RW 0x10 Section to Segment mapping: Segment Sections... 00 .text 01 .bss 02 Skip the empty PT_LOAD segment at offset 0 to support such binaries. This fixes BZ #32763. Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Sam James <sam@gentoo.org>
2025-03-05Remove dl-procinfo.hAdhemerval Zanella7-7/+0
powerpc was the only architecture with arch-specific hooks for LD_SHOW_AUXV, and with the information moved to ld diagnostics there is no need to keep the _dl_procinfo hook. Checked with a build for all affected ABIs. Reviewed-by: Peter Bergner <bergner@linux.ibm.com>
2025-03-04Pass -Wl,--no-error-execstack for tests where -Wl,-z,execstack is used [PR32717]Sam James1-0/+6
When GNU Binutils is configured with --enable-error-execstack=yes, a handful of our tests which rely on -Wl,-z,execstack fail. Pass --Wl,--no-error-execstack to override the behaviour and get a warning instead. Bug: https://sourceware.org/PR32717 Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-02-24AArch64: Remove AARCH64_R macroWilco Dijkstra1-13/+0
Remove AArch64_R relocation macro. Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2025-02-13elf: Keep using minimal malloc after early DTV resize (bug 32412)Florian Weimer4-0/+117
If an auditor loads many TLS-using modules during startup, it is possible to trigger DTV resizing. Previously, the DTV was marked as allocated by the main malloc afterwards, even if the minimal malloc was still in use. With this change, _dl_resize_dtv marks the resized DTV as allocated with the minimal malloc. The new test reuses TLS-using modules from other auditing tests. Reviewed-by: DJ Delorie <dj@redhat.com>
2025-02-05elf: Build dl-tls.o with early startup symbol redirectionsFlorian Weimer1-0/+6
This is required when building for powerpc64le POWER8 with GCC 8 at least. Fixes commit cbd9fd236981717d3d4ee942986ea912e9707c32 ("Consolidate TLS block allocation for static binaries with ld.so"). Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-02-02elf: Use _dl_find_object instead of _dl_find_dso_for_object in dlopenFlorian Weimer1-2/+4
The _dl_find_object function uses a binary search and is faster if there are many objects.
2025-02-02elf: Add fast path to dlopen for fully-opened mapsFlorian Weimer1-5/+31
If the map is already fully open (has matching flags and its own scope allocated), it is not necessary to unprotected memory during dlopen.
2025-02-02elf: Determine the caller link map in _dl_openFlorian Weimer1-28/+19
No functional change expected. This is in preparation of adding a fast path to dlopen in case no link map changes are required.
2025-02-02elf: Merge __dl_libc_freemem into __rtld_libc_freeresFlorian Weimer2-107/+101
The functions serve very similar purposes. The advantage of __rtld_libc_freeres is that it is located within ld.so, so it is more natural to poke at link map internals there. This slightly regresses cleanup capabilities for statically linked binaries. If that becomes a problem, we should start calling __rtld_libc_freeres from __libc_freeres (perhaps after renaming it).
2025-02-02elf: Add l_soname accessor function for DT_SONAME valuesFlorian Weimer5-56/+35
It's not necessary to introduce temporaries because the compiler is able to evaluate l_soname just once in constracts like: l_soname (l) != NULL && strcmp (l_soname (l), LIBC_SO) != 0
2025-02-02elf: Split _dl_lookup_map, _dl_map_new_object from _dl_map_objectFlorian Weimer1-12/+29
So that they can eventually be called separately from dlopen.
2025-02-01elf: Do not add a copy of _dl_find_object to libc.soFlorian Weimer2-4/+3
This reduces code size and dependencies on ld.so internals from libc.so. Fixes commit f4c142bb9fe6b02c0af8cfca8a920091e2dba44b ("arm: Use _dl_find_object on __gnu_Unwind_Find_exidx (BZ 31405)"). Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-01-30ld.so: Decorate BSS mappingsPetr Malat1-0/+30
Decorate BSS mappings with [anon: glibc: .bss <file>], for example [anon: glibc: .bss /lib/libc.so.6]. The string ".bss" is already used by bionic so use the same, but add the filename as well. If the name would be longer than what the kernel allows, drop the directory part of the path. Refactor glibc.mem.decorate_maps check to a separate function and use it to avoid assembling a name, which would not be used later. Signed-off-by: Petr Malat <oss@malat.biz> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-01-25elf: fix 'valgrind' typo in commentSam James1-1/+1
2025-01-20elf: Make tst-ro-dynamic-mod.map compatible with older binutilsFlorian Weimer1-1/+1
Older versions such as binutils 2.35.2 do not recognize PT_GNU_PROPERTY. Fixes commit d3f2b71ef1d146137a25dd1367d97a14fac341c6 ("aarch64: Fix tests not compatible with targets supporting GCS").
2025-01-20aarch64: Fix tests not compatible with targets supporting GCSYury Khrustalev3-4/+54
- Add GCS marking to some of the tests when target supports GCS - Fix tst-ro-dynamic-mod.map linker script to avoid removing GNU properties - Add header with macros for GNU properties Reviewed-by: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
2025-01-20elf.h: Define GNU_PROPERTY_AARCH64_FEATURE_1_GCSSzabolcs Nagy1-0/+1
Reviewed-by: Carlos O'Donell <carlos@redhat.com> Reviewed-by: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
2025-01-16Consolidate TLS block allocation for static binaries with ld.soFlorian Weimer2-43/+77
Use the same code to compute the TLS block size and its alignment. The code in elf/dl-tls.c is linked in anyway for all binaries due to the reference to _dl_tls_static_surplus_init. It is not possible to call _dl_allocate_tls_storage directly because malloc is not available in the static case. (The dynamic linker uses the minimal malloc at this stage.) Therefore, split _dl_tls_block_size_with_pre and _dl_tls_block_align from _dl_allocate_tls_storage, and call those new functions from __libc_setup_tls. This fixes extra TLS allocation for the static case, and apparently some pre-existing bugs as well (the independent recomputation of TLS block sizes in init_static_tls looks rather suspect). Fixes commit 0e411c5d3098982d67cd2d7a233eaa6c977a1869 ("Add generic 'extra TLS'").
2025-01-16elf: Iterate over loaded object list in _dl_determine_tlsoffsetFlorian Weimer2-57/+39
The old code used the slotinfo array as a scratch area to pass the list of TLS-using objects to _dl_determine_tlsoffset. All array entries are subsequently overwritten by _dl_add_to_slotinfo, except the first one. The link maps are usually not at their right position for their module ID in the slotinfo array, so the initial use of the slotinfo array would be incorrect if not for scratch purposes only. In _dl_tls_initial_modid_limit_setup, the old code relied that some link map was written to the first slotinfo entry. After the change, this no longer happens because TLS module ID zero is unused. It's also necessary to move the call after the real initialization of the slotinfo array.
2025-01-13elf: Suppress unused function clang for __ifunc_resolverAdhemerval Zanella2-0/+8
The __ifunc_resolver macro expands to: extern __typeof (__redirect_name) name __attribute__ ((ifunc ("iname_ifunc"))); static __typeof (__redirect_name) *name_ifunc (void) { [...] }; And although NAME_IFUNC is and alias for NAME, clang-18 still emits an 'unused function 'name_ifunc' [-Werror,-Wunused-function]' warning.
2025-01-12elf: make tst-tls_tp_offset compare &thread_var to THREAD_SELFSamuel Thibault1-1/+1
rather than pthread_self (), which in htl is not a pointer into dtv, but an index.
2025-01-10Add generic 'extra TLS'Michael Jeanson1-0/+72
Add the logic to append an 'extra TLS' block in the TLS block allocator with a generic stub implementation. The duplicated code in 'csu/libc-tls.c' and 'elf/dl-tls.c' is to handle both statically linked applications and the ELF dynamic loader. Signed-off-by: Michael Jeanson <mjeanson@efficios.com> Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Reviewed-by: Florian Weimer <fweimer@redhat.com>
2025-01-09elf: Always define TLS_TP_OFFSETFlorian Weimer2-0/+58
This will be needed to compute __rseq_offset outside of the TLS relocation machinery. Reviewed-by: Michael Jeanson <mjeanson@efficios.com>
2025-01-08elf: Minimize library dependencies of tst-nolink-libc.cFlorian Weimer1-2/+4
On 32-bit Arm, -fasynchronous-unwind-tables creates a reference to the symbol __aeabi_unwind_cpp_pr0. Compile the tests without this flag even if it is passed as part of CC, to avoid linker failures.
2025-01-07elf: Second ld.so relocation only if libc.so has been loadedFlorian Weimer2-34/+45
Commit 8f8dd904c4a2207699bb666f30acceb5209c8d3f (“elf: rtld_multiple_ref is always true”) removed some code that happened to enable compatibility with programs that do not link against libc.so. Such programs cannot call dlopen or any dynamic linker functions (except __tls_get_addr), so this is not really useful. Still ld.so should not crash with a null-pointer dereference or undefined symbol reference in these cases. In the main relocation loop, call _dl_relocate_object unconditionally because it already checks if the object has been relocated. If libc.so was loaded, self-relocate ld.so against it and call __rtld_mutex_init and __rtld_malloc_init_real to activate the full implementations. Those are available only if libc.so is there, so skip these initialization steps if libc.so is absent. Without libc.so, the global scope can be completely empty. This can cause ld.so self-relocation to fail because if it uses symbol-based relocations, which is why the second ld.so self-relocation is not performed if libc.so is missing. The previous concern regarding GOT updates through self-relocation no longer applies because function pointers are updated explicitly through __rtld_mutex_init and __rtld_malloc_init_real, and not through relocation. However, the second ld.so self-relocation is still delayed, in case there are other symbols being used. Fixes commit 8f8dd904c4a2207699bb666f30acceb5209c8d3f (“elf: rtld_multiple_ref is always true”). Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-01-04Rename have-mtls-descriptor to have-test-mtls-descriptorH.J. Lu1-17/+17
Since have-mtls-descriptor is only used for glibc testing, rename it to have-test-mtls-descriptor. Also enable tst-gnu2-tls2-amx only if $(have-test-mtls-descriptor) == gnu2. Tested with GCC 14 and Clang 19/18/17 on x86-64. Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Sam James <sam@gentoo.org>
2025-01-03tst-unique[34].cc: Use explicit instantiation declaration/definitionH.J. Lu3-22/+51
Use explicit instantiation declaration and definition to silence Clang error: tst-unique3.cc:6:18: error: instantiation of variable 'S<char>::i' required here, but no definition is available [-Werror,-Wundefined-var-template] 6 | int t = S<char>::i; | ^ ./tst-unique3.h:5:14: note: forward declaration of template entity is here 5 | static int i; | ^ tst-unique3.cc:6:18: note: add an explicit instantiation declaration to suppress this warning if 'S<char>::i' is explicitly instantiated in another translation unit 6 | int t = S<char>::i; | ^ Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Sam James <sam@gentoo.org>
2025-01-02elf: Use TLS_DTV_OFFSET in __tls_get_addrFlorian Weimer1-4/+16
This fixes commit 5e249192cac7354af02a7347a0d8c984e0c88ed3 ("elf: Remove the GET_ADDR_ARGS and related macros from the TLS code"): GET_ADDR_ARGS was indeed unused, but GET_ADDR_OFFSET was used on several targets, those that define TLS_DTV_OFFSET. Instead of reintroducing GET_ADDR_OFFSET, use TLS_DTV_OFFSET directly, now that it is defined on all targets. In the new tls_get_addr_adjust helper function, add a cast to uintptr_t to help the s390 case, where the offset can be positive or negative, depending on the addresses malloc returns. The cast avoids pointer wraparound/overflow. The outer uintptr_t cast is needed to suppress a warning on x86-64 x32 about mismatched integer/pointer sizes. Eventually this offset should be folded into the DTV addresses themselves, to eliminate the subtraction on the TLS fast path. This will require an adjustment to libthread_db because the debugger interface currently returns unadjusted pointers. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-01-01Update copyright dates with scripts/update-copyrightsPaul Eggert430-431/+431
2025-01-01Pass glibc pre-commit checksPaul Eggert1-1/+0
This is needed for the next patch which updates copyright dates. * assert/test-assert-2.c: Remove trailing white space. * elf/tst-startup-errno.c: Remove trailing empty lines.