Commit Graph

4178 Commits

Author SHA1 Message Date
Nick Alcock
acf3236e6b libctf: a little string sharing test
It's actually quite hard to come up with simple tests that do *not* share
all their strings, but with enough ingenuity suitable cycles can be
concocted.

This test verifies that only and precisely those strings that are only used
in one child dict actually end up in its strtab.

ld/
	* testsuite/ld-ctf/unshared-strings*: New test.
2025-02-28 14:47:24 +00:00
Nick Alcock
4d2d5afa60 libctf: actually deduplicate the strtab
This commit finally implements strtab deduplication, putting together all
the pieces assembled in the earlier commits.

The magic is entirely localized to ctf_link_write, which preserializes all
the dicts (parent first), and calls ctf_dedup_strings on the parent.

(The error paths get tweaked a bit too.)

Calling ctf_dedup_strings has implications elsewhere: the lifetime rules for
the inputs versus outputs change a bit now that the child output dicts
contain references to the parent dict's atoms table.  We also pre-purge
movable refs from all the deduplicated strings before freeing any of this
because movable refs contain backreferences into the dict they came from,
which means the parent contains references to all the children!  Purging
the refs first makes those references go away so we can free the children
without creating any wild pointers, even temporarily.

There's a new testcase that identifies a regression whereby offset 0 (the
null string) and index 0 (in children now often the parent dict name,
".ctf") got mixed up, leading to anonymous structs and unions getting the
not entirely C-valid name ".ctf" instead.

May other testcases get adjusted to no longer depend on the precise layout
of the strtab.

TODO: add new tests to verify that strings are actually being deduplicated.

libctf/
	* ctf-link.c (ctf_link_write): Deduplicate strings.
	* ctf-open.c (ctf_dict_close): Free refs, then the link outputs,
        then the out cu_mapping, then the inputs, in that order.
        * ctf-string.c (ctf_str_purge_refs): Not static any more.
	* ctf-impl.h: Declare it.

ld/
	* testsuite/ld-ctf/conflicting-cycle-2.A-1.d: Don't depend on
        strtab contents.
	* testsuite/ld-ctf/conflicting-cycle-2.A-2.d: Likewise.
	* testsuite/ld-ctf/conflicting-cycle-2.parent.d: Likewise.
	* testsuite/ld-ctf/conflicting-cycle-3.C-1.d: Likewise.
	* testsuite/ld-ctf/conflicting-cycle-3.C-2.d: Likewise.
	* testsuite/ld-ctf/anonymous-conflicts*: New test.
2025-02-28 14:47:24 +00:00
Nick Alcock
6c77689963 include, libctf: add cth_parent_strlen CTFv4 header field
The first format difference between v3 and v4 is a cth_parent_strlen header
field.  This field (obviously not present in BTF) is populated from the
string table length of the parent at serialization time (protection against
being serialized before the parent is will be added in a later commit in
this series), and will be used at open time to prohibit opening of dicts
with a different strlen (which would corrupt the child's string table
if it was shared with the parent).

For now, just add the field, populate it at serialization time when linking
(when not linking, no deduplication is done and the correct value remains
unchanged), and dump it.

include/
	* ctf.h (ctf_header) [cth_parent_strlen]: New.

libctf/
	* ctf-dump.c (ctf_dump_header_sizefield): New.
	(ctf_dump_header): Use to dump the cth_parent_strlen.
	* ctf-open.c (upgrade_header_v2): Populate cth_parent_strlen.
	(upgrade_header_v3): Likewise.
	(ctf_flip_header): Flip it.
	(ctf_bufopen): Drop unnecessary initialization.
	* ctf-serialize.c (ctf_serialize): Write it out when linking.

ld/
	* testsuite/ld-ctf/data-func-conflicted-vars.d: Skip the nwe dump output.
	* testsuite/ld-ctf/data-func-conflicted.d: Likewise.
2025-02-28 14:47:24 +00:00
Nick Alcock
9a74ab12c8 include, libctf: start work on libctf v4
This format is a superset of BTF, but for now we just do the minimum to
declare a new file format version, without actually introducing any format
changes.

From now on, we refuse to reserialize CTFv1 dicts: these have a distinct
parent/child boundary which obviously cannot change upon reserialization
(that would change the type IDs): instead, we encoded this by stuffing in
a unique CTF version for such dicts.  We can't do that now we have one
version for all CTFv4 dicts, and testing such old dicts is very hard these
days anyway, and is not automated: so just drop support for writing them out
entirely. (You still *can* write them out, but you have to do a full-blown
ctf_link, which generates an all-new fresh dict and recomputes type IDs as
part of deduplication.)

To prevent this extremely-not-ready format escaping into the wild, add a
new mechanism whereby any format version higher than the new #define
CTF_STABLE_VERSION cannot be serialized unless I_KNOW_LIBCTF_IS_UNSTABLE is
set in the environment.

include/
	* ctf-api.h (_CTF_ERRORS) [ECTF_CTFVERS_NO_SERIALIZE]: New.
        [ECTF_UNSTABLE]: New.
         (ECTF_NERR): Update.
	* ctf.h: Small comment improvements..
        (ctf_header_v3): New, copy of ctf_header.
	(CTF_VERSION_4): New.
	(CTF_VERSION): Now CTF_VERSION_4.
	(CTF_STABLE_VERSION): Still 4, CTF_VERSION_3.

