Module Name:    src
Committed By:   martin
Date:           Tue Aug  1 16:35:00 UTC 2023

Modified Files:
        src/distrib/sets/lists/debug [netbsd-10]: mi shl.mi
        src/distrib/sets/lists/tests [netbsd-10]: mi shl.mi
        src/libexec/ld.elf_so [netbsd-10]: README.TLS map_object.c rtld.c
            rtld.h tls.c
        src/libexec/ld.elf_so/arch/aarch64 [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/alpha [netbsd-10]: alpha_reloc.c
        src/libexec/ld.elf_so/arch/arm [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/hppa [netbsd-10]: hppa_reloc.c
        src/libexec/ld.elf_so/arch/i386 [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/m68k [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/mips [netbsd-10]: mips_reloc.c
        src/libexec/ld.elf_so/arch/or1k [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/powerpc [netbsd-10]: ppc_reloc.c
        src/libexec/ld.elf_so/arch/riscv [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/sh3 [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/sparc [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/sparc64 [netbsd-10]: mdreloc.c
        src/libexec/ld.elf_so/arch/x86_64 [netbsd-10]: mdreloc.c
        src/tests/libexec/ld.elf_so [netbsd-10]: Makefile
        src/tests/libexec/ld.elf_so/helper_dso1 [netbsd-10]: Makefile
        src/tests/libexec/ld.elf_so/helper_dso2 [netbsd-10]: Makefile
        src/tests/libexec/ld.elf_so/helper_dso3 [netbsd-10]: Makefile
        src/tests/libexec/ld.elf_so/helper_ifunc_dso [netbsd-10]: Makefile
        src/tests/libexec/ld.elf_so/helper_symver_dso0 [netbsd-10]: Makefile
        src/tests/libexec/ld.elf_so/helper_symver_dso1 [netbsd-10]: Makefile
        src/tests/libexec/ld.elf_so/helper_symver_dso2 [netbsd-10]: Makefile
Added Files:
        src/tests/libexec/ld.elf_so [netbsd-10]: helper.mk t_tls_extern.c
        src/tests/libexec/ld.elf_so/helper_abuse_dynamic [netbsd-10]: Makefile
            h_abuse_dynamic.c
        src/tests/libexec/ld.elf_so/helper_abuse_static [netbsd-10]: Makefile
            h_abuse_static.c
        src/tests/libexec/ld.elf_so/helper_def_dynamic [netbsd-10]: Makefile
            h_def_dynamic.c
        src/tests/libexec/ld.elf_so/helper_def_static [netbsd-10]: Makefile
            h_def_static.c
        src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic [netbsd-10]:
            Makefile h_onlyctor_dynamic.c
        src/tests/libexec/ld.elf_so/helper_onlydef [netbsd-10]: Makefile
            h_onlydef.c
        src/tests/libexec/ld.elf_so/helper_onlydef_static [netbsd-10]: Makefile
            h_onlydef_static.c
        src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic [netbsd-10]:
            Makefile h_onlyuse_dynamic.c
        src/tests/libexec/ld.elf_so/helper_onlyuse_static [netbsd-10]: Makefile
            h_onlyuse_static.c
        src/tests/libexec/ld.elf_so/helper_use_dynamic [netbsd-10]: Makefile
            h_use_dynamic.c
        src/tests/libexec/ld.elf_so/helper_use_static [netbsd-10]: Makefile
            h_use_static.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #297):

        distrib/sets/lists/tests/shl.mi: revision 1.14
        distrib/sets/lists/tests/shl.mi: revision 1.15
        distrib/sets/lists/tests/shl.mi: revision 1.16
        tests/libexec/ld.elf_so/helper_def_static/h_def_static.c: revision 1.1
        tests/libexec/ld.elf_so/helper_def_dynamic/Makefile: revision 1.1
        tests/libexec/ld.elf_so/helper_def_dynamic/Makefile: revision 1.2
        tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile: revision 1.1
        tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile: revision 1.2
        libexec/ld.elf_so/arch/mips/mips_reloc.c: revision 1.75
        distrib/sets/lists/tests/mi: revision 1.1265
        libexec/ld.elf_so/arch/sh3/mdreloc.c: revision 1.36
        libexec/ld.elf_so/rtld.c: revision 1.214
        tests/libexec/ld.elf_so/helper_onlydef_static/Makefile: revision 1.1
        distrib/sets/lists/debug/mi: revision 1.400
        tests/libexec/ld.elf_so/helper_onlydef_static/Makefile: revision 1.2
        distrib/sets/lists/debug/mi: revision 1.401
        distrib/sets/lists/debug/mi: revision 1.402
        tests/libexec/ld.elf_so/helper_dso2/Makefile: revision 1.2
        distrib/sets/lists/debug/mi: revision 1.403
        tests/libexec/ld.elf_so/helper_symver_dso0/Makefile: revision 1.2
        libexec/ld.elf_so/arch/x86_64/mdreloc.c: revision 1.48
        distrib/sets/lists/debug/mi: revision 1.406
        tests/libexec/ld.elf_so/helper_use_dynamic/Makefile: revision 1.1
        tests/libexec/ld.elf_so/helper_use_dynamic/Makefile: revision 1.2
        tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile: revision 1.2
        libexec/ld.elf_so/arch/sparc64/mdreloc.c: revision 1.70
        libexec/ld.elf_so/arch/aarch64/mdreloc.c: revision 1.18
        tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile: revision 1.1
        tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile: revision 1.2
        tests/libexec/ld.elf_so/Makefile: revision 1.13
        libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.46
        libexec/ld.elf_so/rtld.h: revision 1.146
        tests/libexec/ld.elf_so/Makefile: revision 1.14
        distrib/sets/lists/debug/shl.mi: revision 1.306
        tests/libexec/ld.elf_so/Makefile: revision 1.15
        tests/libexec/ld.elf_so/helper_abuse_static/Makefile: revision 1.1
        distrib/sets/lists/debug/shl.mi: revision 1.307
        tests/libexec/ld.elf_so/Makefile: revision 1.16
        tests/libexec/ld.elf_so/helper_abuse_static/Makefile: revision 1.2
        distrib/sets/lists/debug/shl.mi: revision 1.308
        tests/libexec/ld.elf_so/Makefile: revision 1.17
        distrib/sets/lists/debug/shl.mi: revision 1.309
        tests/libexec/ld.elf_so/Makefile: revision 1.18
        tests/libexec/ld.elf_so/Makefile: revision 1.19
        libexec/ld.elf_so/tls.c: revision 1.16
        libexec/ld.elf_so/tls.c: revision 1.17
        libexec/ld.elf_so/tls.c: revision 1.18
        libexec/ld.elf_so/tls.c: revision 1.19
        tests/libexec/ld.elf_so/helper_onlydef_static/h_onlydef_static.c: 
revision 1.1
        tests/libexec/ld.elf_so/helper_use_static/h_use_static.c: revision 1.1
        tests/libexec/ld.elf_so/helper_use_static/h_use_static.c: revision 1.2
        tests/libexec/ld.elf_so/helper_def_static/Makefile: revision 1.1
        tests/libexec/ld.elf_so/helper_def_static/Makefile: revision 1.2
        libexec/ld.elf_so/arch/hppa/hppa_reloc.c: revision 1.50
        distrib/sets/lists/debug/shl.mi: revision 1.310
        libexec/ld.elf_so/README.TLS: revision 1.6
        distrib/sets/lists/debug/shl.mi: revision 1.311
        distrib/sets/lists/debug/shl.mi: revision 1.314
        tests/libexec/ld.elf_so/helper_dso3/Makefile: revision 1.2
        tests/libexec/ld.elf_so/helper_symver_dso1/Makefile: revision 1.4
        libexec/ld.elf_so/arch/powerpc/ppc_reloc.c: revision 1.63
        tests/libexec/ld.elf_so/helper_def_dynamic/h_def_dynamic.c: revision 1.1
        tests/libexec/ld.elf_so/helper_onlydef/Makefile: revision 1.1
        tests/libexec/ld.elf_so/helper_onlydef/Makefile: revision 1.2
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.10
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.11
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.12
        libexec/ld.elf_so/map_object.c: revision 1.66
        tests/libexec/ld.elf_so/helper.mk: revision 1.1
        libexec/ld.elf_so/arch/sparc/mdreloc.c: revision 1.57
        libexec/ld.elf_so/map_object.c: revision 1.67
        tests/libexec/ld.elf_so/helper_onlydef/h_onlydef.c: revision 1.1
        tests/libexec/ld.elf_so/helper_symver_dso2/Makefile: revision 1.4
        tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.1
        tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.2
        tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.3
        tests/libexec/ld.elf_so/helper_use_dynamic/h_use_dynamic.c: revision 1.1
        tests/libexec/ld.elf_so/helper_abuse_static/h_abuse_static.c: revision 
1.1
        libexec/ld.elf_so/arch/riscv/mdreloc.c: revision 1.9
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.1
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.2
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.3
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.4
        tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile: revision 1.1
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.5
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.6
        libexec/ld.elf_so/arch/m68k/mdreloc.c: revision 1.34
        tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile: revision 1.2
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.7
        libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.42
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.8
        libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.43
        libexec/ld.elf_so/arch/or1k/mdreloc.c: revision 1.4
        tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.9
        tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile: revision 1.1
        tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile: revision 1.2
        tests/libexec/ld.elf_so/helper_abuse_dynamic/h_abuse_dynamic.c: 
revision 1.1
        tests/libexec/ld.elf_so/helper_onlyctor_dynamic/h_onlyctor_dynamic.c: 
revision 1.1
        tests/libexec/ld.elf_so/helper_onlyuse_static/h_onlyuse_static.c: 
revision 1.1
        tests/libexec/ld.elf_so/helper_onlyuse_dynamic/h_onlyuse_dynamic.c: 
revision 1.1
        tests/libexec/ld.elf_so/helper_dso1/Makefile: revision 1.2
        distrib/sets/lists/tests/shl.mi: revision 1.12
        distrib/sets/lists/tests/shl.mi: revision 1.13
        libexec/ld.elf_so/arch/alpha/alpha_reloc.c: revision 1.44
        (all via patch)

ld.elf_so: New test for extern initial-exec TLS, PR toolchain/50277.

ld.elf_so: Fix extern TLS test to match PR toolchain/50277.
Now it's actually testing the problem.
ld.elf_so: Nix inadvertently committed private test program.
ld.elf_so: Fix set lists for MKDEBUG=yes builds with t_tls_extern.

ld.elf_so: Sprinkle tls debug messages.

ld.elf_so: Make tls alloc debug messages more detailed and greppable.

ld.elf_so: Test variations on PR toolchain/50277.

ld.elf_so: Test extern dynamic TLS too.

ld.elf_so: Factor out logic in TLS tests to make writing more easier.
No functional change intended.

ld.elf_so: Test TLS abuse of static def, dynamic use and vice versa.

ld.elf_so: Shorter test names.
No functional non-cosmetic change intended.

ld.elf_so: Separately test eager and lazy resolution of def tls ptr.
eager: before loading use library
lazy: after loading use library

Add recent ld.elf_so test helpers debug info
ld.elf_so: Add new files to debug/shl.mi.

ld.elf_so: tls_extern dynamic_defabuse_eager must xfail differently.
If a symbol has already been resolved as dynamic TLS, any library
that tries to use it as static TLS cannot be dlopened.

ld.elf_so: Test another edge case of mismatched TLS models.
One library defines a symbol and _doesn't_ use it, so it has no
indication of whether the symbol is for static TLS or dynamic TLS,
and then two other libraries use it in different ways.

ld.elf_so: Test dynamic-then-static abuse via ctor.

ld.elf_so: Fix missing tab in debug/shl.mi in last change.

Fix interactions of initial-exec TLS model and dlopen
(1) If an initial-exec relocation was used for a non-local symbol
(i.e. the definition of the symbol is in a different DSO), the
computation of the static TLS offset used the wrong DSO.
This would effectively mean the wrong address was computed
(PR toolchain/50277, PR pkg/57445).
Fix this by forcing the computation of the correct DSO (the one defining
the symbol).
This code uses __UNCONST to avoid the vast interface changes for this
special case.
(2) If symbols from a DSO loaded via dlopen are used with both
global-dynamic/local-dynamic and initial-exec relocations AND
a initial-exec relocation was resolved first in a thread, a split brain
situation could exist where the dynamic relocations would use one memory
block (separate allocation) and the initial-exec relocations the static
per-thread TLS space.
(3) If the initial-exec relocation in (2) is seen after any thread has
already used a GD/LD allocation, bail out. Since IE relocations are used
only in the GOT, this will prevent the dlopen. This is a bit more
aggressive than necessary, but a full blown reference counting doesn't
seem to be justified.
Avoid using uninitialized variable "symnum" when building with DEBUG
enabled by borrowing the rdbg_symname() macro from arch/x86_64.
ld.elf_so: Sprinkle more debug messages on dlopen and error.
PR pkg/57445
Fix MKDEBUGLIB build by adding these installed files to the debug
set list.
XXX
One could argue that these files are not of any use, so why install
them?  I don't have a good argument either way, and this is (for
now) a simple work-around for PR bin/57455   Please feel free to
commit a different fix to avoid installing these files at all.
Fix markup of libh_ MKDEBUGLIB=yes only files
TLS variant I archs need to fudge the offset by the size of the TCB.
tests/libexec/ld.elf_so: Fix helper library makefiles.
1. Consolidate logic into a single helper.mk to reduce duplication.
2. Set NO* variables, not MK* variables which are reserved for user.
3. Avoid eager X!= in favour of lazy ${X:sh}.
4. Mark _g.a set list entries obsolete.  Never should've been built!
PR misc/57462


To generate a diff of this commit:
cvs rdiff -u -r1.394 -r1.394.2.1 src/distrib/sets/lists/debug/mi
cvs rdiff -u -r1.298.2.1 -r1.298.2.2 src/distrib/sets/lists/debug/shl.mi
cvs rdiff -u -r1.1238 -r1.1238.2.1 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.11 -r1.11.18.1 src/distrib/sets/lists/tests/shl.mi
cvs rdiff -u -r1.5 -r1.5.10.1 src/libexec/ld.elf_so/README.TLS
cvs rdiff -u -r1.62 -r1.62.2.1 src/libexec/ld.elf_so/map_object.c
cvs rdiff -u -r1.212.2.1 -r1.212.2.2 src/libexec/ld.elf_so/rtld.c
cvs rdiff -u -r1.144 -r1.144.2.1 src/libexec/ld.elf_so/rtld.h
cvs rdiff -u -r1.14 -r1.14.8.1 src/libexec/ld.elf_so/tls.c
cvs rdiff -u -r1.17 -r1.17.2.1 src/libexec/ld.elf_so/arch/aarch64/mdreloc.c
cvs rdiff -u -r1.43 -r1.43.14.1 \
    src/libexec/ld.elf_so/arch/alpha/alpha_reloc.c
cvs rdiff -u -r1.45 -r1.45.6.1 src/libexec/ld.elf_so/arch/arm/mdreloc.c
cvs rdiff -u -r1.49 -r1.49.2.1 src/libexec/ld.elf_so/arch/hppa/hppa_reloc.c
cvs rdiff -u -r1.41 -r1.41.12.1 src/libexec/ld.elf_so/arch/i386/mdreloc.c
cvs rdiff -u -r1.33 -r1.33.14.1 src/libexec/ld.elf_so/arch/m68k/mdreloc.c
cvs rdiff -u -r1.74 -r1.74.6.1 src/libexec/ld.elf_so/arch/mips/mips_reloc.c
cvs rdiff -u -r1.3 -r1.3.14.1 src/libexec/ld.elf_so/arch/or1k/mdreloc.c
cvs rdiff -u -r1.62 -r1.62.2.1 src/libexec/ld.elf_so/arch/powerpc/ppc_reloc.c
cvs rdiff -u -r1.7 -r1.7.2.1 src/libexec/ld.elf_so/arch/riscv/mdreloc.c
cvs rdiff -u -r1.35 -r1.35.14.1 src/libexec/ld.elf_so/arch/sh3/mdreloc.c
cvs rdiff -u -r1.55 -r1.55.12.1 src/libexec/ld.elf_so/arch/sparc/mdreloc.c
cvs rdiff -u -r1.69 -r1.69.12.1 src/libexec/ld.elf_so/arch/sparc64/mdreloc.c
cvs rdiff -u -r1.47 -r1.47.12.1 src/libexec/ld.elf_so/arch/x86_64/mdreloc.c
cvs rdiff -u -r1.12 -r1.12.4.1 src/tests/libexec/ld.elf_so/Makefile
cvs rdiff -u -r0 -r1.1.2.2 src/tests/libexec/ld.elf_so/helper.mk
cvs rdiff -u -r0 -r1.12.2.2 src/tests/libexec/ld.elf_so/t_tls_extern.c
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_abuse_dynamic/h_abuse_dynamic.c
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_abuse_static/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_abuse_static/h_abuse_static.c
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_def_dynamic/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_def_dynamic/h_def_dynamic.c
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_def_static/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_def_static/h_def_static.c
cvs rdiff -u -r1.1 -r1.1.54.1 \
    src/tests/libexec/ld.elf_so/helper_dso1/Makefile
cvs rdiff -u -r1.1 -r1.1.54.1 \
    src/tests/libexec/ld.elf_so/helper_dso2/Makefile
cvs rdiff -u -r1.1 -r1.1.18.1 \
    src/tests/libexec/ld.elf_so/helper_dso3/Makefile
cvs rdiff -u -r1.1 -r1.1.26.1 \
    src/tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic/h_onlyctor_dynamic.c
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlydef/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlydef/h_onlydef.c
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlydef_static/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlydef_static/h_onlydef_static.c
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic/h_onlyuse_dynamic.c
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_onlyuse_static/h_onlyuse_static.c
cvs rdiff -u -r1.1 -r1.1.52.1 \
    src/tests/libexec/ld.elf_so/helper_symver_dso0/Makefile
cvs rdiff -u -r1.3 -r1.3.36.1 \
    src/tests/libexec/ld.elf_so/helper_symver_dso1/Makefile
cvs rdiff -u -r1.3 -r1.3.36.1 \
    src/tests/libexec/ld.elf_so/helper_symver_dso2/Makefile
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_use_dynamic/Makefile
cvs rdiff -u -r0 -r1.1.2.2 \
    src/tests/libexec/ld.elf_so/helper_use_dynamic/h_use_dynamic.c
cvs rdiff -u -r0 -r1.3.2.2 \
    src/tests/libexec/ld.elf_so/helper_use_static/Makefile
cvs rdiff -u -r0 -r1.2.2.2 \
    src/tests/libexec/ld.elf_so/helper_use_static/h_use_static.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/debug/mi
diff -u src/distrib/sets/lists/debug/mi:1.394 src/distrib/sets/lists/debug/mi:1.394.2.1
--- src/distrib/sets/lists/debug/mi:1.394	Wed Nov 30 06:07:51 2022
+++ src/distrib/sets/lists/debug/mi	Tue Aug  1 16:34:57 2023
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.394 2022/11/30 06:07:51 ozaki-r Exp $
+# $NetBSD: mi,v 1.394.2.1 2023/08/01 16:34:57 martin Exp $
 ./etc/mtree/set.debug                           comp-sys-root
 ./usr/lib					comp-sys-usr		compatdir
 ./usr/lib/i18n/libBIG5_g.a			comp-c-debuglib		debuglib,compatfile
@@ -2388,6 +2388,8 @@
 ./usr/libdata/debug/usr/tests/lib/libutil/t_sockaddr_snprintf.debug	tests-lib-debug		debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/lua/libm/h_lualibm.debug		tests-lib-debug		debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/lib/semaphore/pthread/t_sem_pth.debug	tests-lib-debug		debug,atf,rump
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_abuse_dynamic.so.1.debug	tests-libexec-debug	debug,atf,pic,compattestfile
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_abuse_static.so.1.debug	tests-libexec-debug	debug,atf,pic,compattestfile
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/h_df_1_noopen1.debug	tests-libexec-debug	debug,atf,pic,compattestfile
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/h_df_1_noopen2.debug	tests-libexec-debug	debug,atf,pic,compattestfile
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/h_dl_symver_v0.debug	tests-libexec-debug	debug,atf,pic,compattestfile
@@ -2401,6 +2403,7 @@
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_dlvsym.debug		tests-libexec-debug	debug,atf,pic,compattestfile
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_rtld_r_debug.debug	tests-libexec-debug	debug,atf,pic,compattestfile
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_ifunc.debug	tests-libexec-debug	debug,atf,pic,compattestfile
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_tls_extern.debug	tests-libexec-debug	debug,atf,pic,compattestfile
 ./usr/libdata/debug/usr/tests/net/bpf/t_bpf.debug		tests-net-debug		debug,atf,rump
 ./usr/libdata/debug/usr/tests/net/bpf/t_div-by-zero.debug		tests-net-debug		debug,atf,rump
 ./usr/libdata/debug/usr/tests/net/bpf/t_mbuf.debug		tests-net-debug		debug,atf,rump
@@ -2524,4 +2527,14 @@
 ./usr/libdata/debug/usr/tests/util/id/h_id.debug			tests-obsolete		obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/util/systrace/h_have_systrace.debug	tests-obsolete		obsolete,compattestfile
 ./usr/tests/lib/libc/tls/libh_tls_dynamic_g.a	comp-c-debuglib		atf,debuglib,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_abuse_dynamic_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_abuse_static_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_def_dynamic_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_def_static_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_onlyctor_dynamic_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_onlydef_g.a				tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_onlyuse_dynamic_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_onlyuse_static_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_use_dynamic_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
+./usr/tests/libexec/ld.elf_so/libh_use_static_g.a			tests-obsolete	obsolete,debuglib,atf,pic,compattestfile
 ./var/db/obsolete/debug				base-sys-root		debug

Index: src/distrib/sets/lists/debug/shl.mi
diff -u src/distrib/sets/lists/debug/shl.mi:1.298.2.1 src/distrib/sets/lists/debug/shl.mi:1.298.2.2
--- src/distrib/sets/lists/debug/shl.mi:1.298.2.1	Fri Feb 24 13:42:06 2023
+++ src/distrib/sets/lists/debug/shl.mi	Tue Aug  1 16:34:57 2023
@@ -1,4 +1,4 @@
-# $NetBSD: shl.mi,v 1.298.2.1 2023/02/24 13:42:06 martin Exp $
+# $NetBSD: shl.mi,v 1.298.2.2 2023/08/01 16:34:57 martin Exp $
 ./usr/lib/libbfd_g.a						comp-c-debuglib	debuglib,compatfile,binutils
 ./usr/libdata/debug/lib						base-sys-usr	debug,dynamicroot,compatdir
 ./usr/libdata/debug/lib/libavl.so.0.0.debug			comp-zfs-debug	debug,dynamicroot,zfs
@@ -356,14 +356,24 @@
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/h_helper_symver_dso1/libh_helper_symver_dso.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/h_helper_symver_dso2/libh_helper_symver_dso.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/h_thread_local_dtor.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_abuse_dynamic.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_abuse_static.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_def_dynamic.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_def_static.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_helper_dso1.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_helper_dso2.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_helper_dso3.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
 ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_helper_ifunc_dso.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
-./usr/tests/libexec/ld.elf_so/h_helper_symver_dso0/libh_helper_symver_dso_g.a	comp-c-debuglib	atf,debuglib,compattestfile
-./usr/tests/libexec/ld.elf_so/h_helper_symver_dso1/libh_helper_symver_dso_g.a	comp-c-debuglib	atf,debuglib,compattestfile
-./usr/tests/libexec/ld.elf_so/h_helper_symver_dso2/libh_helper_symver_dso_g.a	comp-c-debuglib	atf,debuglib,compattestfile
-./usr/tests/libexec/ld.elf_so/libh_helper_dso1_g.a			comp-c-debuglib	atf,debuglib,compattestfile
-./usr/tests/libexec/ld.elf_so/libh_helper_dso2_g.a			comp-c-debuglib	atf,debuglib,compattestfile
-./usr/tests/libexec/ld.elf_so/libh_helper_dso3_g.a			comp-c-debuglib	atf,debuglib,compattestfile
-./usr/tests/libexec/ld.elf_so/libh_helper_ifunc_dso_g.a			comp-c-debuglib	atf,debuglib,compattestfile
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_onlyctor_dynamic.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_onlydef.so.1.debug		tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_onlyuse_dynamic.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_onlyuse_static.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_use_dynamic.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/libdata/debug/usr/tests/libexec/ld.elf_so/libh_use_static.so.1.debug	tests-libexec-debug	debug,compattestfile,atf
+./usr/tests/libexec/ld.elf_so/h_helper_symver_dso0/libh_helper_symver_dso_g.a	comp-obsolete	atf,debuglib,compattestfile,obsolete
+./usr/tests/libexec/ld.elf_so/h_helper_symver_dso1/libh_helper_symver_dso_g.a	comp-obsolete	atf,debuglib,compattestfile,obsolete
+./usr/tests/libexec/ld.elf_so/h_helper_symver_dso2/libh_helper_symver_dso_g.a	comp-obsolete	atf,debuglib,compattestfile,obsolete
+./usr/tests/libexec/ld.elf_so/libh_helper_dso1_g.a			comp-obsolete	atf,debuglib,compattestfile,obsolete
+./usr/tests/libexec/ld.elf_so/libh_helper_dso2_g.a			comp-obsolete	atf,debuglib,compattestfile,obsolete
+./usr/tests/libexec/ld.elf_so/libh_helper_dso3_g.a			comp-obsolete	atf,debuglib,compattestfile,obsolete
+./usr/tests/libexec/ld.elf_so/libh_helper_ifunc_dso_g.a			comp-obsolete	atf,debuglib,compattestfile,obsolete

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.1238 src/distrib/sets/lists/tests/mi:1.1238.2.1
--- src/distrib/sets/lists/tests/mi:1.1238	Thu Dec  1 04:24:37 2022
+++ src/distrib/sets/lists/tests/mi	Tue Aug  1 16:34:56 2023
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1238 2022/12/01 04:24:37 ozaki-r Exp $
+# $NetBSD: mi,v 1.1238.2.1 2023/08/01 16:34:56 martin Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -4096,6 +4096,7 @@
 ./usr/tests/libexec/ld.elf_so/t_ifunc			tests-libexec-tests	compattestfile,atf,pic
 ./usr/tests/libexec/ld.elf_so/t_rtld_r_debug		tests-libexec-tests	compattestfile,atf,pic
 ./usr/tests/libexec/ld.elf_so/t_thread_local_dtor	tests-libexec-tests	compattestfile,atf,pic
+./usr/tests/libexec/ld.elf_so/t_tls_extern		tests-libexec-tests	compattestfile,atf,pic
 ./usr/tests/modules					tests-sys-tests		compattestfile,atf
 ./usr/tests/net						tests-net-tests		compattestfile,atf
 ./usr/tests/net/Atffile					tests-net-tests		compattestfile,atf

Index: src/distrib/sets/lists/tests/shl.mi
diff -u src/distrib/sets/lists/tests/shl.mi:1.11 src/distrib/sets/lists/tests/shl.mi:1.11.18.1
--- src/distrib/sets/lists/tests/shl.mi:1.11	Tue Jul 11 15:21:32 2017
+++ src/distrib/sets/lists/tests/shl.mi	Tue Aug  1 16:34:56 2023
@@ -1,4 +1,4 @@
-# $NetBSD: shl.mi,v 1.11 2017/07/11 15:21:32 joerg Exp $
+# $NetBSD: shl.mi,v 1.11.18.1 2023/08/01 16:34:56 martin Exp $
 #
 ./usr/tests/lib/csu/h_initfini3_dso.so		tests-lib-tests		compattestfile,atf
 ./usr/tests/lib/csu/h_initfini3_dso.so.1	tests-lib-tests		compattestfile,atf
@@ -16,6 +16,14 @@
 ./usr/tests/libexec/ld.elf_so/h_helper_symver_dso1/libh_helper_symver_dso.so.1	tests-libexec-tests	compattestfile,atf
 ./usr/tests/libexec/ld.elf_so/h_helper_symver_dso2/libh_helper_symver_dso.so	tests-libexec-tests	compattestfile,atf
 ./usr/tests/libexec/ld.elf_so/h_helper_symver_dso2/libh_helper_symver_dso.so.1	tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_abuse_dynamic.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_abuse_dynamic.so.1				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_abuse_static.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_abuse_static.so.1				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_def_dynamic.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_def_dynamic.so.1				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_def_static.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_def_static.so.1				tests-libexec-tests	compattestfile,atf
 ./usr/tests/libexec/ld.elf_so/libh_helper_dso1.so				tests-libexec-tests	compattestfile,atf
 ./usr/tests/libexec/ld.elf_so/libh_helper_dso1.so.1				tests-libexec-tests	compattestfile,atf
 ./usr/tests/libexec/ld.elf_so/libh_helper_dso2.so				tests-libexec-tests	compattestfile,atf
@@ -24,5 +32,17 @@
 ./usr/tests/libexec/ld.elf_so/libh_helper_dso3.so.1				tests-libexec-tests	compattestfile,atf
 ./usr/tests/libexec/ld.elf_so/libh_helper_ifunc_dso.so				tests-libexec-tests	compattestfile,atf
 ./usr/tests/libexec/ld.elf_so/libh_helper_ifunc_dso.so.1			tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_onlyctor_dynamic.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_onlyctor_dynamic.so.1			tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_onlydef.so					tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_onlydef.so.1					tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_onlyuse_dynamic.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_onlyuse_dynamic.so.1				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_onlyuse_static.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_onlyuse_static.so.1				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_use_dynamic.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_use_dynamic.so.1				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_use_static.so				tests-libexec-tests	compattestfile,atf
+./usr/tests/libexec/ld.elf_so/libh_use_static.so.1				tests-libexec-tests	compattestfile,atf
 ./usr/tests/util/id/libfake.so.0		tests-obsolete		obsolete
 ./usr/tests/util/id/libfake.so.0.0		tests-obsolete		obsolete

Index: src/libexec/ld.elf_so/README.TLS
diff -u src/libexec/ld.elf_so/README.TLS:1.5 src/libexec/ld.elf_so/README.TLS:1.5.10.1
--- src/libexec/ld.elf_so/README.TLS:1.5	Sat Dec  1 13:01:57 2018
+++ src/libexec/ld.elf_so/README.TLS	Tue Aug  1 16:34:56 2023
@@ -43,9 +43,10 @@ This is normally def->st_value + rela->r
 
 (c) R_TYPE(TLS_TPOFF): Static TLS offset.  The code has to check whether
 the static TLS offset for this module has been allocated
-(defobj->tls_done) and otherwise call _rtld_tls_offset_allocate().  This
+(defobj->tls_static) and otherwise call _rtld_tls_offset_allocate().  This
 may fail if no static space is available and the object has been pulled
-in via dlopen(3).
+in via dlopen(3). It can also fail if the TLS area has already been used
+via a global-dynamic allocation.
 
 For TLS Variant I, this is typically:
 

Index: src/libexec/ld.elf_so/map_object.c
diff -u src/libexec/ld.elf_so/map_object.c:1.62 src/libexec/ld.elf_so/map_object.c:1.62.2.1
--- src/libexec/ld.elf_so/map_object.c:1.62	Wed Mar 30 08:26:45 2022
+++ src/libexec/ld.elf_so/map_object.c	Tue Aug  1 16:34:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: map_object.c,v 1.62 2022/03/30 08:26:45 hannken Exp $	 */
+/*	$NetBSD: map_object.c,v 1.62.2.1 2023/08/01 16:34:56 martin Exp $	 */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: map_object.c,v 1.62 2022/03/30 08:26:45 hannken Exp $");
+__RCSID("$NetBSD: map_object.c,v 1.62.2.1 2023/08/01 16:34:56 martin Exp $");
 #endif /* not lint */
 
 #include <errno.h>
@@ -286,6 +286,9 @@ _rtld_map_object(const char *path, int f
 		obj->tlsalign = phtls->p_align;
 		obj->tlsinitsize = phtls->p_filesz;
 		tls_vaddr = phtls->p_vaddr;
+		dbg(("%s: tls index %zu size %zu align %zu initsize %zu",
+		    obj->path, obj->tlsindex, obj->tlssize, obj->tlsalign,
+		    obj->tlsinitsize));
 	}
 #endif
 
@@ -399,8 +402,11 @@ _rtld_map_object(const char *path, int f
 #endif
 
 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
-	if (phtls != NULL)
+	if (phtls != NULL) {
 		obj->tlsinit = mapbase + tls_vaddr;
+		dbg(("%s: tls init = %p + %"PRImemsz" = %p", obj->path,
+		    mapbase, tls_vaddr, obj->tlsinit));
+	}
 #endif
 
 	obj->mapbase = mapbase;
@@ -444,7 +450,7 @@ _rtld_obj_free(Obj_Entry *obj)
 	Name_Entry *entry;
 
 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
-	if (obj->tls_done)
+	if (obj->tls_static)
 		_rtld_tls_offset_free(obj);
 #endif
 	xfree(obj->path);

Index: src/libexec/ld.elf_so/rtld.c
diff -u src/libexec/ld.elf_so/rtld.c:1.212.2.1 src/libexec/ld.elf_so/rtld.c:1.212.2.2
--- src/libexec/ld.elf_so/rtld.c:1.212.2.1	Tue Aug  1 15:13:02 2023
+++ src/libexec/ld.elf_so/rtld.c	Tue Aug  1 16:34:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: rtld.c,v 1.212.2.1 2023/08/01 15:13:02 martin Exp $	 */
+/*	$NetBSD: rtld.c,v 1.212.2.2 2023/08/01 16:34:56 martin Exp $	 */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -40,7 +40,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: rtld.c,v 1.212.2.1 2023/08/01 15:13:02 martin Exp $");
+__RCSID("$NetBSD: rtld.c,v 1.212.2.2 2023/08/01 16:34:56 martin Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -1034,7 +1034,7 @@ dlopen(const char *name, int mode)
 	sigset_t mask;
 	int result;
 
-	dbg(("dlopen of %s %d", name, mode));
+	dbg(("dlopen of %s 0x%x", name, mode));
 
 	_rtld_exclusive_enter(&mask);
 
@@ -1091,6 +1091,9 @@ dlopen(const char *name, int mode)
 	_rtld_debug.r_state = RT_CONSISTENT;
 	_rtld_debug_state();
 
+	dbg(("dlopen of %s 0x%x returned %p%s%s%s", name, mode, obj,
+	    obj ? "" : " (", obj ? "" : error_message, obj ? "" : ")"));
+
 	_rtld_exclusive_exit(&mask);
 
 	return obj;
@@ -1565,6 +1568,7 @@ _rtld_error(const char *fmt,...)
 
 	va_start(ap, fmt);
 	xvsnprintf(buf, sizeof buf, fmt, ap);
+	dbg(("%s: %s", __func__, buf));
 	error_message = buf;
 	va_end(ap);
 }

Index: src/libexec/ld.elf_so/rtld.h
diff -u src/libexec/ld.elf_so/rtld.h:1.144 src/libexec/ld.elf_so/rtld.h:1.144.2.1
--- src/libexec/ld.elf_so/rtld.h:1.144	Tue Jun 21 06:52:17 2022
+++ src/libexec/ld.elf_so/rtld.h	Tue Aug  1 16:34:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: rtld.h,v 1.144 2022/06/21 06:52:17 skrll Exp $	 */
+/*	$NetBSD: rtld.h,v 1.144.2.1 2023/08/01 16:34:56 martin Exp $	 */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -219,7 +219,9 @@ typedef struct Struct_Obj_Entry {
 			phdr_loaded:1,	/* Phdr is loaded and doesn't need to
 					 * be freed. */
 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
-			tls_done:1,	/* True if static TLS offset
+			tls_static:1,	/* True if static TLS offset
+					 * has been allocated */
+			tls_dynamic:1,	/* True if any non-static DTV entry
 					 * has been allocated */
 #endif
 			ref_nodel:1,	/* Refcount increased to prevent dlclose */
@@ -483,7 +485,6 @@ _rtld_fetch_ventry(const Obj_Entry *obj,
 /* tls.c */
 void *_rtld_tls_get_addr(void *, size_t, size_t);
 void _rtld_tls_initial_allocation(void);
-void *_rtld_tls_module_allocate(size_t index);
 int _rtld_tls_offset_allocate(Obj_Entry *);
 void _rtld_tls_offset_free(Obj_Entry *);
 

Index: src/libexec/ld.elf_so/tls.c
diff -u src/libexec/ld.elf_so/tls.c:1.14 src/libexec/ld.elf_so/tls.c:1.14.8.1
--- src/libexec/ld.elf_so/tls.c:1.14	Tue Nov  5 22:22:42 2019
+++ src/libexec/ld.elf_so/tls.c	Tue Aug  1 16:34:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: tls.c,v 1.14 2019/11/05 22:22:42 joerg Exp $	*/
+/*	$NetBSD: tls.c,v 1.14.8.1 2023/08/01 16:34:56 martin Exp $	*/
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: tls.c,v 1.14 2019/11/05 22:22:42 joerg Exp $");
+__RCSID("$NetBSD: tls.c,v 1.14.8.1 2023/08/01 16:34:56 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/ucontext.h>
@@ -43,6 +43,7 @@ __RCSID("$NetBSD: tls.c,v 1.14 2019/11/0
 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
 
 static struct tls_tcb *_rtld_tls_allocate_locked(void);
+static void *_rtld_tls_module_allocate(struct tls_tcb *, size_t);
 
 #ifndef TLS_DTV_OFFSET
 #define	TLS_DTV_OFFSET	0
@@ -84,7 +85,7 @@ _rtld_tls_get_addr(void *tls, size_t idx
 	}
 
 	if (__predict_false(dtv[idx] == NULL))
-		dtv[idx] = _rtld_tls_module_allocate(idx);
+		dtv[idx] = _rtld_tls_module_allocate(tcb, idx);
 
 	_rtld_exclusive_exit(&mask);
 
@@ -129,21 +130,22 @@ _rtld_tls_allocate_locked(void)
 	tcb = (struct tls_tcb *)p;
 	tcb->tcb_self = tcb;
 #endif
-	dbg(("tcb %p", tcb));
+	dbg(("lwp %d tls tcb %p", _lwp_self(), tcb));
 	tcb->tcb_dtv = xcalloc(sizeof(*tcb->tcb_dtv) * (2 + _rtld_tls_max_index));
 	++tcb->tcb_dtv;
 	SET_DTV_MAX_INDEX(tcb->tcb_dtv, _rtld_tls_max_index);
 	SET_DTV_GENERATION(tcb->tcb_dtv, _rtld_tls_dtv_generation);
 
 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
-		if (obj->tls_done) {
+		if (obj->tls_static) {
 #ifdef __HAVE_TLS_VARIANT_I
 			q = p + obj->tlsoffset;
 #else
 			q = p - obj->tlsoffset;
 #endif
-			dbg(("obj %p dtv %p tlsoffset %zu",
-			    obj, q, obj->tlsoffset));
+			dbg(("%s: [lwp %d] tls dtv %p index %zu offset %zu",
+			    obj->path, _lwp_self(),
+			    q, obj->tlsindex, obj->tlsoffset));
 			if (obj->tlsinitsize)
 				memcpy(q, obj->tlsinit, obj->tlsinitsize);
 			tcb->tcb_dtv[obj->tlsindex] = q;
@@ -194,8 +196,8 @@ _rtld_tls_free(struct tls_tcb *tcb)
 	_rtld_exclusive_exit(&mask);
 }
 
-void *
-_rtld_tls_module_allocate(size_t idx)
+static void *
+_rtld_tls_module_allocate(struct tls_tcb *tcb, size_t idx)
 {
 	Obj_Entry *obj;
 	uint8_t *p;
@@ -208,11 +210,21 @@ _rtld_tls_module_allocate(size_t idx)
 		_rtld_error("Module for TLS index %zu missing", idx);
 		_rtld_die();
 	}
+	if (obj->tls_static) {
+#ifdef __HAVE_TLS_VARIANT_I
+		p = (uint8_t *)tcb + obj->tlsoffset + sizeof(struct tls_tcb);
+#else
+		p = (uint8_t *)tcb - obj->tlsoffset;
+#endif
+		return p;
+	}
 
 	p = xmalloc(obj->tlssize);
 	memcpy(p, obj->tlsinit, obj->tlsinitsize);
 	memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
 
+	obj->tls_dynamic = 1;
+
 	return p;
 }
 
@@ -221,11 +233,14 @@ _rtld_tls_offset_allocate(Obj_Entry *obj
 {
 	size_t offset, next_offset;
 
-	if (obj->tls_done)
+	if (obj->tls_dynamic)
+		return -1;
+
+	if (obj->tls_static)
 		return 0;
 	if (obj->tlssize == 0) {
 		obj->tlsoffset = 0;
-		obj->tls_done = 1;
+		obj->tls_static = 1;
 		return 0;
 	}
 
@@ -261,8 +276,10 @@ _rtld_tls_offset_allocate(Obj_Entry *obj
 		}
 	}
 	obj->tlsoffset = offset;
+	dbg(("%s: static tls offset 0x%zx size %zu\n",
+	    obj->path, obj->tlsoffset, obj->tlssize));
 	_rtld_tls_static_offset = next_offset;
-	obj->tls_done = 1;
+	obj->tls_static = 1;
 
 	return 0;
 }
@@ -274,7 +291,7 @@ _rtld_tls_offset_free(Obj_Entry *obj)
 	/*
 	 * XXX See above.
 	 */
-	obj->tls_done = 0;
+	obj->tls_static = 0;
 	return;
 }
 

Index: src/libexec/ld.elf_so/arch/aarch64/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/aarch64/mdreloc.c:1.17 src/libexec/ld.elf_so/arch/aarch64/mdreloc.c:1.17.2.1
--- src/libexec/ld.elf_so/arch/aarch64/mdreloc.c:1.17	Sat Dec  3 09:10:40 2022
+++ src/libexec/ld.elf_so/arch/aarch64/mdreloc.c	Tue Aug  1 16:34:58 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: mdreloc.c,v 1.17 2022/12/03 09:10:40 skrll Exp $ */
+/* $NetBSD: mdreloc.c,v 1.17.2.1 2023/08/01 16:34:58 martin Exp $ */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -60,7 +60,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.17 2022/12/03 09:10:40 skrll Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.17.2.1 2023/08/01 16:34:58 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -157,7 +157,7 @@ _rtld_tlsdesc_fill(const Obj_Entry *obj,
 	}
 	offs += rela->r_addend;
 
-	if (defobj->tls_done) {
+	if (defobj->tls_static) {
 		/* Variable is in initially allocated TLS segment */
 		where[0] = (Elf_Addr)_rtld_tlsdesc_static;
 		where[1] = defobj->tlsoffset + offs +
@@ -299,8 +299,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TLS_TYPE(TLS_TPREL):
-			if (!defobj->tls_done &&
-			    _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where = (Elf_Addr)(def->st_value + defobj->tlsoffset +

Index: src/libexec/ld.elf_so/arch/alpha/alpha_reloc.c
diff -u src/libexec/ld.elf_so/arch/alpha/alpha_reloc.c:1.43 src/libexec/ld.elf_so/arch/alpha/alpha_reloc.c:1.43.14.1
--- src/libexec/ld.elf_so/arch/alpha/alpha_reloc.c:1.43	Thu Aug 10 19:03:26 2017
+++ src/libexec/ld.elf_so/arch/alpha/alpha_reloc.c	Tue Aug  1 16:35:00 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: alpha_reloc.c,v 1.43 2017/08/10 19:03:26 joerg Exp $	*/
+/*	$NetBSD: alpha_reloc.c,v 1.43.14.1 2023/08/01 16:35:00 martin Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -62,7 +62,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: alpha_reloc.c,v 1.43 2017/08/10 19:03:26 joerg Exp $");
+__RCSID("$NetBSD: alpha_reloc.c,v 1.43.14.1 2023/08/01 16:35:00 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -280,8 +280,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TPREL64):
-			if (!defobj->tls_done &&
-			    _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			tmp = (Elf64_Addr)(def->st_value +

Index: src/libexec/ld.elf_so/arch/arm/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/arm/mdreloc.c:1.45 src/libexec/ld.elf_so/arch/arm/mdreloc.c:1.45.6.1
--- src/libexec/ld.elf_so/arch/arm/mdreloc.c:1.45	Tue Jun 16 21:02:20 2020
+++ src/libexec/ld.elf_so/arch/arm/mdreloc.c	Tue Aug  1 16:34:58 2023
@@ -1,8 +1,8 @@
-/*	$NetBSD: mdreloc.c,v 1.45 2020/06/16 21:02:20 joerg Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.45.6.1 2023/08/01 16:34:58 martin Exp $	*/
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.45 2020/06/16 21:02:20 joerg Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.45.6.1 2023/08/01 16:34:58 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -228,8 +228,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TLS_TPOFF32):
-			if (!defobj->tls_done &&
-			    _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			if (__predict_true(RELOC_ALIGNED_P(where)))

Index: src/libexec/ld.elf_so/arch/hppa/hppa_reloc.c
diff -u src/libexec/ld.elf_so/arch/hppa/hppa_reloc.c:1.49 src/libexec/ld.elf_so/arch/hppa/hppa_reloc.c:1.49.2.1
--- src/libexec/ld.elf_so/arch/hppa/hppa_reloc.c:1.49	Mon May 30 17:06:34 2022
+++ src/libexec/ld.elf_so/arch/hppa/hppa_reloc.c	Tue Aug  1 16:34:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: hppa_reloc.c,v 1.49 2022/05/30 17:06:34 skrll Exp $	*/
+/*	$NetBSD: hppa_reloc.c,v 1.49.2.1 2023/08/01 16:34:58 martin Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2004 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: hppa_reloc.c,v 1.49 2022/05/30 17:06:34 skrll Exp $");
+__RCSID("$NetBSD: hppa_reloc.c,v 1.49.2.1 2023/08/01 16:34:58 martin Exp $");
 #endif /* not lint */
 
 #include <stdlib.h>
@@ -553,7 +553,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TLS_TPREL32):
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where = (Elf_Addr)(defobj->tlsoffset + def->st_value +

Index: src/libexec/ld.elf_so/arch/i386/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/i386/mdreloc.c:1.41 src/libexec/ld.elf_so/arch/i386/mdreloc.c:1.41.12.1
--- src/libexec/ld.elf_so/arch/i386/mdreloc.c:1.41	Tue Apr  3 21:10:27 2018
+++ src/libexec/ld.elf_so/arch/i386/mdreloc.c	Tue Aug  1 16:34:59 2023
@@ -1,8 +1,8 @@
-/*	$NetBSD: mdreloc.c,v 1.41 2018/04/03 21:10:27 joerg Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.41.12.1 2023/08/01 16:34:59 martin Exp $	*/
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.41 2018/04/03 21:10:27 joerg Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.41.12.1 2023/08/01 16:34:59 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -15,6 +15,9 @@ void _rtld_bind_start(void);
 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
 
+#define rdbg_symname(obj, rela) \
+	((obj)->strtab + (obj)->symtab[ELF_R_SYM((rela)->r_info)].st_name)
+
 void
 _rtld_setup_pltgot(const Obj_Entry *obj)
 {
@@ -97,7 +100,7 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 
 			*where += target - (Elf_Addr)where;
 			rdbg(("PC32 %s in %s --> %p in %s",
-			    obj->strtab + obj->symtab[symnum].st_name,
+			    rdbg_symname(obj, rel),
 			    obj->path, (void *)*where, defobj->path));
 			break;
 
@@ -111,7 +114,7 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			if (*where != tmp)
 				*where = tmp;
 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
-			    obj->strtab + obj->symtab[symnum].st_name,
+			    rdbg_symname(obj, rel),
 			    obj->path, (void *)*where, defobj->path));
 			break;
 
@@ -147,25 +150,25 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TLS_TPOFF):
-			if (!defobj->tls_done &&
-			    _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where += (Elf_Addr)(def->st_value - defobj->tlsoffset);
 
 			rdbg(("TLS_TPOFF %s in %s --> %p",
-			    obj->strtab + obj->symtab[symnum].st_name,
+			    rdbg_symname(obj, rel),
 			    obj->path, (void *)*where));
 			break;
 
 		case R_TYPE(TLS_TPOFF32):
-			if (!defobj->tls_done &&
-			    _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where += (Elf_Addr)(defobj->tlsoffset - def->st_value);
 			rdbg(("TLS_TPOFF32 %s in %s --> %p",
-			    obj->strtab + obj->symtab[symnum].st_name,
+			    rdbg_symname(obj, rel),
 			    obj->path, (void *)*where));
 			break;
 
@@ -173,7 +176,7 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			*where = (Elf_Addr)(defobj->tlsindex);
 
 			rdbg(("TLS_DTPMOD32 %s in %s --> %p",
-			    obj->strtab + obj->symtab[symnum].st_name,
+			    rdbg_symname(obj, rel),
 			    obj->path, (void *)*where));
 			break;
 
@@ -181,7 +184,7 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			*where = (Elf_Addr)(def->st_value);
 
 			rdbg(("TLS_DTPOFF32 %s in %s --> %p",
-			    obj->strtab + obj->symtab[symnum].st_name,
+			    rdbg_symname(obj, rel),
 			    obj->path, (void *)*where));
 
 			break;
@@ -192,7 +195,7 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			    (u_long)ELF_R_SYM(rel->r_info),
 			    (u_long)ELF_R_TYPE(rel->r_info),
 			    (void *)rel->r_offset, (void *)*where,
-			    obj->strtab + obj->symtab[symnum].st_name));
+			    rdbg_symname(obj, rel)));
 			_rtld_error("%s: Unsupported relocation type %ld "
 			    "in non-PLT relocations",
 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));

Index: src/libexec/ld.elf_so/arch/m68k/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/m68k/mdreloc.c:1.33 src/libexec/ld.elf_so/arch/m68k/mdreloc.c:1.33.14.1
--- src/libexec/ld.elf_so/arch/m68k/mdreloc.c:1.33	Thu Aug 10 19:03:26 2017
+++ src/libexec/ld.elf_so/arch/m68k/mdreloc.c	Tue Aug  1 16:34:59 2023
@@ -1,8 +1,8 @@
-/*	$NetBSD: mdreloc.c,v 1.33 2017/08/10 19:03:26 joerg Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.33.14.1 2023/08/01 16:34:59 martin Exp $	*/
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.33 2017/08/10 19:03:26 joerg Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.33.14.1 2023/08/01 16:34:59 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -142,9 +142,6 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TLS_DTPREL32):
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
-				return -1;
-
 			*where = (Elf_Addr)(def->st_value + rela->r_addend
 			    - TLS_DTV_OFFSET);
 			rdbg(("DTPREL32 %s in %s --> %p in %s",
@@ -153,7 +150,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TLS_TPREL32):
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where = (Elf_Addr)(def->st_value + rela->r_addend

Index: src/libexec/ld.elf_so/arch/mips/mips_reloc.c
diff -u src/libexec/ld.elf_so/arch/mips/mips_reloc.c:1.74 src/libexec/ld.elf_so/arch/mips/mips_reloc.c:1.74.6.1
--- src/libexec/ld.elf_so/arch/mips/mips_reloc.c:1.74	Sat Mar  6 20:11:08 2021
+++ src/libexec/ld.elf_so/arch/mips/mips_reloc.c	Tue Aug  1 16:34:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: mips_reloc.c,v 1.74 2021/03/06 20:11:08 christos Exp $	*/
+/*	$NetBSD: mips_reloc.c,v 1.74.6.1 2023/08/01 16:34:56 martin Exp $	*/
 
 /*
  * Copyright 1997 Michael L. Hitch <mhi...@montana.edu>
@@ -30,7 +30,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mips_reloc.c,v 1.74 2021/03/06 20:11:08 christos Exp $");
+__RCSID("$NetBSD: mips_reloc.c,v 1.74.6.1 2023/08/01 16:34:56 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -429,9 +429,6 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			Elf_Addr old = load_ptr(where, ELFSIZE / 8);
 			Elf_Addr val = old;
 
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
-				return -1;
-
 			val += (Elf_Addr)def->st_value - TLS_DTV_OFFSET;
 			store_ptr(where, val, ELFSIZE / 8);
 
@@ -450,7 +447,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			Elf_Addr old = load_ptr(where, ELFSIZE / 8);
 			Elf_Addr val = old;
 
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			val += (Elf_Addr)(def->st_value + defobj->tlsoffset

Index: src/libexec/ld.elf_so/arch/or1k/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/or1k/mdreloc.c:1.3 src/libexec/ld.elf_so/arch/or1k/mdreloc.c:1.3.14.1
--- src/libexec/ld.elf_so/arch/or1k/mdreloc.c:1.3	Thu Aug 10 19:03:26 2017
+++ src/libexec/ld.elf_so/arch/or1k/mdreloc.c	Tue Aug  1 16:35:00 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: mdreloc.c,v 1.3 2017/08/10 19:03:26 joerg Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.3.14.1 2023/08/01 16:35:00 martin Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.3 2017/08/10 19:03:26 joerg Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.3.14.1 2023/08/01 16:35:00 martin Exp $");
 #endif /* not lint */
 
 #include <stdarg.h>
@@ -171,9 +171,6 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TLS_DTPOFF):
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
-				return -1;
-
 			*where = (Elf_Addr)(def->st_value + rela->r_addend
 			    - TLS_DTV_OFFSET);
 			rdbg(("DTPOFF %s in %s --> %p in %s",
@@ -182,7 +179,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TLS_TPOFF):
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where = (Elf_Addr)(def->st_value + rela->r_addend

Index: src/libexec/ld.elf_so/arch/powerpc/ppc_reloc.c
diff -u src/libexec/ld.elf_so/arch/powerpc/ppc_reloc.c:1.62 src/libexec/ld.elf_so/arch/powerpc/ppc_reloc.c:1.62.2.1
--- src/libexec/ld.elf_so/arch/powerpc/ppc_reloc.c:1.62	Wed Jul  6 17:35:20 2022
+++ src/libexec/ld.elf_so/arch/powerpc/ppc_reloc.c	Tue Aug  1 16:34:59 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: ppc_reloc.c,v 1.62 2022/07/06 17:35:20 martin Exp $	*/
+/*	$NetBSD: ppc_reloc.c,v 1.62.2.1 2023/08/01 16:34:59 martin Exp $	*/
 
 /*-
  * Copyright (C) 1998	Tsubai Masanari
@@ -30,7 +30,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: ppc_reloc.c,v 1.62 2022/07/06 17:35:20 martin Exp $");
+__RCSID("$NetBSD: ppc_reloc.c,v 1.62.2.1 2023/08/01 16:34:59 martin Exp $");
 #endif /* not lint */
 
 #include <stdarg.h>
@@ -321,7 +321,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TPREL):
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where = (Elf_Addr)(def->st_value + rela->r_addend

Index: src/libexec/ld.elf_so/arch/riscv/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/riscv/mdreloc.c:1.7 src/libexec/ld.elf_so/arch/riscv/mdreloc.c:1.7.2.1
--- src/libexec/ld.elf_so/arch/riscv/mdreloc.c:1.7	Mon Dec  5 07:26:25 2022
+++ src/libexec/ld.elf_so/arch/riscv/mdreloc.c	Tue Aug  1 16:34:59 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: mdreloc.c,v 1.7 2022/12/05 07:26:25 skrll Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.7.2.1 2023/08/01 16:34:59 martin Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.7 2022/12/05 07:26:25 skrll Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.7.2.1 2023/08/01 16:34:59 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -168,7 +168,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			Elf_Addr old = *where;
 			Elf_Addr val = old;
 
-			if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_done &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			val = (Elf_Addr)def->st_value - TLS_DTV_OFFSET;

Index: src/libexec/ld.elf_so/arch/sh3/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/sh3/mdreloc.c:1.35 src/libexec/ld.elf_so/arch/sh3/mdreloc.c:1.35.14.1
--- src/libexec/ld.elf_so/arch/sh3/mdreloc.c:1.35	Thu Aug 10 19:03:26 2017
+++ src/libexec/ld.elf_so/arch/sh3/mdreloc.c	Tue Aug  1 16:34:56 2023
@@ -1,8 +1,8 @@
-/*	$NetBSD: mdreloc.c,v 1.35 2017/08/10 19:03:26 joerg Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.35.14.1 2023/08/01 16:34:56 martin Exp $	*/
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.35 2017/08/10 19:03:26 joerg Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.35.14.1 2023/08/01 16:34:56 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -173,8 +173,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 			break;
 
 		case R_TYPE(TLS_TPOFF32):
-			if (!defobj->tls_done &&
-			    _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where = (Elf_Addr)def->st_value +

Index: src/libexec/ld.elf_so/arch/sparc/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/sparc/mdreloc.c:1.55 src/libexec/ld.elf_so/arch/sparc/mdreloc.c:1.55.12.1
--- src/libexec/ld.elf_so/arch/sparc/mdreloc.c:1.55	Tue Apr  3 21:10:27 2018
+++ src/libexec/ld.elf_so/arch/sparc/mdreloc.c	Tue Aug  1 16:34:59 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: mdreloc.c,v 1.55 2018/04/03 21:10:27 joerg Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.55.12.1 2023/08/01 16:34:59 martin Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.55 2018/04/03 21:10:27 joerg Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.55.12.1 2023/08/01 16:34:59 martin Exp $");
 #endif /* not lint */
 
 #include <machine/elf_support.h>
@@ -282,9 +282,9 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 				break;
 
 			case R_TYPE(TLS_TPOFF32):
-				if (!defobj->tls_done &&
-					_rtld_tls_offset_allocate(obj))
-					     return -1;
+				if (!defobj->tls_static &&
+				    _rtld_tls_offset_allocate(__UNCONST(defobj)))
+					return -1;
 
 				*where = (Elf_Addr)(def->st_value -
 				    defobj->tlsoffset + rela->r_addend);

Index: src/libexec/ld.elf_so/arch/sparc64/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/sparc64/mdreloc.c:1.69 src/libexec/ld.elf_so/arch/sparc64/mdreloc.c:1.69.12.1
--- src/libexec/ld.elf_so/arch/sparc64/mdreloc.c:1.69	Tue Apr  3 21:10:27 2018
+++ src/libexec/ld.elf_so/arch/sparc64/mdreloc.c	Tue Aug  1 16:34:57 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: mdreloc.c,v 1.69 2018/04/03 21:10:27 joerg Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.69.12.1 2023/08/01 16:34:57 martin Exp $	*/
 
 /*-
  * Copyright (c) 2000 Eduardo Horvath.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.69 2018/04/03 21:10:27 joerg Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.69.12.1 2023/08/01 16:34:57 martin Exp $");
 #endif /* not lint */
 
 #include <machine/elf_support.h>
@@ -383,9 +383,9 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
 				break;
 
 			case R_TYPE(TLS_TPOFF64):
-				if (!defobj->tls_done &&
-					_rtld_tls_offset_allocate(obj))
-					     return -1;
+				if (!defobj->tls_static &&
+				    _rtld_tls_offset_allocate(__UNCONST(defobj)))
+					return -1;
 
 				*where = (Elf64_Addr)(def->st_value -
 				    defobj->tlsoffset + rela->r_addend);

Index: src/libexec/ld.elf_so/arch/x86_64/mdreloc.c
diff -u src/libexec/ld.elf_so/arch/x86_64/mdreloc.c:1.47 src/libexec/ld.elf_so/arch/x86_64/mdreloc.c:1.47.12.1
--- src/libexec/ld.elf_so/arch/x86_64/mdreloc.c:1.47	Tue Apr  3 21:10:27 2018
+++ src/libexec/ld.elf_so/arch/x86_64/mdreloc.c	Tue Aug  1 16:34:57 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: mdreloc.c,v 1.47 2018/04/03 21:10:27 joerg Exp $	*/
+/*	$NetBSD: mdreloc.c,v 1.47.12.1 2023/08/01 16:34:57 martin Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -68,7 +68,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdreloc.c,v 1.47 2018/04/03 21:10:27 joerg Exp $");
+__RCSID("$NetBSD: mdreloc.c,v 1.47.12.1 2023/08/01 16:34:57 martin Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -226,8 +226,8 @@ _rtld_relocate_nonplt_objects(Obj_Entry 
        			break;
 
 		case R_TYPE(TPOFF64):
-			if (!defobj->tls_done &&
-			    _rtld_tls_offset_allocate(obj))
+			if (!defobj->tls_static &&
+			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
 				return -1;
 
 			*where64 = (Elf64_Addr)(def->st_value -

Index: src/tests/libexec/ld.elf_so/Makefile
diff -u src/tests/libexec/ld.elf_so/Makefile:1.12 src/tests/libexec/ld.elf_so/Makefile:1.12.4.1
--- src/tests/libexec/ld.elf_so/Makefile:1.12	Sun May 30 02:26:51 2021
+++ src/tests/libexec/ld.elf_so/Makefile	Tue Aug  1 16:34:58 2023
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.12 2021/05/30 02:26:51 joerg Exp $
+# $NetBSD: Makefile,v 1.12.4.1 2023/08/01 16:34:58 martin Exp $
 #
 
 NOMAN=		# defined
@@ -13,10 +13,23 @@ SUBDIR+=	helper_dso1 helper_dso3 .WAIT h
 		helper_symver_dso2 .WAIT \
 		data
 
+SUBDIR+=	helper_def_dynamic
+SUBDIR+=	helper_def_static
+SUBDIR+=	helper_onlydef
+SUBDIR+=	.WAIT
+SUBDIR+=	helper_abuse_dynamic
+SUBDIR+=	helper_abuse_static
+SUBDIR+=	helper_onlyctor_dynamic
+SUBDIR+=	helper_onlyuse_dynamic
+SUBDIR+=	helper_onlyuse_static
+SUBDIR+=	helper_use_dynamic
+SUBDIR+=	helper_use_static
+
 TESTSDIR=	${TESTSBASE}/libexec/ld.elf_so
 
 TESTS_C+=	t_dlerror-cleared t_dlerror-false t_dlinfo t_dlvsym t_ifunc
 TESTS_C+=	t_rtld_r_debug
+TESTS_C+=	t_tls_extern
 
 COPTS.t_rtld_r_debug.c += ${${ACTIVE_CC} == "gcc" :? -Wno-maybe-uninitialized :}
 
@@ -24,6 +37,7 @@ LDADD.t_dlerror-false=	-Wl,-rpath,/var/n
 LDADD.t_dlvsym=		-Wl,-rpath,${TESTSDIR}/h_helper_symver_dso2
 LDADD.t_ifunc=		-Wl,-rpath,${TESTSDIR} -lutil
 DPADD.t_ifunc=		${LIBUTIL}
+LDADD.t_tls_extern+=	-Wl,-rpath,${TESTSDIR}
 
 TESTS_SH+=		t_df_1_noopen t_dl_symver t_thread_local_dtor
 

Index: src/tests/libexec/ld.elf_so/helper_dso1/Makefile
diff -u src/tests/libexec/ld.elf_so/helper_dso1/Makefile:1.1 src/tests/libexec/ld.elf_so/helper_dso1/Makefile:1.1.54.1
--- src/tests/libexec/ld.elf_so/helper_dso1/Makefile:1.1	Thu Mar 31 23:34:48 2011
+++ src/tests/libexec/ld.elf_so/helper_dso1/Makefile	Tue Aug  1 16:35:00 2023
@@ -1,19 +1,6 @@
-# $NetBSD: Makefile,v 1.1 2011/03/31 23:34:48 joerg Exp $
+# $NetBSD: Makefile,v 1.1.54.1 2023/08/01 16:35:00 martin Exp $
 
-.include <bsd.own.mk>
+LIB=		h_helper_dso1
+SRCS=		h_helper_dso1.c
 
-LIB=			h_helper_dso1
-SRCS=			h_helper_dso1.c
-
-LIBDIR=		${TESTSBASE}/libexec/ld.elf_so
-SHLIBDIR=	${TESTSBASE}/libexec/ld.elf_so
-SHLIB_MAJOR=	1
-
-MKSTATICLIB=	no
-MKPROFILE=	no
-MKPICINSTALL=	no
-MKLINT=		no
-
-NOMAN=		# defined
-
-.include <bsd.lib.mk>
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_dso2/Makefile
diff -u src/tests/libexec/ld.elf_so/helper_dso2/Makefile:1.1 src/tests/libexec/ld.elf_so/helper_dso2/Makefile:1.1.54.1
--- src/tests/libexec/ld.elf_so/helper_dso2/Makefile:1.1	Thu Mar 31 23:34:48 2011
+++ src/tests/libexec/ld.elf_so/helper_dso2/Makefile	Tue Aug  1 16:34:57 2023
@@ -1,22 +1,10 @@
-# $NetBSD: Makefile,v 1.1 2011/03/31 23:34:48 joerg Exp $
+# $NetBSD: Makefile,v 1.1.54.1 2023/08/01 16:34:57 martin Exp $
 
-.include <bsd.own.mk>
+LIB=		h_helper_dso2
+SRCS=		h_helper_dso2.c
 
-LIB=			h_helper_dso2
-SRCS=			h_helper_dso2.c
-
-DSO1DIR!=	cd ${.CURDIR}/../helper_dso1 && ${PRINTOBJDIR}
+DSO1DIR_cmd=	cd ${.CURDIR}/../helper_dso1 && ${PRINTOBJDIR}
+DSO1DIR=	${DSO1DIR_cmd:sh}
 LDADD+=		-Wl,-rpath,${TESTSDIR} -L${DSO1DIR} -lh_helper_dso1
 
-LIBDIR=		${TESTSBASE}/libexec/ld.elf_so
-SHLIBDIR=	${TESTSBASE}/libexec/ld.elf_so
-SHLIB_MAJOR=	1
-
-MKSTATICLIB=	no
-MKPROFILE=	no
-MKPICINSTALL=	no
-MKLINT=		no
-
-NOMAN=		# defined
-
-.include <bsd.lib.mk>
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_dso3/Makefile
diff -u src/tests/libexec/ld.elf_so/helper_dso3/Makefile:1.1 src/tests/libexec/ld.elf_so/helper_dso3/Makefile:1.1.18.1
--- src/tests/libexec/ld.elf_so/helper_dso3/Makefile:1.1	Tue Jul 11 15:21:36 2017
+++ src/tests/libexec/ld.elf_so/helper_dso3/Makefile	Tue Aug  1 16:34:58 2023
@@ -1,22 +1,9 @@
-# $NetBSD: Makefile,v 1.1 2017/07/11 15:21:36 joerg Exp $
+# $NetBSD: Makefile,v 1.1.18.1 2023/08/01 16:34:58 martin Exp $
 
-.include <bsd.own.mk>
-
-LIB=			h_helper_dso3
+LIB=		h_helper_dso3
 LIBISCXX=	yes
-SRCS=			h_helper_dso3.cpp
-
-LIBDIR=		${TESTSBASE}/libexec/ld.elf_so
-SHLIBDIR=	${TESTSBASE}/libexec/ld.elf_so
-SHLIB_MAJOR=	1
-
-MKSTATICLIB=	no
-MKPROFILE=	no
-MKPICINSTALL=	no
-MKLINT=		no
-
-NOMAN=		# defined
+SRCS=		h_helper_dso3.cpp
 
 CXXFLAGS+=	-std=c++11
 
-.include <bsd.lib.mk>
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile
diff -u src/tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile:1.1 src/tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile:1.1.26.1
--- src/tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile:1.1	Mon Aug 25 20:40:53 2014
+++ src/tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile	Tue Aug  1 16:34:57 2023
@@ -1,19 +1,6 @@
-# $NetBSD: Makefile,v 1.1 2014/08/25 20:40:53 joerg Exp $
+# $NetBSD: Makefile,v 1.1.26.1 2023/08/01 16:34:57 martin Exp $
 
-.include <bsd.own.mk>
+LIB=		h_helper_ifunc_dso
+SRCS=		h_helper_ifunc.c
 
-LIB=			h_helper_ifunc_dso
-SRCS=			h_helper_ifunc.c
-
-LIBDIR=		${TESTSBASE}/libexec/ld.elf_so
-SHLIBDIR=	${TESTSBASE}/libexec/ld.elf_so
-SHLIB_MAJOR=	1
-
-MKSTATICLIB=	no
-MKPROFILE=	no
-MKPICINSTALL=	no
-MKLINT=		no
-
-NOMAN=		# defined
-
-.include <bsd.lib.mk>
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_symver_dso0/Makefile
diff -u src/tests/libexec/ld.elf_so/helper_symver_dso0/Makefile:1.1 src/tests/libexec/ld.elf_so/helper_symver_dso0/Makefile:1.1.52.1
--- src/tests/libexec/ld.elf_so/helper_symver_dso0/Makefile:1.1	Sat Jun 25 05:45:13 2011
+++ src/tests/libexec/ld.elf_so/helper_symver_dso0/Makefile	Tue Aug  1 16:34:57 2023
@@ -1,6 +1,4 @@
-# $NetBSD: Makefile,v 1.1 2011/06/25 05:45:13 nonaka Exp $
-
-.include <bsd.own.mk>
+# $NetBSD: Makefile,v 1.1.52.1 2023/08/01 16:34:57 martin Exp $
 
 LIB=		h_helper_symver_dso
 SRCS=		h_helper_symver_dso0.c
@@ -9,13 +7,6 @@ LIBDIR=		${TESTSBASE}/libexec/ld.elf_so/
 SHLIBDIR=	${TESTSBASE}/libexec/ld.elf_so/h_helper_symver_dso0
 SHLIB_MAJOR=	1
 
-MKSTATICLIB=	no
-MKPROFILE=	no
-MKPICINSTALL=	no
-MKLINT=		no
-
-NOMAN=		# defined
-
 FILESDIR=	${TESTSBASE}/libexec/ld.elf_so/h_helper_symver_dso0
 
-.include <bsd.lib.mk>
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_symver_dso1/Makefile
diff -u src/tests/libexec/ld.elf_so/helper_symver_dso1/Makefile:1.3 src/tests/libexec/ld.elf_so/helper_symver_dso1/Makefile:1.3.36.1
--- src/tests/libexec/ld.elf_so/helper_symver_dso1/Makefile:1.3	Fri Feb 14 20:33:32 2014
+++ src/tests/libexec/ld.elf_so/helper_symver_dso1/Makefile	Tue Aug  1 16:34:59 2023
@@ -1,6 +1,4 @@
-# $NetBSD: Makefile,v 1.3 2014/02/14 20:33:32 joerg Exp $
-
-.include <bsd.own.mk>
+# $NetBSD: Makefile,v 1.3.36.1 2023/08/01 16:34:59 martin Exp $
 
 LIB=		h_helper_symver_dso
 SRCS=		h_helper_symver_dso1.c
@@ -9,16 +7,9 @@ LIBDIR=		${TESTSBASE}/libexec/ld.elf_so/
 SHLIBDIR=	${TESTSBASE}/libexec/ld.elf_so/h_helper_symver_dso1
 SHLIB_MAJOR=	1
 
-MKSTATICLIB=	no
-MKPROFILE=	no
-MKPICINSTALL=	no
-MKLINT=		no
-
-NOMAN=		# defined
-
 FILESDIR=	${TESTSBASE}/libexec/ld.elf_so/h_helper_symver_dso1
 
 LDFLAGS.lib${LIB} += -Wl,--version-script=${.CURDIR}/h_helper_symver_dso1.map
 
-.include <bsd.lib.mk>
+.include "../helper.mk"
 

Index: src/tests/libexec/ld.elf_so/helper_symver_dso2/Makefile
diff -u src/tests/libexec/ld.elf_so/helper_symver_dso2/Makefile:1.3 src/tests/libexec/ld.elf_so/helper_symver_dso2/Makefile:1.3.36.1
--- src/tests/libexec/ld.elf_so/helper_symver_dso2/Makefile:1.3	Fri Feb 14 20:33:32 2014
+++ src/tests/libexec/ld.elf_so/helper_symver_dso2/Makefile	Tue Aug  1 16:34:59 2023
@@ -1,6 +1,4 @@
-# $NetBSD: Makefile,v 1.3 2014/02/14 20:33:32 joerg Exp $
-
-.include <bsd.own.mk>
+# $NetBSD: Makefile,v 1.3.36.1 2023/08/01 16:34:59 martin Exp $
 
 LIB=		h_helper_symver_dso
 SRCS=		h_helper_symver_dso2.c
@@ -9,15 +7,8 @@ LIBDIR=		${TESTSBASE}/libexec/ld.elf_so/
 SHLIBDIR=	${TESTSBASE}/libexec/ld.elf_so/h_helper_symver_dso2
 SHLIB_MAJOR=	1
 
-MKSTATICLIB=	no
-MKPROFILE=	no
-MKPICINSTALL=	no
-MKLINT=		no
-
-NOMAN=		# defined
-
 FILESDIR=	${TESTSBASE}/libexec/ld.elf_so/h_helper_symver_dso2
 
 LDFLAGS.lib${LIB} += -Wl,--version-script=${.CURDIR}/h_helper_symver_dso2.map
 
-.include <bsd.lib.mk>
+.include "../helper.mk"

Added files:

Index: src/tests/libexec/ld.elf_so/helper.mk
diff -u /dev/null src/tests/libexec/ld.elf_so/helper.mk:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper.mk	Tue Aug  1 16:34:58 2023
@@ -0,0 +1,18 @@
+#	$NetBSD: helper.mk,v 1.1.2.2 2023/08/01 16:34:58 martin Exp $
+
+# Makefile fragment for building a helper library used by ld.elf_so
+# tests.  All we need to install is lib${LIB}.so and lib${LIB}.so.1.
+# No man page, no lint library, no static libraries of any sort.
+
+LIBDIR?=	${TESTSBASE}/libexec/ld.elf_so
+SHLIBDIR?=	${TESTSBASE}/libexec/ld.elf_so
+SHLIB_MAJOR?=	1
+
+NODEBUGLIB=	# defined
+NOLINT=		# defined
+NOMAN=		# defined
+NOPICINSTALL=	# defined
+NOPROFILE=	# defined
+NOSTATICLIB=	# defined
+
+.include <bsd.lib.mk>

Index: src/tests/libexec/ld.elf_so/t_tls_extern.c
diff -u /dev/null src/tests/libexec/ld.elf_so/t_tls_extern.c:1.12.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/t_tls_extern.c	Tue Aug  1 16:34:58 2023
@@ -0,0 +1,410 @@
+/*	$NetBSD: t_tls_extern.c,v 1.12.2.2 2023/08/01 16:34:58 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+
+#include <atf-c.h>
+#include <dlfcn.h>
+
+#define	ATF_REQUIRE_DL(x)	ATF_REQUIRE_MSG(x, "%s: %s", #x, dlerror())
+
+enum order {
+	DEF_USE_EAGER,
+	DEF_USE_LAZY,
+	USE_DEF,
+	USE_DEF_NOLOAD,
+};
+
+static void
+tls_extern(const char *libdef, const char *libuse, enum order order)
+{
+	void *def, *use;
+	int *(*fdef)(void), *(*fuse)(void);
+	int *pdef, *puse;
+
+	(void)dlerror();
+
+	switch (order) {
+	case DEF_USE_EAGER:
+		ATF_REQUIRE_DL(def = dlopen(libdef, 0));
+		ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
+		pdef = (*fdef)();
+		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
+		ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
+		puse = (*fuse)();
+		break;
+	case DEF_USE_LAZY:
+		ATF_REQUIRE_DL(def = dlopen(libdef, 0));
+		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
+		goto lazy;
+	case USE_DEF:
+		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
+		ATF_REQUIRE_DL(def = dlopen(libdef, 0));
+		goto lazy;
+	case USE_DEF_NOLOAD:
+		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
+		ATF_REQUIRE_DL(def = dlopen(libdef, RTLD_NOLOAD));
+lazy:		ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
+		ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
+		pdef = (*fdef)();
+		puse = (*fuse)();
+		break;
+	}
+
+	ATF_CHECK_EQ_MSG(pdef, puse,
+	    "%p in defining library != %p in using library",
+	    pdef, puse);
+}
+
+ATF_TC(dynamic_abusedef);
+ATF_TC_HEAD(dynamic_abusedef, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " loading static use than dynamic def");
+}
+ATF_TC_BODY(dynamic_abusedef, tc)
+{
+	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so", USE_DEF);
+}
+
+ATF_TC(dynamic_abusedefnoload);
+ATF_TC_HEAD(dynamic_abusedefnoload, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " loading static use then dynamic def with RTLD_NOLOAD");
+}
+ATF_TC_BODY(dynamic_abusedefnoload, tc)
+{
+	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so",
+	    USE_DEF_NOLOAD);
+}
+
+ATF_TC(dynamic_defabuse_eager);
+ATF_TC_HEAD(dynamic_defabuse_eager, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "dlopen refuses extern __thread for TLS,"
+	    " loading dynamic def then static use eagerly");
+}
+ATF_TC_BODY(dynamic_defabuse_eager, tc)
+{
+	void *def;
+	int *(*fdef)(void);
+
+	ATF_REQUIRE_DL(def = dlopen("libh_def_dynamic.so", 0));
+	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
+	(void)(*fdef)();
+	ATF_CHECK_EQ_MSG(NULL, dlopen("libh_abuse_dynamic.so", 0),
+	    "dlopen failed to detect static-then-dynamic abuse");
+}
+
+ATF_TC(dynamic_defabuse_lazy);
+ATF_TC_HEAD(dynamic_defabuse_lazy, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " loading dynamic def then static use lazily");
+}
+ATF_TC_BODY(dynamic_defabuse_lazy, tc)
+{
+	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so",
+	    DEF_USE_LAZY);
+}
+
+ATF_TC(dynamic_defuse_eager);
+ATF_TC_HEAD(dynamic_defuse_eager, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
+	    " loading def then use eagerly");
+}
+ATF_TC_BODY(dynamic_defuse_eager, tc)
+{
+	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
+	    DEF_USE_EAGER);
+}
+
+ATF_TC(dynamic_defuse_lazy);
+ATF_TC_HEAD(dynamic_defuse_lazy, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
+	    " loading def then use lazyly");
+}
+ATF_TC_BODY(dynamic_defuse_lazy, tc)
+{
+	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
+	    DEF_USE_LAZY);
+}
+
+ATF_TC(dynamic_usedef);
+ATF_TC_HEAD(dynamic_usedef, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
+	    " loading use then def");
+}
+ATF_TC_BODY(dynamic_usedef, tc)
+{
+	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
+	    USE_DEF);
+}
+
+ATF_TC(dynamic_usedefnoload);
+ATF_TC_HEAD(dynamic_usedefnoload, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
+	    " loading use then def with RTLD_NOLOAD");
+}
+ATF_TC_BODY(dynamic_usedefnoload, tc)
+{
+	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
+	    USE_DEF_NOLOAD);
+}
+
+ATF_TC(static_abusedef);
+ATF_TC_HEAD(static_abusedef, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " loading dynamic use then static def");
+}
+ATF_TC_BODY(static_abusedef, tc)
+{
+	tls_extern("libh_def_static.so", "libh_abuse_static.so", USE_DEF);
+}
+
+ATF_TC(static_abusedefnoload);
+ATF_TC_HEAD(static_abusedefnoload, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " loading dynamic use then static def with RTLD_NOLOAD");
+}
+ATF_TC_BODY(static_abusedefnoload, tc)
+{
+	tls_extern("libh_def_static.so", "libh_abuse_static.so",
+	    USE_DEF_NOLOAD);
+}
+
+ATF_TC(static_defabuse_eager);
+ATF_TC_HEAD(static_defabuse_eager, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " loading static def then dynamic use eagerly");
+}
+ATF_TC_BODY(static_defabuse_eager, tc)
+{
+	tls_extern("libh_def_static.so", "libh_abuse_static.so",
+	    DEF_USE_EAGER);
+}
+
+ATF_TC(static_defabuse_lazy);
+ATF_TC_HEAD(static_defabuse_lazy, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " loading static def then dynamic use lazyly");
+}
+ATF_TC_BODY(static_defabuse_lazy, tc)
+{
+	tls_extern("libh_def_static.so", "libh_abuse_static.so",
+	    DEF_USE_LAZY);
+}
+
+ATF_TC(static_defuse_eager);
+ATF_TC_HEAD(static_defuse_eager, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
+	    " loading def then use eagerly");
+}
+ATF_TC_BODY(static_defuse_eager, tc)
+{
+	tls_extern("libh_def_static.so", "libh_use_static.so",
+	    DEF_USE_EAGER);
+}
+
+ATF_TC(static_defuse_lazy);
+ATF_TC_HEAD(static_defuse_lazy, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
+	    " loading def then use lazyly");
+}
+ATF_TC_BODY(static_defuse_lazy, tc)
+{
+	tls_extern("libh_def_static.so", "libh_use_static.so",
+	    DEF_USE_LAZY);
+}
+
+ATF_TC(static_usedef);
+ATF_TC_HEAD(static_usedef, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
+	    " loading use then def");
+}
+ATF_TC_BODY(static_usedef, tc)
+{
+	tls_extern("libh_def_static.so", "libh_use_static.so",
+	    USE_DEF);
+}
+
+ATF_TC(static_usedefnoload);
+ATF_TC_HEAD(static_usedefnoload, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
+	    " loading use then def with RTLD_NOLOAD");
+}
+ATF_TC_BODY(static_usedefnoload, tc)
+{
+	tls_extern("libh_def_static.so", "libh_use_static.so",
+	    USE_DEF_NOLOAD);
+}
+
+ATF_TC(onlydef_dynamic_static_ctor);
+ATF_TC_HEAD(onlydef_dynamic_static_ctor, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "definition-only library,"
+	    " dynamic load and use in ctor, then static load fails");
+}
+ATF_TC_BODY(onlydef_dynamic_static_ctor, tc)
+{
+
+	ATF_REQUIRE_DL(dlopen("libh_onlydef.so", 0));
+	ATF_REQUIRE_DL(dlopen("libh_onlyctor_dynamic.so", 0));
+	ATF_CHECK_EQ_MSG(NULL, dlopen("libh_onlyuse_static.so", 0),
+	    "dlopen failed to detect dynamic-then-static abuse");
+}
+
+ATF_TC(onlydef_dynamic_static_eager);
+ATF_TC_HEAD(onlydef_dynamic_static_eager, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "definition-only library,"
+	    " dynamic load and use, then static load fails");
+}
+ATF_TC_BODY(onlydef_dynamic_static_eager, tc)
+{
+	void *use_dynamic;
+	int *(*fdynamic)(void);
+
+	ATF_REQUIRE_DL(use_dynamic = dlopen("libh_onlyuse_dynamic.so", 0));
+	ATF_REQUIRE_DL(fdynamic = dlsym(use_dynamic, "fdynamic"));
+	(void)(*fdynamic)();
+	ATF_CHECK_EQ_MSG(NULL, dlopen("libh_onlyuse_static.so", 0),
+	    "dlopen failed to detect dynamic-then-static abuse");
+}
+
+ATF_TC(onlydef_dynamic_static_lazy);
+ATF_TC_HEAD(onlydef_dynamic_static_lazy, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " with definition-only library, dynamic and static load and use");
+}
+ATF_TC_BODY(onlydef_dynamic_static_lazy, tc)
+{
+	void *use_dynamic, *use_static;
+	int *(*fdynamic)(void), *(*fstatic)(void);
+	int *pdynamic, *pstatic;
+
+	ATF_REQUIRE_DL(use_dynamic = dlopen("libh_onlyuse_dynamic.so", 0));
+	ATF_REQUIRE_DL(use_static = dlopen("libh_onlyuse_static.so", 0));
+	ATF_REQUIRE_DL(fdynamic = dlsym(use_dynamic, "fdynamic"));
+	ATF_REQUIRE_DL(fstatic = dlsym(use_static, "fstatic"));
+	pdynamic = (*fdynamic)();
+	pstatic = (*fstatic)();
+	ATF_CHECK_EQ_MSG(pdynamic, pstatic,
+	    "%p in dynamic tls user != %p in static tls user",
+	    pdynamic, pstatic);
+}
+
+ATF_TC(onlydef_static_dynamic_eager);
+ATF_TC_HEAD(onlydef_static_dynamic_eager, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " with definition-only library,"
+	    " static load and use, then dynamic load and use");
+}
+ATF_TC_BODY(onlydef_static_dynamic_eager, tc)
+{
+	void *use_static, *use_dynamic;
+	int *(*fstatic)(void), *(*fdynamic)(void);
+	int *pstatic, *pdynamic;
+
+	ATF_REQUIRE_DL(dlopen("libh_onlydef.so", 0));
+	ATF_REQUIRE_DL(use_static = dlopen("libh_onlyuse_static.so", 0));
+	ATF_REQUIRE_DL(fstatic = dlsym(use_static, "fstatic"));
+	pstatic = (*fstatic)();
+	ATF_REQUIRE_DL(use_dynamic = dlopen("libh_onlyuse_dynamic.so", 0));
+	ATF_REQUIRE_DL(fdynamic = dlsym(use_dynamic, "fdynamic"));
+	pdynamic = (*fdynamic)();
+	ATF_CHECK_EQ_MSG(pstatic, pdynamic,
+	    "%p in static tls user != %p in dynamic tls user",
+	    pstatic, pdynamic);
+}
+
+ATF_TC(onlydef_static_dynamic_lazy);
+ATF_TC_HEAD(onlydef_static_dynamic_lazy, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
+	    " with definition-only library, static and dynamic load and use");
+}
+ATF_TC_BODY(onlydef_static_dynamic_lazy, tc)
+{
+	void *use_static, *use_dynamic;
+	int *(*fstatic)(void), *(*fdynamic)(void);
+	int *pstatic, *pdynamic;
+
+	ATF_REQUIRE_DL(dlopen("libh_onlydef.so", 0));
+	ATF_REQUIRE_DL(use_static = dlopen("libh_onlyuse_static.so", 0));
+	ATF_REQUIRE_DL(use_dynamic = dlopen("libh_onlyuse_dynamic.so", 0));
+	ATF_REQUIRE_DL(fstatic = dlsym(use_static, "fstatic"));
+	ATF_REQUIRE_DL(fdynamic = dlsym(use_dynamic, "fdynamic"));
+	pstatic = (*fstatic)();
+	pdynamic = (*fdynamic)();
+	ATF_CHECK_EQ_MSG(pstatic, pdynamic,
+	    "%p in static tls user != %p in dynamic tls user",
+	    pstatic, pdynamic);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, dynamic_abusedef);
+	ATF_TP_ADD_TC(tp, dynamic_abusedefnoload);
+	ATF_TP_ADD_TC(tp, dynamic_defabuse_eager);
+	ATF_TP_ADD_TC(tp, dynamic_defabuse_lazy);
+	ATF_TP_ADD_TC(tp, dynamic_defuse_eager);
+	ATF_TP_ADD_TC(tp, dynamic_defuse_lazy);
+	ATF_TP_ADD_TC(tp, dynamic_usedef);
+	ATF_TP_ADD_TC(tp, dynamic_usedefnoload);
+	ATF_TP_ADD_TC(tp, onlydef_dynamic_static_ctor);
+	ATF_TP_ADD_TC(tp, onlydef_dynamic_static_eager);
+	ATF_TP_ADD_TC(tp, onlydef_dynamic_static_lazy);
+	ATF_TP_ADD_TC(tp, onlydef_static_dynamic_eager);
+	ATF_TP_ADD_TC(tp, onlydef_static_dynamic_lazy);
+	ATF_TP_ADD_TC(tp, static_abusedef);
+	ATF_TP_ADD_TC(tp, static_abusedefnoload);
+	ATF_TP_ADD_TC(tp, static_defabuse_eager);
+	ATF_TP_ADD_TC(tp, static_defabuse_lazy);
+	ATF_TP_ADD_TC(tp, static_defuse_eager);
+	ATF_TP_ADD_TC(tp, static_defuse_lazy);
+	ATF_TP_ADD_TC(tp, static_usedef);
+	ATF_TP_ADD_TC(tp, static_usedefnoload);
+	return atf_no_error();
+}

Index: src/tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile	Tue Aug  1 16:34:58 2023
@@ -0,0 +1,10 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:58 martin Exp $
+
+LIB=		h_abuse_dynamic
+SRCS=		h_abuse_dynamic.c
+
+DEF_DIR_cmd=	cd ${.CURDIR}/../helper_def_dynamic && ${PRINTOBJDIR}
+DEF_DIR=	${DEF_DIR_cmd:sh}
+LDADD+=		-Wl,-rpath,${TESTSDIR} -L${DEF_DIR} -lh_def_dynamic
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_abuse_dynamic/h_abuse_dynamic.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_abuse_dynamic/h_abuse_dynamic.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_abuse_dynamic/h_abuse_dynamic.c	Tue Aug  1 16:34:58 2023
@@ -0,0 +1,37 @@
+/*	$NetBSD: h_abuse_dynamic.c,v 1.1.2.2 2023/08/01 16:34:58 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+extern __thread int mydynsym __attribute__((tls_model("initial-exec")));
+
+int *fuse(void);
+int *
+fuse(void)
+{
+
+	return &mydynsym;
+}

Index: src/tests/libexec/ld.elf_so/helper_abuse_static/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_abuse_static/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_abuse_static/Makefile	Tue Aug  1 16:34:58 2023
@@ -0,0 +1,10 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:58 martin Exp $
+
+LIB=		h_abuse_static
+SRCS=		h_abuse_static.c
+
+DEF_DIR_cmd=	cd ${.CURDIR}/../helper_def_static && ${PRINTOBJDIR}
+DEF_DIR=	${DEF_DIR_cmd:sh}
+LDADD+=		-Wl,-rpath,${TESTSDIR} -L${DEF_DIR} -lh_def_static
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_abuse_static/h_abuse_static.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_abuse_static/h_abuse_static.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_abuse_static/h_abuse_static.c	Tue Aug  1 16:34:58 2023
@@ -0,0 +1,37 @@
+/*	$NetBSD: h_abuse_static.c,v 1.1.2.2 2023/08/01 16:34:58 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+extern __thread int mysym;
+
+int *fuse(void);
+int *
+fuse(void)
+{
+
+	return &mysym;
+}

Index: src/tests/libexec/ld.elf_so/helper_def_dynamic/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_def_dynamic/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_def_dynamic/Makefile	Tue Aug  1 16:34:56 2023
@@ -0,0 +1,6 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:56 martin Exp $
+
+LIB=		h_def_dynamic
+SRCS=		h_def_dynamic.c
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_def_dynamic/h_def_dynamic.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_def_dynamic/h_def_dynamic.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_def_dynamic/h_def_dynamic.c	Tue Aug  1 16:34:56 2023
@@ -0,0 +1,37 @@
+/*	$NetBSD: h_def_dynamic.c,v 1.1.2.2 2023/08/01 16:34:56 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+__thread int mydynsym = 0;
+
+int *fdef(void);
+int *
+fdef(void)
+{
+
+	return &mydynsym;
+}

Index: src/tests/libexec/ld.elf_so/helper_def_static/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_def_static/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_def_static/Makefile	Tue Aug  1 16:34:56 2023
@@ -0,0 +1,6 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:56 martin Exp $
+
+LIB=		h_def_static
+SRCS=		h_def_static.c
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_def_static/h_def_static.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_def_static/h_def_static.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_def_static/h_def_static.c	Tue Aug  1 16:34:56 2023
@@ -0,0 +1,37 @@
+/*	$NetBSD: h_def_static.c,v 1.1.2.2 2023/08/01 16:34:56 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+__thread int mysym __attribute__((tls_model("initial-exec"))) = 0;
+
+int *fdef(void);
+int *
+fdef(void)
+{
+
+	return &mysym;
+}

Index: src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile	Tue Aug  1 16:34:59 2023
@@ -0,0 +1,10 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:59 martin Exp $
+
+LIB=		h_onlyctor_dynamic
+SRCS=		h_onlyctor_dynamic.c
+
+DEF_DIR_cmd=	cd ${.CURDIR}/../helper_onlydef && ${PRINTOBJDIR}
+DEF_DIR=	${DEF_DIR_cmd:sh}
+LDADD+=		-Wl,-rpath,${TESTSDIR} -L${DEF_DIR} -lh_onlydef
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic/h_onlyctor_dynamic.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic/h_onlyctor_dynamic.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlyctor_dynamic/h_onlyctor_dynamic.c	Tue Aug  1 16:34:59 2023
@@ -0,0 +1,36 @@
+/*	$NetBSD: h_onlyctor_dynamic.c,v 1.1.2.2 2023/08/01 16:34:59 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+extern __thread int mysym;	/* dynamic */
+int *mysymptr;
+
+static void __attribute__((constructor))
+init(void)
+{
+	mysymptr = &mysym;
+}

Index: src/tests/libexec/ld.elf_so/helper_onlydef/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlydef/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlydef/Makefile	Tue Aug  1 16:34:59 2023
@@ -0,0 +1,6 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:59 martin Exp $
+
+LIB=		h_onlydef
+SRCS=		h_onlydef.c
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_onlydef/h_onlydef.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlydef/h_onlydef.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlydef/h_onlydef.c	Tue Aug  1 16:34:59 2023
@@ -0,0 +1,29 @@
+/*	$NetBSD: h_onlydef.c,v 1.1.2.2 2023/08/01 16:34:59 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+__thread int mysym = 0;

Index: src/tests/libexec/ld.elf_so/helper_onlydef_static/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlydef_static/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlydef_static/Makefile	Tue Aug  1 16:34:57 2023
@@ -0,0 +1,6 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:57 martin Exp $
+
+LIB=		h_onlydef_static
+SRCS=		h_onlydef_static.c
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_onlydef_static/h_onlydef_static.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlydef_static/h_onlydef_static.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlydef_static/h_onlydef_static.c	Tue Aug  1 16:34:57 2023
@@ -0,0 +1,29 @@
+/*	$NetBSD: h_onlydef_static.c,v 1.1.2.2 2023/08/01 16:34:57 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+__thread int mysym __attribute__((tls_model("initial-exec"))) = 0;

Index: src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile	Tue Aug  1 16:35:00 2023
@@ -0,0 +1,10 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:35:00 martin Exp $
+
+LIB=		h_onlyuse_dynamic
+SRCS=		h_onlyuse_dynamic.c
+
+DEF_DIR_cmd=	cd ${.CURDIR}/../helper_onlydef && ${PRINTOBJDIR}
+DEF_DIR=	${DEF_DIR_cmd:sh}
+LDADD+=		-Wl,-rpath,${TESTSDIR} -L${DEF_DIR} -lh_onlydef
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic/h_onlyuse_dynamic.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic/h_onlyuse_dynamic.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlyuse_dynamic/h_onlyuse_dynamic.c	Tue Aug  1 16:35:00 2023
@@ -0,0 +1,37 @@
+/*	$NetBSD: h_onlyuse_dynamic.c,v 1.1.2.2 2023/08/01 16:35:00 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+extern __thread int mysym;	/* dynamic */
+
+int *fdynamic(void);
+int *
+fdynamic(void)
+{
+
+	return &mysym;
+}

Index: src/tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile	Tue Aug  1 16:34:56 2023
@@ -0,0 +1,10 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:56 martin Exp $
+
+LIB=		h_onlyuse_static
+SRCS=		h_onlyuse_static.c
+
+DEF_DIR_cmd=	cd ${.CURDIR}/../helper_onlydef && ${PRINTOBJDIR}
+DEF_DIR=	${DEF_DIR_cmd:sh}
+LDADD+=		-Wl,-rpath,${TESTSDIR} -L${DEF_DIR} -lh_onlydef
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_onlyuse_static/h_onlyuse_static.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_onlyuse_static/h_onlyuse_static.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_onlyuse_static/h_onlyuse_static.c	Tue Aug  1 16:34:56 2023
@@ -0,0 +1,37 @@
+/*	$NetBSD: h_onlyuse_static.c,v 1.1.2.2 2023/08/01 16:34:56 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+extern __thread int mysym __attribute__((tls_model("initial-exec")));
+
+int *fstatic(void);
+int *
+fstatic(void)
+{
+
+	return &mysym;
+}

Index: src/tests/libexec/ld.elf_so/helper_use_dynamic/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_use_dynamic/Makefile:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_use_dynamic/Makefile	Tue Aug  1 16:34:57 2023
@@ -0,0 +1,10 @@
+#	$NetBSD: Makefile,v 1.2.2.2 2023/08/01 16:34:57 martin Exp $
+
+LIB=		h_use_dynamic
+SRCS=		h_use_dynamic.c
+
+DEF_DIR_cmd=	cd ${.CURDIR}/../helper_def_dynamic && ${PRINTOBJDIR}
+DEF_DIR=	${DEF_DIR_cmd:sh}
+LDADD+=		-Wl,-rpath,${TESTSDIR} -L${DEF_DIR} -lh_def_dynamic
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_use_dynamic/h_use_dynamic.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_use_dynamic/h_use_dynamic.c:1.1.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_use_dynamic/h_use_dynamic.c	Tue Aug  1 16:34:57 2023
@@ -0,0 +1,37 @@
+/*	$NetBSD: h_use_dynamic.c,v 1.1.2.2 2023/08/01 16:34:57 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+extern __thread int mydynsym;
+
+int *fuse(void);
+int *
+fuse(void)
+{
+
+	return &mydynsym;
+}

Index: src/tests/libexec/ld.elf_so/helper_use_static/Makefile
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_use_static/Makefile:1.3.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_use_static/Makefile	Tue Aug  1 16:34:58 2023
@@ -0,0 +1,10 @@
+#	$NetBSD: Makefile,v 1.3.2.2 2023/08/01 16:34:58 martin Exp $
+
+LIB=		h_use_static
+SRCS=		h_use_static.c
+
+DEF_DIR_cmd=	cd ${.CURDIR}/../helper_def_static && ${PRINTOBJDIR}
+DEF_DIR=	${DEF_DIR_cmd:sh}
+LDADD+=		-Wl,-rpath,${TESTSDIR} -L${DEF_DIR} -lh_def_static
+
+.include "../helper.mk"

Index: src/tests/libexec/ld.elf_so/helper_use_static/h_use_static.c
diff -u /dev/null src/tests/libexec/ld.elf_so/helper_use_static/h_use_static.c:1.2.2.2
--- /dev/null	Tue Aug  1 16:35:01 2023
+++ src/tests/libexec/ld.elf_so/helper_use_static/h_use_static.c	Tue Aug  1 16:34:58 2023
@@ -0,0 +1,37 @@
+/*	$NetBSD: h_use_static.c,v 1.2.2.2 2023/08/01 16:34:58 martin Exp $	*/
+
+/*-
+ * Copyright (c) 2023 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+extern __thread int mysym __attribute__((tls_model("initial-exec")));
+
+int *fuse(void);
+int *
+fuse(void)
+{
+
+	return &mysym;
+}

Reply via email to