Commit graph

3112 commits

Author SHA1 Message Date
H.J. Lu
596130591a static-pie: Skip the empty PT_LOAD segment at offset 0 [BZ #32763]
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-06 07:27:03 +08:00
Adhemerval Zanella
1d60b9dfda Remove dl-procinfo.h
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-05 11:22:09 -03:00
Sam James
a2bd5008a9
Pass -Wl,--no-error-execstack for tests where -Wl,-z,execstack is used [PR32717]
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-03-04 20:08:50 +00:00
Wilco Dijkstra
0a021727bc AArch64: Remove AARCH64_R macro
Remove AArch64_R relocation macro.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2025-02-24 14:19:19 +00:00
Florian Weimer
aa3d7bd529 elf: Keep using minimal malloc after early DTV resize (bug 32412)
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-13 21:56:52 +01:00
Florian Weimer
68c9ef4419 elf: Build dl-tls.o with early startup symbol redirections
This is required when building for powerpc64le POWER8 with GCC 8
at least.

Fixes commit cbd9fd2369 ("Consolidate
TLS block allocation for static binaries with ld.so").

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-02-05 09:52:16 +01:00
Florian Weimer
fc058b46c7 elf: Use _dl_find_object instead of _dl_find_dso_for_object in dlopen
The _dl_find_object function uses a binary search and is faster if
there are many objects.
2025-02-02 20:10:09 +01:00
Florian Weimer
b05e78d473 elf: Add fast path to dlopen for fully-opened maps
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-02 20:10:09 +01:00
Florian Weimer
edc6842bbc elf: Determine the caller link map in _dl_open
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-02 20:10:09 +01:00
Florian Weimer
d12cb8e452 elf: Merge __dl_libc_freemem into __rtld_libc_freeres
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-02 20:10:09 +01:00
Florian Weimer
749310c61b elf: Add l_soname accessor function for DT_SONAME values
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-02 20:10:09 +01:00
Florian Weimer
aa1bf89039 elf: Split _dl_lookup_map, _dl_map_new_object from _dl_map_object
So that they can eventually be called separately from dlopen.
2025-02-02 20:10:08 +01:00
Florian Weimer
96429bcc91 elf: Do not add a copy of _dl_find_object to libc.so
This reduces code size and dependencies on ld.so internals from
libc.so.

Fixes commit f4c142bb9f
("arm: Use _dl_find_object on __gnu_Unwind_Find_exidx (BZ 31405)").

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-02-01 12:37:58 +01:00
Petr Malat
4c43173eba ld.so: Decorate BSS mappings
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-30 10:16:37 -03:00
Sam James
8d55f2fb17
elf: fix 'valgrind' typo in comment 2025-01-25 05:34:20 +00:00
Florian Weimer
a3131e71cf elf: Make tst-ro-dynamic-mod.map compatible with older binutils
Older versions such as binutils 2.35.2 do not recognize
PT_GNU_PROPERTY.

Fixes commit d3f2b71ef1
("aarch64: Fix tests not compatible with targets supporting GCS").
2025-01-20 20:35:10 +01:00
Yury Khrustalev
d3f2b71ef1 aarch64: Fix tests not compatible with targets supporting GCS
- 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-20 09:36:19 +00:00
Szabolcs Nagy
640c209a72 elf.h: Define GNU_PROPERTY_AARCH64_FEATURE_1_GCS
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Reviewed-by: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
2025-01-20 09:22:41 +00:00
Florian Weimer
cbd9fd2369 Consolidate TLS block allocation for static binaries with ld.so
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 0e411c5d30 ("Add generic
'extra TLS'").
2025-01-16 20:02:42 +01:00
Florian Weimer
7f784ffc17 elf: Iterate over loaded object list in _dl_determine_tlsoffset
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-16 20:02:42 +01:00
Adhemerval Zanella
6a498d780e elf: Suppress unused function clang for __ifunc_resolver
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-13 10:17:42 -03:00
Samuel Thibault
4ab62ce605 elf: make tst-tls_tp_offset compare &thread_var to THREAD_SELF
rather than pthread_self (), which in htl is not a pointer into dtv, but an
index.
2025-01-12 01:03:13 +01:00
Michael Jeanson
0e411c5d30 Add generic 'extra TLS'
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-10 20:19:28 +00:00
Florian Weimer
d1da011118 elf: Always define TLS_TP_OFFSET
This will be needed to compute __rseq_offset outside of the TLS
relocation machinery.

Reviewed-by: Michael Jeanson <mjeanson@efficios.com>
2025-01-09 19:30:44 +01:00
Florian Weimer
39183f47d8 elf: Minimize library dependencies of tst-nolink-libc.c
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-08 16:55:31 +01:00
Florian Weimer
706209867f elf: Second ld.so relocation only if libc.so has been loaded
Commit 8f8dd904c4 (“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 8f8dd904c4 (“elf:
rtld_multiple_ref is always true”).

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2025-01-07 09:19:01 +01:00
H.J. Lu
1294926da3 Rename have-mtls-descriptor to have-test-mtls-descriptor
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-04 04:51:21 +08:00
H.J. Lu
dbc9a40007 tst-unique[34].cc: Use explicit instantiation declaration/definition
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-03 04:54:44 +08:00
Florian Weimer
30e32da6aa elf: Use TLS_DTV_OFFSET in __tls_get_addr
This fixes commit 5e249192ca ("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-02 13:45:27 +01:00
Paul Eggert
2642002380 Update copyright dates with scripts/update-copyrights 2025-01-01 11:22:09 -08:00
Paul Eggert
b1da163a21 Pass glibc pre-commit checks
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.
2025-01-01 11:20:43 -08:00
Adhemerval Zanella
58272284b6 elf: Add glibc.rtld.execstack
The new tunable can be used to control whether executable stacks are
allowed from either the main program or dependencies.  The default is
to allow executable stacks.

The executable stacks default permission is checked agains the one
provided by the PT_GNU_STACK from program headers (if present).  The
tunable also disables the stack permission change if any dependency
requires an executable stack at loading time.

Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.

Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-12-31 09:04:20 -03:00
Adhemerval Zanella
c9540704ac elf: Add tst-execstack-prog-static
Similar to tst-execstack-prog, check if executable stacks works for
statically linked programs.

Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-12-31 09:04:20 -03:00
Adhemerval Zanella
0ca8785a28 elf: Do not change stack permission on dlopen/dlmopen
If some shared library loaded with dlopen/dlmopen requires an executable
stack, either implicitly because of a missing GNU_STACK ELF header
(where the ABI default flags implies in the executable bit) or explicitly
because of the executable bit from GNU_STACK; the loader will try to set
the both the main thread and all thread stacks (from the pthread cache)
as executable.

Besides the issue where any __nptl_change_stack_perm failure does not
undo the previous executable transition (meaning that if the library
fails to load, there can be thread stacks with executable stacks), this
behavior was used on a CVE [1] as a vector for RCE.

This patch changes that if a shared library requires an executable
stack, and the current stack is not executable, dlopen fails.  The
change is done only for dynamically loaded modules, if the program
or any dependency requires an executable stack, the loader will still
change the main thread before program execution and any thread created
with default stack configuration.

[1] https://www.qualys.com/2023/07/19/cve-2023-38408/rce-openssh-forwarded-ssh-agent.txt

Checked on x86_64-linux-gnu and i686-linux-gnu.

Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-12-31 09:04:20 -03:00
Adhemerval Zanella
ca96ea06b3 elf: Cleanup and improve tst-execstack
Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-12-31 09:04:20 -03:00
Florian Weimer
5e249192ca elf: Remove the GET_ADDR_ARGS and related macros from the TLS code
This was used to manage an IA-64 ABI divergence is no longere needed
after the IA-64 removal.

(It should be possible to encode all the required information in
one machine word, so the pointer indirection is really unnecessary.
Technically, none of this is part of the ABI, so perhaps it's
possible to do this retroactively.  See bug 27404.)

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2024-12-27 07:29:56 +01:00
H.J. Lu
0813cb5847 Suppress Clang -Wimplicit-fallthrough warnings
Since Clang doesn't properly handle

/* FALLTHROUGH */

in elf/tst-align2.c nor

/* fall through */

in misc/tst-tsearch.c

tst-align2.c💯9: error: unannotated fall-through between switch labels [-Werror,-Wimplicit-fallthrough]
  100 |         case 'A':
      |         ^
tst-align2.c💯9: note: insert '__attribute__((fallthrough));' to silence this warning
  100 |         case 'A':
      |         ^
      |         __attribute__((fallthrough));
tst-align2.c💯9: note: insert 'break;' to avoid fall-through
  100 |         case 'A':
      |         ^
      |         break;

suppress them when compiled with Clang.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Reviewed-by: Sam James <sam@gentoo.org>
2024-12-23 06:28:55 +08:00
Adhemerval Zanella
a2b0ff98a0 include/sys/cdefs.h: Add __attribute_optimization_barrier__
Add __attribute_optimization_barrier__ to disable inlining and cloning on a
function.  For Clang, expand it to

__attribute__ ((optnone))

Otherwise, expand it to

__attribute__ ((noinline, clone))

Co-Authored-By: H.J. Lu <hjl.tools@gmail.com>
Reviewed-by: Sam James <sam@gentoo.org>
2024-12-23 06:28:55 +08:00
Adhemerval Zanella
46a813466e elf: Suppress clang warning on tst-unique4
clang warns that the instantiation of the variable is required,
but no definition is available.  They are implemented on
tst-unique4lib.so.

Checked on x86_64-linux-gnu.
Reviewed-by: Sam James <sam@gentoo.org>
2024-12-22 17:24:16 +08:00
H.J. Lu
8d25b9f2c8 elf: Enable tst-dlopen-nodelete-reloc if TEST_CXX supports STB_GNU_UNIQUE
tst-dlopen-nodelete-reloc requires STB_GNU_UNIQUE support so that NODELETE
is propagated by do_lookup_unique.  Enable it only if TEST_CXX supports
STB_GNU_UNIQUE,

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Reviewed-by: Sam James <sam@gentoo.org>
2024-12-22 17:20:15 +08:00
H.J. Lu
03feea74dc elf: Compile test modules with -fsemantic-interposition
Compiler may default to -fno-semantic-interposition. But some elf test
modules must be compiled with -fsemantic-interposition to function properly.
Add a TEST_CC check for -fsemantic-interposition and use it on elf test
modules.  This fixed

FAIL: elf/tst-dlclose-lazy
FAIL: elf/tst-pie1
FAIL: elf/tst-plt-rewrite1
FAIL: elf/unload4

when Clang 19 is used to test glibc.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Reviewed-by: Sam James <sam@gentoo.org>
2024-12-22 13:15:43 +08:00
H.J. Lu
d9d30f6cb5 Enable execstack tests only if compiler supports trampoline
Since trampoline is required to test execstack, enable execstack tests
only if compiler supports trampoline.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Reviewed-by: Sam James <sam@gentoo.org>
2024-12-22 12:55:50 +08:00
Florian Weimer
495b96e064 elf: Reorder audit events in dlcose to match _dl_fini (bug 32066)
This was discovered after extending elf/tst-audit23 to cover
dlclose of the dlmopen namespace.

Auditors already experience the new order during process
shutdown (_dl_fini), so no LAV_CURRENT bump or backwards
compatibility code seems necessary.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2024-12-20 16:17:10 +01:00
Florian Weimer
c4b160744c elf: Call la_objclose for proxy link maps in _dl_fini (bug 32065)
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2024-12-20 16:17:08 +01:00
Florian Weimer
8f36b14696 elf: Signal la_objopen for the proxy link map in dlmopen (bug 31985)
Previously, the ld.so link map was silently added to the namespace.
This change produces an auditing event for it.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2024-12-20 16:16:21 +01:00
Florian Weimer
a20bc2f623 elf: Add the endswith function to <endswith.h>
And include <stdbool.h> for a definition of bool.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2024-12-20 16:15:53 +01:00
Florian Weimer
4a50fdf8b2 elf: Update DSO list, write audit log to elf/tst-audit23.out
After commit 1d5024f4f0
("support: Build with exceptions and asynchronous unwind tables
[BZ #30587]"), libgcc_s is expected to show up in the DSO
list on 32-bit Arm.  Do not update max_objs because vdso is not
tracked (and which is the reason why the test currently passes
even with libgcc_s present).

Also write the log output from the auditor to standard output,
for easier test debugging.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2024-12-20 16:15:51 +01:00
Florian Weimer
ef5823d955 elf: Move _dl_rtld_map, _dl_rtld_audit_state out of GL
This avoids immediate GLIBC_PRIVATE ABI issues if the size of
struct link_map or struct auditstate changes.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2024-12-20 15:52:57 +01:00
Florian Weimer
2b1dba3eb3 elf: Introduce is_rtld_link_map
Unconditionally define it to false for static builds.

This avoids the awkward use of weak_extern for _dl_rtld_map
in checks that cannot be possibly true on static builds.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2024-12-20 15:52:57 +01:00
Florian Weimer
e79b9e962d elf: Remove code dependent on __rtld_lock_default_lock_recursive macro
Neither NPTL nor Hurd define this macro anymore.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2024-12-19 21:29:58 +01:00