ld/
	* testsuite/ld-ctf/*.d: Update to CTF_VERSION_4.

libctf/
	* ctf-impl.h (LCTF_NO_SERIALIZE): New.
	* ctf-dump.c (ctf_dump_header): Add CTF_VERSION_4.
	* ctf-open.c (ctf_dictops): Likewise.
        (upgrade_header): Rename to...
	(upgrade_header_v2): ... this.
	(upgrade_header_v3): New.
	(upgrade_types): Support upgrading from CTF_VERSION_3.
        Turn on LCTF_NO_SERIALIZE for CTFv1.
	(init_static_types_internal): Upgrade all types tables older than
	* CTF_VERSION_4.
	(ctf_bufopen): Support CTF_VERSION_4: error out if we forget to
	update this switch in future.  Add header upgrading from v3 and
	below.  Improve comments slightly.
	* ctf-serialize.c (ctf_serialize): Block serialization of unstable
	file formats, and of file formats for which LCTF_NO_SERIALIZE is
	turned on (v1).
2025-02-28 14:47:24 +00:00
H.J. Lu
c44400b6ad x86-64: Pass -z separate-code to ld for -z mark-plt tests
Pass -z separate-code to ld for -z mark-plt tests to fix:

FAIL: ld-x86-64/mark-plt-1a
FAIL: ld-x86-64/mark-plt-1b
FAIL: ld-x86-64/mark-plt-1c
FAIL: ld-x86-64/mark-plt-1d
FAIL: ld-x86-64/mark-plt-1a-x32
FAIL: ld-x86-64/mark-plt-1b-x32
FAIL: ld-x86-64/mark-plt-1c-x32
FAIL: ld-x86-64/mark-plt-1d-x32

when binutils is configured with --disable-separate-code.

	* ld-x86-64/mark-plt-1a-x32.d: Pass -z separate-code to ld.
	* ld-x86-64/mark-plt-1a.d: Likewise.
	* ld-x86-64/mark-plt-1b-x32.d: Likewise.
	* ld-x86-64/mark-plt-1b.d: Likewise.
	* ld-x86-64/mark-plt-1c-x32.d: Likewise.
	* ld-x86-64/mark-plt-1c.d: Likewise.
	* ld-x86-64/mark-plt-1d-x32.d: Likewise.
	* ld-x86-64/mark-plt-1d.d: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-02-27 08:19:29 +08:00
Clément Chigot
5e0032993a ld/testsuite: add -z separate-code to sframe x86_64 tests
Those tests were generated by a linker having "-z separate-code" on by
default. However, being controlled by a configure option, it can be off
by default. Forcing the option as part of the tests ensures clean
results in both cases.
2025-02-26 09:02:18 +01:00
Jan Beulich
5649b9dc86 ix86: restrict use of GOT32X relocs
The ELF linker rejects use of this reloc type without a base register
for PIC code. Suppress its use by gas in such cases.

To keep things building for non-ELF, include the entire containing if()
in an #ifdef: All consumers of ->fx_tcbit* live in such conditionals as
well, hence there's no reason to keep the producer active.
2025-02-21 10:26:59 +01:00
Jan Beulich
010a8fd409 x86-64: further tighten convert-load-reloc checking
REX2.M affects what insn we're actually dealing with, so we better check
this to avoid transforming (future) insns we must not touch.
2025-02-21 10:25:41 +01:00
Jan Beulich
11c2852449 x86: widen @got{,pcrel} support to PUSH and APX IMUL
With us doing the transformation to an immediate operand for MOV and
various ALU insns, there's little reason to then not support the same
conversion for the other two insns which have respective immediate
operand forms. Unfortunately for IMUL (due to the 0F opcode prefix)
there's no suitable relocation, so the pre-APX forms cannot be marked
for relaxation in the assembler.
2025-02-21 10:24:50 +01:00
Jan Beulich
922fe9449a x86/APX: use CS: in place of ES: in @gotpcrel and @gottpoff relaxation
H.J. requested this adjustment; I'm unaware of any specific technical
background.
2025-02-21 10:23:21 +01:00
Jan Beulich
e6dc092038 ix86: tighten convert-load-reloc checking
Just like was done recently for x86-64 (commit 4998f9ea9d): Even if
the assembler avoids using the relaxable relocation for inapplicable
insns, the relocation type can still appear for other reasons. Be more
thorough in the opcode checking we do, to avoid bogusly altering other
insns.

Furthermore correct an opcode mask (even if with the added condition
that's now fully benign).
2025-02-21 10:22:50 +01:00
Alan Modra
6592258718 PR32715, ld-elf/pr29072 fail with --disable-default-execstack
--disable-default-stack is an alias for --enable-default-execstack=no.
The existing check only looked for the latter config option.

	PR 32715
	* testsuite/ld-elf/elf.exp (target_defaults_to_execstack): Look
	in config.h for result of --enable-default-execstack.
2025-02-19 08:40:38 +10:30
H.J. Lu
1256b9860f ld: Add tests for PR ld/32690
Without

commit 230a788eb2
Author: Alan Modra <amodra@gmail.com>
Date:   Tue Feb 18 08:54:06 2025 +1030

    PR32690, assertion failure in lang_size_relro_segment

this test triggers the linker error:

.../ld: internal error .../ld/ldlang.c 6618
collect2: error: ld returned 1 exit status

with GCC 10 or above on x86-64.

	PR ld/32690
	* testsuite/ld-elf/elf.exp: Run PR ld/32690 tests.
	* testsuite/ld-elf/pr32690.h: New file.
	* testsuite/ld-elf/pr32690a.c: Likewise.
	* testsuite/ld-elf/pr32690b.c: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-02-18 12:31:30 +08:00
H.J. Lu
d4d66eb19f x86: Return error for invalid relocation offset
Return error if relocation offset + relocation size > section size.

bfd/

	PR ld/32665
	* elf32-i386.c (elf_i386_scan_relocs): Return error for invalid
	relocation offset.
	* elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.

ld/

	PR ld/32665
	* testsuite/ld-x86-64/pr32665.err: New file.
	* testsuite/ld-x86-64/pr32665.o.bz2: Likewise.
	* testsuite/ld-x86-64/x86-64.exp: Run PR ld/32665 test.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-02-10 06:35:34 +08:00
Alexandre Oliva
fd82d5ddf5 sparc: define _GLOBAL_OFFSET_TABLE_ when referenced
GCC testsuite gcc.dg/20050321-2.c hit link errors on undefined
_GLOBAL_OFFSET_TABLE_.  The compiler output referenced only
_GLOBAL_OFFSET_TABLE_-offsets to set it up, and to compute the
GOT-relative address of local symbols, none of which triggered the
machinery that enabled the creation of the dynamic section, so
_GLOBAL_OFFSET_TABLE_ ended up undefined.

Enable the dynamic section if we find a relocation involving
_GLOBAL_OFFSET_TABLE_.  While at that, optimize checks for references
to it.


for  bfd/ChangeLog

	* elfxx-sparc.c (_bfd_sparc_elf_check_relocs): Check for
	_GLOBAL_OFFSET_TABLE_ references early, then compare hashed
	symbols instead of strings.
	(_bfd_sparc_elf_relocate_section): Compare hashed symbols.

for  ld/ChangeLog

	* testsuite/ld-sparc/got-def.s: New test.
	* testsuite/ld-sparc/sparc.exp: Add it.
2025-02-08 03:12:24 -03:00
H.J. Lu
73ab3b9825 x86-64: Estimate output section layout before sizing dynamic sections
When sizing dynamic sections, elf_x86_64_scan_relocs converts GOTPCREL
relocations to R_X86_64_PC32, R_X86_64_32S or R_X86_64_32 for local
symbols.  But at that time, since the output section layout is unknown,
the local symbol values can't be determined.  Later linker issues an
error if the converted relocation overflows when resolving relocations
against these local symbols.  Update the x86-64 ELF linker to estimate
output section layout before sizing dynamic sections and use the
preliminary output section layout info to skip the GOTPCREL relocation
conversion if the converted relocation overflows.

bfd/

	PR ld/32591
	* elf64-x86-64.c (elf_x86_64_convert_load_reloc): Add an input
	section argument.  Use the lowest-addressed section to estimate
	the __ehdr_start symbol value.  Don't convert relocation if the
	converted relocation will overflow.

ld/

	PR ld/32591
	* emultempl/elf-x86.em (elf_x86_64_before_allocation):
	New.  Defined for x86-64.
	(LDEMUL_BEFORE_ALLOCATION): Likewise.
	* testsuite/ld-x86-64/pr19609-2a.d: Don't fail.
	* testsuite/ld-x86-64/pr19609-2b.d: Likewise.
	* testsuite/ld-x86-64/pr19609-4a.d: Likewise.
	* testsuite/ld-x86-64/pr19609-5d.d: Likewise.
	* testsuite/ld-x86-64/pr19609-7a.d: Likewise.
	* testsuite/ld-x86-64/pr19609-7c.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1.s: New file.
	* testsuite/ld-x86-64/pr32591-1a-x32.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1a.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1a.t: Likewise.
	* testsuite/ld-x86-64/pr32591-1b-x32.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1b.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1b.t: Likewise.
	* testsuite/ld-x86-64/pr32591-1c-x32.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1c.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1c.t: Likewise.
	* testsuite/ld-x86-64/pr32591-1d-x32.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1d.d: Likewise.
	* testsuite/ld-x86-64/pr32591-1d.t: Likewise.
	* testsuite/ld-x86-64/pr32591-2.s: Likewise.
	* testsuite/ld-x86-64/pr32591-2-x32.d: Likewise.
	* testsuite/ld-x86-64/pr32591-2.d: Likewise.
	* testsuite/ld-x86-64/pr32591-2.t: Likewise.
	* testsuite/ld-x86-64/pr32591-3.s: Likewise.
	* testsuite/ld-x86-64/pr32591-3-x32.d: Likewise.
	* testsuite/ld-x86-64/pr32591-3.d: Likewise.
	* testsuite/ld-x86-64/pr32591-3.t: Likewise.
	* testsuite/ld-x86-64/pr32591-4.s: Likewise.
	* testsuite/ld-x86-64/pr32591-4-x32.d: Likewise.
	* testsuite/ld-x86-64/pr32591-4.d: Likewise.
	* testsuite/ld-x86-64/x86-64.exp: Run PR ld/32591 tests.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-02-07 06:02:05 +08:00
Lulu Cai
a73d904700 LoongArch: Do not relax against __[start|stop]_SECNAME symbol 2025-01-31 10:38:14 +00:00
Jan Beulich
43a7719af5 x86/APX: correct libbfd's EVEX Rn -> Bn transformations
In the recent GOTPCREL addition I screwed up, in clearing the Rn bits
afterwards rather than setting them. While that ought to be benign (for
the bits being ignored in situations like this), we still want to leave
"canonical" encodings.

The pre-existing GOTTPOFF conversion wasn't doing quite correctly
either: We cannot assume the incoming Bn bits to be in a particular
state, as for the addressing form in question they're ignored as well.

To address both, introduce a helper function. This is then also an
overall reduction of (source) code size (and use of "magic" numbers).
2025-01-31 10:07:54 +01:00
H.J. Lu
625cadfb85 x86-64: Remove pr19609-4c.d and pr19609-4d.d
Remove pr19609-4c.d and pr19609-4d.d since they are identical to
pr19609-4a.d and pr19609-4b.d, respectively.

	* testsuite/ld-x86-64/pr19609-4c.d: Removed.
	* testsuite/ld-x86-64/pr19609-4d.d: Likewise.
	* testsuite/ld-x86-64/pr19609-4e.d: Renamed to ...
	* testsuite/ld-x86-64/pr19609-4c.d: This.
	* testsuite/ld-x86-64/x86-64.exp: Updated.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-01-30 12:17:57 +08:00
Jens Remus
98d9fe6cb6 s390: Generate .eh_frame unwind information for .plt section
Enable unwinding using .eh_frame information through PLT entries.  Based
on x86-64.

This enhances stack traces if the instruction pointer is in a PLT entry.
For instance perf call graphs, when using --call-graph=dwarf, and Glibc
backtraces, when using backtrace() e.g. from a signal handler.
Note that GDB could already unwind through PLT entries using its s390-
specific prologue unwinder.

Furthermore this lays the foundation to generate SFrame information for
the PLT section in the future.

bfd/
	* elf64-s390.c: Include dwarf2.h.
	(PLT_CIE_SIZE, PLT_FDE_SIZE,
	PLT_FDE_START_OFFSET, PLT_FDE_LEN_OFFSET,
	elf_s390x_eh_frame_plt): New .eh_frame template for .plt
	section.
	(elf_s390_link_hash_table): Add plt_eh_frame field.
	(elf_s390_create_dynamic_sections): New s390-specific wrapper
	around _bfd_elf_create_dynamic_sections.  Create .eh_frame
	section for .plt section.
	(elf_backend_create_dynamic_sections): Register s390-specific
	elf_s390_create_dynamic_sections.
	(elf_s390_late_size_sections): Fill in .eh_frame section for
	.plt section.  Write .plt section size into .eh_frame FDE
	covering .plt section.
	(elf_s390_finish_dynamic_sections): Write .plt section start
	into .eh_frame FDE covering .plt section.  Call
	_bfd_elf_write_section_eh_frame on on htab->plt_eh_frame
	section.

ld/
	* NEWS: Add news entry.
	* emulparams/elf64_s390.sh: Include plt_unwind.sh.

ld/testsuite/
	* ld-s390/plt_64-1_eh.wf: New PLT .eh_frame generation test.
	* ld-s390/s390.exp: Link some existing test cases with
	--no-ld-generated-unwind-info so that they do not fail.  Run
	new PLT .eh_frame generation test.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-01-27 16:47:10 +01:00
Jens Remus
6dbc8a3ef4 s390: Add basic PLT generation tests
ld/testsuite/
	* ld-s390/plt_31_non-pic-1.pd: New non-PIC/PIE PLT generation
	test for 31-bit.
	* ld-s390/plt_31_pic-1.pd: New PIC/PIE PLT generation test for
	31-bit.
	* ld-s390/plt_31-1.wf: New PLT generation test for 31-bit.
	* ld-s390/plt_64-1.pd: New PLT generation test for 64-bit.
	* ld-s390/plt_64-1.wf: Likewise.
	* ld-s390/plt-1.s: New PLT generation test for 31/64-bit.
	* ld-s390/pltlib.s: Likewise.
	* ld-s390/s390.exp: Run new PLT generation tests.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-01-27 16:47:10 +01:00
H.J. Lu
5be27d2ebc ld-x86-64/pr19609-2d.d: Move "#pass" to the end
* testsuite/ld-x86-64/pr19609-2d.d:  Move "#pass" to the end.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-01-26 18:23:21 +08:00
Jan Beulich
4998f9ea9d x86-64: tighten convert-load-reloc checking
Even if the assembler avoids using relaxable relocations for
inapplicable insns, such relocations can still appear for other reasons.
Be more thorough in the opcode checking we do, to avoid bogusly altering
other insns.

Furthermore correct an opcode mask (even if with the added condition
that's now fully benign).
2025-01-24 10:26:46 +01:00
Jan Beulich
6a7a2387ad x86/APX: widen @gotpcrel and @gottpoff support (incl to MOVRS)
If legacy-encoded arithmetic insns are eligible for @gotpcrel
relaxation, EVEX-encoded ones ought to be, too.

Further anything that MOV-from-memory can be used for (and transformed
from) should then also extend to MOVRS.

While extending the apx-load* testcases add  -mrelax-relocations=yes to
the two ones which were missing this: Without this option the intended
testing would not occur on configurations defaulting the option to off.
2025-01-24 10:26:02 +01:00
Torbjörn SVENSSON
014a7c0fa3 ld: fix alignment issue for ARM thumb long branch stub using PureCode section
When pure-code option is activated. The linker creates for M-profile architecures
a 2-bytes branch instruction. This causes the section alignment to be set to 2-byte
alignment instead of 4-byte alignment. This is a problem for long branch stub
without pure-code section as it contains a 32-bit address as data, which is expected
to be 4-byte aligned. Hence creating a long branch stub for PureCode section followed
by a long branch stub will result in a misalignment for the 32-bit address.

An easy fix is to add a nop instruction after the branch to keep the section alignment
to 4 bytes.

Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
Co-authored-by: Guillaume VACHERIAS <guillaume.vacherias@st.com>
2025-01-23 10:36:03 +01:00
Alan Modra
63488ec7b8 More ld testsuite fixes
* testsuite/ld-elf/indirect.exp: Run compiler capability checks
	using run_host_noleak.
	* testsuite/ld-ifunc/ifunc.exp: Don't exit without restoring
	ASFLAGS.  Don't run ifuncmod5 twice.
2025-01-23 11:40:40 +10:30
Jan Beulich
196fda592e ld: replace another @progbits etc in an ELF testcase
The canonical form (in the testsuite) is %progbits and alike.
2025-01-22 09:51:00 +01:00
Alan Modra
bea261b937 asan ld builds without detect_leaks=0
I found that building binutils with -fsanitize=address,undefined
results in much of the testsuite not being run.  The problem is that
running gcc results in linker plugin memory leaks which of course are
errors, so the testsuite sees this as lack of compiler support.

	* testsuite/lib/ld-lib.exp (run_host_noleak): New proc.
	(check_compiler_available, check_lto_available),
	(check_lto_fat_available, check_lto_shared_available),
	(check_ifunc_available, check_ifunc_attribute_available),
	(check_libdl_available, check_gnu2_tls_available),
	(compile_one_cc): Use run_host_noleak.
	* testsuite/config/default.exp (compiler_supports): Likewise.
2025-01-21 08:42:44 +10:30
Lulu Cai
48984d3da7 LoongArch: Allocate GOT entry for TLS DESC when -mno-relax is enabled
The type transition of TLSDESC is only done when -mrelax is enabled.
So when -mno-relax is enabled, keep GOT_TLS_GDESC to allocate the
GOT entry instead of just keeping GOT_TLS_IE.
2025-01-17 18:35:56 +08:00
H.J. Lu
d7a3c9e650 ld: Load the object only section when opening the mixed object file
Load the object only section when opening the mixed object file, instead
of loading it after all other input files have been loaded. This fixed

.../ld/collect-ld: /tmp/ccZAoUIW.obj-only.o: in function `main':
.../ld/testsuite/ld-plugin/lto-10a.c:4: multiple definition of `main'; /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):(.text.startup+0x0): first defined here
.../ld/collect-ld: /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):(.text.startup+0xc5): undefined reference to `WinMain'
collect2: error: ld returned 1 exit status
...
FAIL: LTO 10

for x86_64-w64-mingw32 so that mixing LTO and non-LTO relocatable files
for "ld -r" works for both ELF and non-ELF platforms.

	* ld.texi: Remove "On ELF platforms" from documentation of mixing
	LTO and non-LTO relocatable files for "ld -r".
	* ldlang.c (cmdline_load_object_only_section): New.
	(cmdline_check_object_only_section): Call it.
	* testsuite/ld-plugin/lto.exp: Enable mixed LTO and non-LTO
	relocatable output tests for all.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-01-17 14:25:28 +08:00
Matthieu Longo
1c136b8ee9 aarch64: GCS tests for linking issues with dynamic objects 2025-01-15 17:15:27 +00:00
Alan Modra
f40e5f0224 Re: elf: Add GNU_PROPERTY_MEMORY_SEAL gnu property
Don't run tests on targets without required support.  Supply an
explicit -z nomemory-seal rather then relying on the harness default,
to lessen confusion for people looking at the test.  Don't use numeric
labels for the sake of hppa64*-hpux, and run the tests there.  Remove
incorrect comment about source editing.  Also, xfail rather than
notarget failing tests with a list of target triples so we check that
the list is correct.
2025-01-15 15:48:50 +10:30
Alan Modra
e0e6aa08d7 Re: ld: Add --enable-memory-seal configure option
Commit 80dc29527f accidentally removed an assignment to board_flags,
resulting in tcl errors 'can't read "board_flags": no such variable'
on sh4-linux-gnu.  Fix that by calling [get_board_flags] in the
condition rather than reinstating the removed line since it seems most
configurations don't have a null STATIC_LDFLAGS.  Do the same in
another similar test too.
2025-01-15 13:45:35 +10:30
Adhemerval Zanella
80dc29527f ld: Add --enable-memory-seal configure option
Add --enable-memory-seal linker configure option to enable memory
sealing (GNU_PROPERTY_MEMORY_SEAL) by default.

Change-Id: I4ce4ff33657f0f09b1ceb06210b6fcaa501f1799
2025-01-14 13:18:52 -03:00
Adhemerval Zanella
4d890484df elf: Add GNU_PROPERTY_MEMORY_SEAL gnu property
The GNU_PROPERTY_MEMORY_SEAL gnu property is a way to mark binaries
to be memory sealed by the loader, to avoid further changes of
PT_LOAD segments (such as unmapping or change permission flags).
This is done along with Linux kernel (the mseal syscall [1]), and
C runtime supports to instruct the kernel on the correct time during
program startup (for instance, after RELRO handling).  This support
is added along the glibc support to handle the new gnu property [2].

This is a opt-in security features, like other security hardening
ones like NX-stack or RELRO.

The new property is ignored if present on ET_REL objects, and only
added on ET_EXEC/ET_DYN if the linker option is used.  A gnu property
is used instead of DT_FLAGS_1 flag to allow memory sealing to work
with ET_EXEC without PT_DYNAMIC support (at least on glibc some ports
still do no support static-pie).

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8be7258aad44b5e25977a98db136f677fa6f4370
[2] https://sourceware.org/pipermail/libc-alpha/2024-September/160291.html

Change-Id: Id47fadabecd24be0e83cff45653f7ce9a900ecf4
2025-01-14 13:18:43 -03:00
H.J. Lu
40db53edab ld: Update mixed LTO and non-LTO relocatable output tests
Since mixed LTO and non-LTO relocatable output is only supported on ELF
platforms, limit these tests to ELF targets.  Since powerpc64 elfv1
defines a function symbol on its procedure descriptor, which is in a
data section, rather than on the code for that function, allow both D
and T for nm test on mixed object.

	* testsuite/ld-plugin/lto.exp: Limits  mixed LTO and non-LTO
	relocatable output tests to ELF targets.  Allow both D and T for
	nm test on mixed object.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-01-14 21:09:36 +08:00
H.J. Lu
9b854f169d ld: Add LTO and none-LTO output support for ld -r
Link with mixed IR/non-IR objects

* 2 kinds of object files
  o non-IR object file has
    * non-IR sections
  o IR object file has
    * IR sections
    * non-IR sections
    * The output of "ld -r" with mixed IR/non-IR objects should work with:
        o Compilers/linkers with IR support.
	o Compilers/linkers without IR support.
* Add the mixed object file which has
  o IR sections
  o non-IR sections:
    * Object codes from IR sections.
    * Object codes from non-IR object files.
  o Object-only section:
    * With section name ".gnu_object_only" and SHT_GNU_OBJECT_ONLY type
    on ELF:
    https://gitlab.com/x86-psABIs/Linux-ABI
    #define SHT_GNU_OBJECT_ONLY 0x6ffffff8	/* Object only */
    * Contain non-IR object file.
    * Input is discarded after link.
* Linker action:
  o Classify each input object file:
    * If there is a ".gnu_object_only" section, it is a mixed object file.
    * If there is a IR section, it is an IR object file.
    * Otherwise, it is a non-IR object file.
  o Relocatable non-IR link:
    * Prepare for an object-only output.
    * Prepare for a regular output.
    * For each mixed object file:
      * Add IR and non-IR sections to the regular output.
      * For object-only section:
	* Extract object only file.
	* Add it to the object-only output.
	* Discard object-only section.
    * For each IR object file:
      * Add IR and non-IR sections to the regular output.
    * For each non-IR object file:
      * Add non-IR sections to the regular output.
      * Add non-IR sections to the object-only output.
    * Final output:
      * If there are IR objects, non-IR objects and the object-only
      output isn't empty:
	* Put the object-only output into the object-only section.
	* Add the object-only section to the regular output.
	* Remove the object-only output.
  o Normal link and relocatable IR link:
    * Prepare for output.
    * IR link:
      * For each mixed object file:
	* Compile and add IR sections to the output.
	* Discard non-IR sections.
	* Object-only section:
	  * Extract object only file.
	  * Add it to the output.
	  * Discard object-only section.
      * For each IR object file:
        * Compile and add IR sections to the output.
	* Discard non-IR sections.
      * For each non-IR object file:
	* Add non-IR sections to the output.
    * Non-IR link:
      * For each mixed object file:
	* Add non-IR sections to the output.
	* Discard IR sections and object-only section.
      * For each IR object file:
	* Add non-IR sections to the output.
	* Discard IR sections.
      * For each non-IR object file:
	* Add non-IR sections to the output.

This is useful for Linux kernel build with LTO.

bfd/

	PR ld/12291
	PR ld/12430
	PR ld/13298
	* bfd.c (bfd_lto_object_type): Add lto_mixed_object.
	(bfd): Add object_only_section.
	(bfd_group_signature): New.
	* elf.c (special_sections_g): Add .gnu_object_only.
	* format.c: Include "plugin-api.h" and "plugin.h" if
	BFD_SUPPORTS_PLUGINS is defined.
	(bfd_set_lto_type): Set type to lto_mixed_object for
	GNU_OBJECT_ONLY_SECTION_NAME section.
	(bfd_check_format_matches): Don't check the plugin target twice
	if the plugin target is explicitly specified.
	* opncls.c (bfd_extract_object_only_section): New.
	* plugin.c (bfd_plugin_fake_text_section): New.
	(bfd_plugin_fake_data_section): Likewise.
	(bfd_plugin_fake_bss_section): Likewise.
	(bfd_plugin_fake_common_section): Likewise.
	(bfd_plugin_get_symbols_in_object_only): Likewise.
	* plugin.c (add_symbols): Call
	bfd_plugin_get_symbols_in_object_only and count
	plugin_data->object_only_nsyms.
	(bfd_plugin_get_symtab_upper_bound): Count
	plugin_data->object_only_nsyms.
	bfd_plugin_get_symbols_in_object_only and add symbols from
	object only section.
	(bfd_plugin_canonicalize_symtab): Remove fake_section,
	fake_data_section, fake_bss_section and fake_common_section.
	Set udata.p to NULL.  Use bfd_plugin_fake_text_section,
	bfd_plugin_fake_data_section, bfd_plugin_fake_bss_section and
	bfd_plugin_fake_common_section.
	Set udata.p to NULL.
	* plugin.h (plugin_data_struct): Add object_only_nsyms and
	object_only_syms.
	* section.c (GNU_OBJECT_ONLY_SECTION_NAME): New.
	* bfd-in2.h: Regenerated.

binutils/

	PR ld/12291
	PR ld/12430
	PR ld/13298
	* objcopy.c (group_signature): Removed.
	(is_strip_section): Replace group_signature with
	bfd_group_signature.
	(setup_section): Likewise.
	* readelf.c (get_os_specific_section_type_name): Handle
	SHT_GNU_OBJECT_ONLY.

gas/

	PR ld/12291
	PR ld/12430
	PR ld/13298
	* testsuite/gas/elf/section9.s: Add the .gnu_object_only test.
	* testsuite/gas/elf/section9.d: Updated.

include/

	PR ld/12291
	PR ld/12430
	PR ld/13298
	* elf/common.h (SHT_GNU_OBJECT_ONLY): New.

ld/

	PR ld/12291
	PR ld/12430
	PR ld/13298
	* ld.h (ld_config_type): Add emit_gnu_object_only and
	emitting_gnu_object_only.
	* ldelf.c (orphan_init_done): Make it file scope.
	(ldelf_place_orphan): Rename hold to orig_hold.  Initialize hold
	from orig_hold at run-time.
	(ldelf_finish): New.
	* ldelf.h (ldelf_finish): New.
	* ldexp.c (ldexp_init): Take a bfd_boolean argument to supprt
	object-only output.
	(ldexp_finish): Likewise.
	* ldexp.h (ldexp_init): Take a bfd_boolean argument.
	(ldexp_finish): Likewise.
	* ldfile.c (ldfile_try_open_bfd): Call
	cmdline_check_object_only_section.
	* ldlang.c: Include "ldwrite.h" and elf-bfd.h.
	* ldlang.c (cmdline_object_only_file_list): New.
	(cmdline_object_only_archive_list): Likewise.
	(cmdline_temp_object_only_list): Likewise.
	(cmdline_lists_init): Likewise.
	(cmdline_list_new): Likewise.
	(cmdline_list_append): Likewise.
	(print_cmdline_list): Likewise.
	(cmdline_on_object_only_archive_list_p): Likewise.
	(cmdline_object_only_list_append): Likewise.
	(cmdline_get_object_only_input_files): Likewise.
	(cmdline_arg): Likewise.
	(setup_section): Likewise.
	(copy_section): Likewise.
	(cmdline_fopen_temp): Likewise.
	(cmdline_add_object_only_section): Likewise.
	(cmdline_emit_object_only_section): Likewise.
	(cmdline_extract_object_only_section): Likewise.
	(cmdline_check_object_only_section): Likewise.
	(cmdline_remove_object_only_files): Likewise.
	(lang_init): Take a bfd_boolean argument to supprt object-only
	output.  Call cmdline_lists_init.
	(load_symbols): Call cmdline_on_object_only_archive_list_p
	to check if an archive member should be loaded.
	(lang_process): Handle object-only link.
	* ldlang.h (lang_init): Take a bfd_boolean argument.
	(cmdline_enum_type): New.
	(cmdline_header_type): Likewise.
	(cmdline_file_type): Likewise.
	(cmdline_bfd_type): Likewise.
	(cmdline_union_type): Likewise.
	(cmdline_list_type): Likewise.
	(cmdline_emit_object_only_section): Likewise.
	(cmdline_check_object_only_section): Likewise.
	(cmdline_remove_object_only_files): Likewise.
	* ldmain.c (main): Call xatexit with
	cmdline_remove_object_only_files.  Pass FALSE to lang_init,
	ldexp_init and ldexp_finish.  Use ld_parse_linker_script.
	Set link_info.output_bfd to NULL after close.  Call
	cmdline_emit_object_only_section if needed.
	(add_archive_element): Call cmdline_check_object_only_section.
	(ld_parse_linker_script): New.
	* ldmain.h (ld_parse_linker_script): New.
	* plugin.c (plugin_maybe_claim): Call
	cmdline_check_object_only_section on claimed IR files.
	* scripttempl/elf.sc: Also discard .gnu_object_only sections.
	* scripttempl/elf64hppa.sc: Likewise.
	* scripttempl/elfxtensa.sc: Likewise.
	* scripttempl/mep.sc: Likewise.
	* scripttempl/pe.sc: Likewise.
	* scripttempl/pep.sc: Likewise.
	* emultempl/aarch64elf.em (gld${EMULATION_NAME}_finish): Replace
	finish_default with ldelf_finish.
	* emultempl/alphaelf.em (alpha_finish): Likewise.
	* emultempl/avrelf.em (avr_finish): Likewise.
	* emultempl/elf.em (ld_${EMULATION_NAME}_emulation): Likewise.
	* emultempl/ppc32elf.em (ppc_finish): Likewise.
	* emultempl/ppc64elf.em (gld${EMULATION_NAME}_finish): Likewise.
	* emultempl/spuelf.em (gld${EMULATION_NAME}_finish): Likewise.
	* testsuite/ld-plugin/lto-10.out: New file.
	* testsuite/ld-plugin/lto-10a.c: Likewise.
	* testsuite/ld-plugin/lto-10b.c: Likewise.
	* testsuite/ld-plugin/lto-10r.d: Likewise.
	* testsuite/ld-plugin/lto-4.out: Likewise.
	* testsuite/ld-plugin/lto-4a.c: Likewise.
	* testsuite/ld-plugin/lto-4b.c: Likewise.
	* testsuite/ld-plugin/lto-4c.c: Likewise.
	* testsuite/ld-plugin/lto-4r-a.d: Likewise.
	* testsuite/ld-plugin/lto-4r-b.d: Likewise.
	* testsuite/ld-plugin/lto-4r-c.d: Likewise.
	* testsuite/ld-plugin/lto-4r-d.d: Likewise.
	* testsuite/ld-plugin/lto.exp (lto_link_tests): Prepare for
	"LTO 4[acd]", "lto-4r-[abcd]" and "LTO 10" tests.
	(lto_run_tests): Add "LTO 4[acd]" and "LTO 10" tests.
	Build liblto-4.a.  Run "lto-4r-[abcd]" tests.
	Run lto-10r and create tmpdir/lto-10.o.
	Add test for nm on mixed LTO/non-LTO object.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-01-14 05:22:04 +08:00
H.J. Lu
fd7728c8a4 ld: Update function prototypes for compilers defaulting to -std=gnu23
Since GCC 15 defaults to -std=gnu23, update function prototypes in linker
tests for compilers defaulting to -std=gnu23.

	PR ld/32546
	* ld-shared/main.c (shlib_checkfunptr1): Update prototype for
	compilers defaulting to -std=gnu23.
	(shlib_checkfunptr2): Likewise.
	* ld-shared/sh1.c (shlib_checkfunptr1): Likewise.
	(shlib_checkfunptr2): Likewise.
	* ld-srec/sr1.c (fn1): Likewise.
	(fn2): Likewise.
	* ld-srec/sr2.c (fn1): Likewise.
	(fn2): Likewise.
	* ld-vsb/main.c (shlib_checkfunptr1): Likewise.
	(shlib_checkfunptr2): Likewise.
	* ld-vsb/sh1.c (hlib_checkfunptr1): Likewise.
	(shlib_checkfunptr2): Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-01-12 16:45:45 +08:00
Clément Chigot
15f2f44ad7 ld/testsuite: move board flags to ld_link
Both CFLAGS and LDFLAGS provided by dejagnu board configuration could be
required to perform a link.

Up to now, those flags were pulled with run_cc_link_tests and
run_ld_link_exec_tests and then passed to ld_link process as arguments.
This means that calling `ld_link` outside those functions must remember
to manually pass them.
2025-01-07 14:07:19 +01:00
Clément Chigot
14c460302b ld/testsuite/lto: replace manual links by ld_link helper
Some tests are calling run_host_cmd in order to retrieve the
errors/warnings messages generated.
ld_link is also making them available through exec_output global
variable but as the advantages of taking the board configuration into
account unlike run_host_cmd.
2025-01-07 14:07:19 +01:00
Clément Chigot
f79e481b57 ld/testsuite: centralize board_cflags and board_ldflags
Those flags are retrieving the CFLAGS or LDFLAGS defined by the dejagnu
board. The same pattern was repeated many times.
2025-01-07 14:07:19 +01:00
Alan Modra
e8e7cf2abe Update year range in copyright notice of binutils files 2025-01-01 18:29:57 +10:30
Jan Beulich
cc26d6caf1 ld/testsuite: replace aarch64 uses of load_lib
Using $srcdir/$subdir directly doesn't work, at least not with expect
5.45, dejagnu 1.6, and an out-of-tree build (I assume it's the latter
aspect which is crucial here). Make use of load_file instead.
2024-12-27 11:37:05 +01:00
Xi Ruoyao
e2cbacaec1 LoongArch: Reword message for unresolvable relocs
For PDE, "recompiling with -fPIE" just makes no sense.

For PIE, "recompiling with -fPIE" makes sense for unresolvable absolute
relocs, but not unresolveable PC-relative relocs: if the reloc is
already PC-relative, the problem is not the reloc is PC-relative or
absolute, but the reloc is not applicable for external symbols.

If we hit an unresolvable reloc in PDE or an unresolvable PC-relative
reloc in PIE, it means the programmer has somehow wrongly instructed the
compiler to treat external symbols as local symbols.  A misuse of
-mdirect-extern-access can cause the issue, so we can suggest
-mno-direct-extern-access.  And in all cases (DSO/PIE/PDE) a mismatching
symbol visibility can also cause the issue, so we should also suggest to
check the visibility.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
2024-12-27 17:52:29 +08:00
Xi Ruoyao
c1a964051b LoongArch: Allow R_LARCH_PCALA_HI20 or R_LARCH_PCREL20_S2 against undefined weak symbols for static PIE
In a static PIE, undefined weak symbols should be just resolved to
runtime address 0, like those symbols with non-default visibility.  This
was silently broken in all prior Binutils releases with "-static-pie
-mdirect-extern-access":

    $ cat t.c
    int x (void) __attribute__ ((weak));

    int
    main (void)
    {
      __builtin_printf("%p\n", x);
    }
    $ gcc t.c -static-pie -mdirect-extern-access
    $ ./a.out
    0x7ffff1d64000

Since commit 4cb77761d6 ("LoongArch: Check PC-relative relocations for
shared libraries), the situation has been improved: the linker errors
out instead of silently producing a wrong output file.

But logically, using -mdirect-extern-access for a static PIE perfectly
makes sense, and we should not prevent that even if the programmer uses
weak symbols.  Linux kernel is such an example, and Linux < 6.10 now
fails to build with Binutils trunk.  (The silent breakage with prior
Binutils releases was "benign" due to some blind luck.)

While since the 6.10 release Linux has removed those potentially
undefined weak symbols (due to performance issue), we still should
support weak symbols in -mdirect-extern-access -static-pie and unbreak
building old kernels.

Link: https://lore.kernel.org/loongarch/20241206085810.112341-1-chenhuacai@loongson.cn/
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
2024-12-27 17:52:29 +08:00
Xi Ruoyao
bd94e7cd84 LoongArch: Fix resolution of undefined weak hidden/protected symbols
An undefined weak hidden/protect symbol should be resolved to runtime
address 0, but we were actually resolving it to link-time address 0.  So
in PIE or DSO the runtime address would be incorrect.

Fix the issue by rewriting pcalau12i to lu12i.w, and pcaddi to addi.w.
The latter does not always work because the immediate field of addi.w is
narrower, report an error in the case the addend is too large.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
2024-12-27 17:52:29 +08:00
Alan Modra
bbc969306f PR 32324, Stripping BOLT'ed binaries leads to unwanted behaviour
This patch corrects layout for a PT_LOAD header that doesn't include
the ELF file header but does contain PHDRs and sections requiring
alignment.  The required alignment (which was missing) is placed
before the PHDRs.
2024-12-24 14:54:19 +10:30
Lulu Cai
1c82e1ee93 LoongArch: Fix bfd ld failed test case
This test case requires host gcc, and different distributions have
different default configurations for gcc, which can cause address value
mismatches.
Therefore, it is fixed by passing consistent options and using regular
expressions.
2024-12-16 15:45:45 +08:00
WANG Xuerui
c2dbc2929e LoongArch: Default to a maximum page size of 64KiB
As per the spec (Section 7.5.10, LoongArch Reference Manual Vol. 1),
LoongArch machines are not limited in page size choices, and currently
page sizes of 4KiB, 16KiB and 64KiB are supported by mainline Linux.
While 16KiB is the most common, the current BFD code says it is the
maximum; this is not correct, and as an effect, almost all existing
binaries are incompatible with a 64KiB kernel because the sections are
not sufficiently aligned, while being totally fine otherwise.
This is needlessly complicating integration testing [1].

This patch fixes the inconsistency, and also brings BFD behavior in line
with that of LLD [2].

[1] https://github.com/loongson-community/discussions/issues/47
[2] https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/lld/ELF/Arch/LoongArch.cpp#L174-L183

bfd/
	* elfnn-loongarch.c (ELF_MAXPAGESIZE): Bump to 64KiB.
	(ELF_MINPAGESIZE): Define as 4KiB.
	(ELF_COMMONPAGESIZE): Define as 16KiB.

ld/
	* testsuite/ld-loongarch-elf/64_pcrel.d: Update assertions after
	changing the target max page size to 64KiB.
	* testsuite/ld-loongarch-elf/data-got.d: Likewise.
	* testsuite/ld-loongarch-elf/desc-relex.d: Likewise.
	* testsuite/ld-loongarch-elf/relax-align-ignore-start.d: Likewise.
	* testsuite/ld-loongarch-elf/tlsdesc_abs.d: Make the fuzzy match work
	as intended by not checking exact instruction words.
	* testsuite/ld-loongarch-elf/tlsdesc_extreme.d: Likewise.

Signed-off-by: WANG Xuerui <git@xen0n.name>
2024-12-10 09:14:30 +08:00
Nelson Chu
6f8e75592c RISC-V: PR27566, consider ELF_MAXPAGESIZE/COMMONPAGESIZE for gp relaxations.
For default linker script, if a symbol's value outsides the bounds of the
defined section, then it may cross the data segment alignment, so we should
reserve more size about MAXPAGESIZE and COMMONPAGESIZE when doing gp
relaxations.  Otherwise we may meet the truncated errors since the data
segment alignment might move the section forward.

bfd/
	PR 27566
	* elfnn-riscv.c (_bfd_riscv_relax_lui): Consider MAXPAGESIZE and
	COMMONPAGESIZE if the symbol's value outsides the bounds of the
	defined section.
	(_bfd_riscv_relax_pc): Likewise.
ld/
	PR 27566
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated.
	* testsuite/ld-riscv-elf/relax-data-segment-align*: New testcase
	for pr27566.  Without this patch, the rv32 binutils will meet
	truncated errors for this testcase.
2024-12-06 11:36:53 +08:00