Commit Graph

7794 Commits

Author SHA1 Message Date
Alan Modra
86b26b453f Replace uses of asprintf with xasprintf
xasprintf has a nicer interface and behaves like xmalloc as far as
memory is concerned, ie. no need to check a return status and the
program exits with an error on OOM.

binutils/
	* dwarf.c (load_debug_sup_file): Replace asprintf with xasprintf.
	* nm.c (get_elf_symbol_type, get_coff_symbol_type): Likewise.
	* objdump.c (dump_ctf_indent_lines): Likewise.
	* readelf.c (display_lto_symtab, dump_ctf_indent_lines): Likewise.
	* windres.c (main): Likewise.
	* configure.ac: Remove asprintf from AC_CHECK_DECLS.
	* config.in: Regenerate.
	* configure: Regenerate.
gas/
	* config/tc-kvx.c (kvx_emit_single_noop): Simplify.
	* config/tc-riscv.c (md_assemblef): Replace asprintf with xasprintf.
	* read.c (s_nop, do_s_func): Likewise.
	* stabs.c (stabs_generate_asm_func): Likewise.
	(stabs_generate_asm_endfunc): Likewise.
	* configure.ac: Remove asprintf from AC_CHECK_DECLS.
	* config.in: Regenerate.
	* configure: Regenerate.
ld/
	* ldlang.c (lang_leave_overlay_section): Replace xmalloc+sprintf
	with xasprintf.  Localise vars.
	* lexsup.c (parse_args): Replace asprintf with xasprintf.
	* pe-dll.c (make_head, make_tail, make_one): Likewise.
	(make_singleton_name_thunk, make_import_fixup_entry): Likewise.
	(make_runtime_pseudo_reloc): Likewise.
	(pe_create_runtime_relocator_reference): Likewise.
	* configure.ac: Remove asprintf from AC_CHECK_DECLS.
	* config.in: Regenerate.
	* configure: Regenerate.
2024-10-24 17:58:00 +10:30
Alan Modra
32dc367249 Report bfd_merge_sections error
PR 32260
bfd/
	* elfxx-target.h (bfd_elfNN_bfd_merge_sections): Default to
	bfd_generic_merge_sections when using the generic linker.
	* elflink.c (_bfd_elf_merge_sections): Return error from
	_bfd_merge_sections.  Abort on wrong hash table.
ld/
	* ldlang.c (lang_process): Report bfd_merge_sections error.
2024-10-20 17:15:53 +10:30
Lulu Cai
4cb77761d6 LoongArch: Check PC-relative relocations for shared libraries
Building shared libraries should not be allowed for PC-relative
relocations against external symbols.
Currently LoongArch has no corresponding checks and silently
generates wrong shared libraries.

However, In the first version of the medium cmodel, pcalau12i+jirl was
used for function calls, in which case PC-relative relocations were
allowed.
2024-10-17 21:01:52 +08:00
Alan Modra
bc85bc665a Add noxfail option to run_dump_test
The noxfail option is useful in situations like pr23658-1e which
fails on all microblaze ELF targets except microblaze-linux.  This was
possible to handle by writing a small proc and use that as an xfail
predicate, or painstakingly listing all microblaze ELF targets, but
this is simpler.  The patch also fixes some other FAILs and XPASSes of
the pr23658 tests.

binutils/
	* testsuite/lib/binutils-common.exp (run_dump_test): Support
	noxfail.
ld/
	* testsuite/ld-elf/pr23658-1a.d: Don't xfail m68hc12.
	* testsuite/ld-elf/pr23658-1e.d: Likewise.  xfail xstormy16
	and correct microblaze xfails.
2024-10-16 14:54:00 +10:30
mengqinggang
a104f0a3e6 LoongArch: Add more relaxation support for call36
Add relaxation support for call36 that jump to PLT entry.

Add relaxation support for call36 with IFUNC symbol.

Add relaxation support for call36 that jump to undefweak symbol.
For undefweak symbol, it can always be relaxed if it have no PLT entry.
Because we set the address of undefweak symbol without PLT entry to PC
like relocate_section.
2024-10-15 14:26:17 +08:00
Jan Beulich
1f1b5e506b bfd/ELF: restrict file alignment for object files
While for executables properly aligning sections within the file can be
quite relevant, the same is of pretty little importance for relocatable
object files. Avoid passing "true" into
_bfd_elf_assign_file_position_for_section() when dealing with object
files, but compensate minimally by applying log_file_align in such
cases as a cap to the alignment put in place.
2024-10-11 08:19:34 +02:00
Alan Modra
124deb3101 Revised "Don't return (null) from bfd_elf_sym_name"
Commit 68bbe11833 results in a lot of follow up work, much of which
likely is still to be done. (And yes, since this is all for corrupted
or fuzzed object files, a whole lot of work doesn't much benefit
anyone.  It was a bad idea to put NULL in asymbol->name.)  So I'm
changing the approach to instead put a unique empty string for symbols
with a corrupted st_name.  An empty string won't require much work to
ensure nm, objcopy, objdump etc. won't crash, since these tools
already must work with unnamed local symbols.

The unique empty string is called bfd_symbol_error_name.  This patch
uses that name string for corrupted symbols in the ELF and COFF
backends.  Such symbols are displayed by nm and objdump as the
translated string "<corrupt>", which is what the COFF backend used to
put directly into corrupted symbols.

ie. it's the way I should have written the original patch, plus a few
tides and cleanups I retained from the reverted patches.
2024-10-08 15:12:19 +10:30
Alan Modra
cc516199d6 Revert "Don't return "(null)" from bfd_elf_sym_name"
This reverts commit 68bbe11833.
2024-10-08 15:12:14 +10:30
Alan Modra
68bbe11833 Don't return "(null)" from bfd_elf_sym_name
A NULL return from bfd_elf_string_from_elf_section indicates an error.
That shouldn't be masked by bfd_elf_sym_name but rather passed up to
callers such as group_signature.  If we want to print "(null)" then
that should be done at a higher level.  That's what this patch does,
except that I chose to print "<null>" instead, like readelf.  If we
see "(null)" we're probably passing a NULL to printf.  I haven't
changed aoutx.h or pdp11.c print_symbol functions because they already
handle NULL names by omitting the name.  I also haven't changed
mach-o.c, mmo.c, som.c, srec.c, tekhex.c, vms-alpha.c and
wasm-module.c print_symbol function because it looks like they will
never have NULL symbol names.

bfd/
	* elf.c (bfd_elf_sym_name): Don't turn a NULL name into a
	pointer to "(null)".
	(bfd_elf_print_symbol): Print "<null>" for NULL symbol names.
	* coffgen.c (coff_print_symbol): Likewise.
	* ecoff.c (_bfd_ecoff_print_symbol): Likewise.
	* pef.c (bfd_pef_print_symbol): Likewise.
	* syms.c (bfd_symbol_info): Return "<null>" in symbol_info.name
	if symbol name is NULL.
ld/
	* ldlang.c (ld_is_local_symbol): Don't check for "(null)"
	symbol name.
2024-10-03 12:31:45 +09:30
Nick Clifton
7ecf0250f7
Improve the placement of orphan note sections.
PR 32219
2024-09-30 14:41:11 +01:00
Alan Modra
f6abafcd91 Move uses_elf_em to ld-lib.exp
and add a missing entry from uses_genelf.

binutils/
	* testsuite/lib/binutils-common.exp (uses_elf_em): Delete.
ld/
	* testsuite/lib/ld-lib.exp (uses_genelf): Add moxie-*-moxiebox.
	(uses_elf_em): New.
2024-09-28 15:45:50 +09:30
Alan Modra
428f3561bc Enable -z separate-code, -z common and -z text for more targets
Fix a mis-placed "fi".
2024-09-27 10:02:14 +09:30
H.J. Lu
052940eba6 ld: Ignore .note.gnu.build-id when placing orphaned notes
The commits:

e8e10743f7 Add --rosegment option to BFD linker to stop the '-z separate-code' from generating two read-only segments.
bf6d7087de ld: Move the .note.build-id section to near the start of the memory map

place .note.gnu.build-id before text sections when --rosegment is used.
Ignore .note.gnu.build-id when placing orphaned notes if --rosegment and
-z separate-code are used together to avoid putting any note sections
between .note.gnu.build-id and text sections in the same PT_LOAD segment.

	PR ld/32191
	* ldlang.c (lang_insert_orphan): Ignore .note.gnu.build-id when
	placing orphaned notes.
	* testsuite/ld-elf/pr23658-1a.d: Pass --no-rosegment to ld.
	* testsuite/ld-elf/pr23658-1c.d: Likewise.
	* testsuite/ld-elf/pr23658-1e.d: New file.
	* testsuite/ld-elf/pr23658-1f.d: Likewise.
	* testsuite/ld-i386/i386.exp: Run PR ld/32191 test.
	* testsuite/ld-i386/pr32191.d: New file.
	* testsuite/ld-x86-64/lam-u48.rd: Updated.
	* testsuite/ld-x86-64/lam-u57.rd: Likewise.
	* testsuite/ld-x86-64/pr32191-x32.d: New file.
	* testsuite/ld-x86-64/pr32191.d: Likewise.
	* testsuite/ld-x86-64/pr32191.s: Likewise.
	* testsuite/ld-x86-64/x86-64.exp: Run PR ld/32191 tests.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-09-26 19:43:08 +08:00
Simon Marchi
89320f8658 ld: re-generate configure
Looks like configure has been generated with a non-upstream autoconf,
re-generate it.

ld/ChangeLog:

	* configure: Re-generate.

Change-Id: I6774381ad411a190fb93ff260234dd79d8791680
2024-09-24 12:32:31 -04:00
Frank Ch. Eigler
2299dfd4ba ld: support --build-id=xx mode
The is patch adds a new ld build-id computation mode, "xx", using
xxhash in its 128-bit mode.  The patch prereqs the xxhash-devel
headers being installed, and uses the "all-inlined" model, so no
run-time or link-time library dependence exists.

The xxhash mode performs well, saving roughly 20% of total userspace
run time from an ld job over a 800MB shared library relative to sha1.
128 bits of good hash should be collision-resistant to a number of
distinct binaries that numbers in the 2**32 - 2**64 range, even if not
"crypto" level hash.  Confirmations of this are in progress.

         ld/configury: add --with-xxhash mode, different from gdb case
                       because only using it in inline mode

         ld/ldbuildid.c: add "xx" mode, #if WITH_XXHASH

         ld/NEWS, ld.texi: mention new option

         ld/lexsup.c: add enumeration of --build-id STYLES to --help

         ld/testsuite/ld-elf/build-id.exp: add test case for 0xHEX case
                                           and conditional for xx case;
                                           also, simply tcl list syntax

https://inbox.sourceware.org/binutils/20240917201509.GB26396@redhat.com/

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
2024-09-24 09:59:42 -04:00
Mark Harmstone
909fcd9bc8 ld/pdb: Handle DEBUG_S_INLINEELINES data
The DEBUG_S_INLINEELINES block in the .debug$S section records the line
numbers in a source file covered by inlined functions. It's similar to
the DEBUG_S_LINES block, but as it references LF_FUNC_ID types we also
need to parse it to remap the type numbers.
2024-09-24 02:33:08 +01:00
Jan Beulich
4c73fafbbd ld/PE: enable secrel testcases also for 64-bit Cygwin
Plus the others that are grouped there.
2024-09-23 15:27:02 +02:00
Jan Beulich
72cd2c7097 ld/PE: no base relocs for section (relative) ones
Even more so than image relative (RVA) relocations, section relative
ones as well as section ones should not have base relocations created in
the final PE image. Reportedly section relative relocations will want
using for TLS support in the (Windows) compiler.

While there also correct the names for two of the "image base" relocs.
2024-09-23 15:26:29 +02:00
Nick Clifton
e02f484776
LD: Document use of SOURCE_DATE_EPOCH in Environment section 2024-09-23 14:22:57 +01:00
H.J. Lu
1b714c14e4 x86: Turn PLT32 to PC32 only for PC-relative relocations
commit 292676c15a
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Feb 13 13:44:17 2020 -0800

    x86: Resolve PLT32 reloc aganst local symbol to section

resolved PLT32 relocation against local symbol to section and

commit 2585b7a5ce
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sun Jul 19 06:51:19 2020 -0700

    x86: Change PLT32 reloc against section to PC32

turned PLT32 relocation against section into PC32 relocation.  But these
transformations are valid only for PC-relative relocations.  Add fx_pcrel
check for PC-relative relocations when performing these transformations
to keep PLT32 relocation in `movq $foo@PLT, %rax`.

gas/

	PR gas/32196
	* config/tc-i386.c (tc_i386_fix_adjustable): Return fixP->fx_pcrel
	for PLT32 relocations.
	(i386_validate_fix): Turn PLT32 relocation into PC32 relocation
	only if fixp->fx_pcrel is set.
	* testsuite/gas/i386/reloc32.d: Updated.
	* testsuite/gas/i386/reloc64.d: Likewise.
	* testsuite/gas/i386/reloc32.s: Add PR gas/32196 test.
	* testsuite/gas/i386/reloc64.s: Likewise.

ld/

	PR gas/32196
	* testsuite/ld-x86-64/plt3.s: New file.
	* testsuite/ld-x86-64/x86-64.exp: Run plt3.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-09-23 03:45:19 +08:00
Cui, Lili
d774bf9b36 x86: Add tls check in gas
Assembler shouldn't accept invalid TLS instructions, TLS relocations
can only be used with specific instructions as specified in TLS psABI
and linker issues an error when TLS relocations are used with wrong
instructions or format. Since it is inconvenient for gcc to rely on
linker to report errors, adding TLS check in the assembler stage so
that gcc can know TLS errors earlier.

gas/ChangeLog:

        PR gas/32022
        * config.in: Regenerate.
        * config/tc-i386.c
        *(enum x86_tls_error_type): New.
        *(struct _i386_insn): Added has_gotrel to indicate whether TLS
	relocations need to be checked.
        (x86_check_tls_relocation): Added a new function to check TLS
	relocation.
        (x86_report_tls_error): Created a new function to report TLS error.
        (i386_assemble): Handle x86_check_tls_relocation.
        (lex_got): Set i.has_gotrel.
        (OPTION_MTLS_CHECK): Added a new option to contrl TLS check.
        (struct option): Ditto.
        (md_parse_option): Ditto.
        (md_show_usage): Ditto.
        * configure.ac: Added a new option to check TLS relocation by
	default.
        * configure: Regenerated.
        * doc/c-i386.texi: Document -mtls-check=.
        * testsuite/gas/i386/i386.exp: Added new tests.
        * testsuite/gas/i386/ilp32/ilp32.exp: Ditto.
        * testsuite/gas/i386/ilp32/reloc64.d: Disable TLS check for it.
        * testsuite/gas/i386/ilp32/x32-tls.d: Ditto.
        * testsuite/gas/i386/inval-tls.l: Added more test cases.
        * testsuite/gas/i386/inval-tls.s: Ditto.
        * testsuite/gas/i386/reloc32.d: Disable TLS check for it.
        * testsuite/gas/i386/reloc64.d: Ditto.
        * testsuite/gas/i386/x86-64-inval-tls.l: Added more test cases.
        * testsuite/gas/i386/x86-64-inval-tls.s: Ditto.
        * testsuite/gas/i386/x86-64.exp: Added new tests.
        * testsuite/gas/i386/ilp32/x32-inval-tls.l: New test.
        * testsuite/gas/i386/ilp32/x32-inval-tls.s: Ditto.
        * testsuite/gas/i386/ilp32/x86-64-tls.d: Ditto.
        * testsuite/gas/i386/tls.d: Ditto.
        * testsuite/gas/i386/tls.s: Ditto.
        * testsuite/gas/i386/x86-64-tls.d: Ditto.
        * testsuite/gas/i386/x86-64-tls.s: Ditto.

ld/ChangeLog:

        PR gas/32022
        * testsuite/ld-i386/tlsgdesc1.d: Disable TLS check for it.
        * testsuite/ld-i386/tlsgdesc2.d: Ditto.
        * testsuite/ld-i386/tlsie2.d: Ditto.
        * testsuite/ld-i386/tlsie3.d: Ditto.
        * testsuite/ld-i386/tlsie4.d: Ditto.
        * testsuite/ld-i386/tlsie5.d: Ditto.
        * testsuite/ld-i386/tlsgdesc3.d: Ditto.
        * testsuite/ld-x86-64/tlsdesc3.d: Ditto.
        * testsuite/ld-x86-64/tlsdesc4.d: Ditto.
        * testsuite/ld-x86-64/tlsie2.d: Ditto.
        * testsuite/ld-x86-64/tlsie3.d: Ditto.
        * testsuite/ld-x86-64/tlsie5.d: Ditto.
        * testsuite/ld-x86-64/tlsdesc5.d: Ditto.
2024-09-21 05:19:16 +08:00
H.J. Lu
ae6a4c3f1d ld: Use --no-rosegment to ld for PR ld/22393 tests
The commit

bf6d7087de ld: Move the .note.build-id section to near the start of the memory map

moves the .note.build-id section before text sections.  When --rosegment
and -z separate-code are used together, the .note.gnu.property section
is placed between the .note.build-id section and text sections in the
same PT_LOAD segment by orphan placement.  Pass --no-rosegment to ld for
PR ld/22393 tests to avoid linker test failures.

	PR ld/32190
	* testsuite/ld-elf/pr22393-2a.rd: Pass --no-rosegment to ld.
	* testsuite/ld-elf/pr22393-2b.rd: Likewise.
	* testsuite/ld-elf/shared.exp: Pass --no-rosegment to ld when
	building pr22393-2 tests.
	* testsuite/ld-x86-64/pr22393-3a.rd: Pass --no-rosegment to ld.
	* testsuite/ld-x86-64/pr22393-3b.rd: Likewise.
	* testsuite/ld-x86-64/x86-64.exp: Pass --no-rosegment to ld when
	building pr22393-3 tests.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-09-21 05:03:18 +08:00
H.J. Lu
c588e37496 ld: Change -z one-rosegment to --rosegment in comments
There is no such linker command-line option, -z one-rosegment.  Replace
it with --rosegment in comments.

	* genscripts.sh: Change -z one-rosegment to --rosegment in
	comments.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-09-20 11:02:40 +08:00
H.J. Lu
075c4ca29a x86-64: Disable PIE on PR gas/32189 test
Disable PIE on PR gas/32189 test, which contains the non-PIE assembly
source, to support GCC defaulted to PIE.

	PR gas/32189
	* testsuite/ld-x86-64/x86-64.exp: Pass $NOPIE_LDFLAGS to linker
	on PR gas/32189 test.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-09-20 07:15:02 +08:00
H.J. Lu
8015b1b0c1 x86-64: Never make R_X86_64_GOT64 section relative
R_X86_64_GOT64 relocation should never be made section relative.  Change
tc_i386_fix_adjustable to return 0 for BFD_RELOC_X86_64_GOT64.

gas/

	PR gas/32189
	* config/tc-i386.c (tc_i386_fix_adjustable): Return 0 for
	BFD_RELOC_X86_64_GOT64.
	* testsuite/gas/i386/reloc64.d: Updated.
	* testsuite/gas/i386/reloc64.s: Add more tests for R_X86_64_GOT64
	and R_X86_64_GOTOFF64.

ld/

	PR gas/32189
	* testsuite/ld-x86-64/x86-64.exp: Run PR gas/32189 test.
	* testsuite/ld-x86-64/pr32189.s: New file.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-09-20 05:44:35 +08:00
Nick Clifton
bf6d7087de
ld: Move the .note.build-id section to near the start of the memory map.
This helps GDB to locate the debug information associated with a core dump.
Core dumps include the first page of an executable's image, and if this
page include the .note.build-id section then GDB can find it and then track
down a debug info file for that build-id.
2024-09-19 16:45:30 +01:00
Christophe Lyon
a77ccc8129 arm: Handle undefweak with ST_BRANCH_UNKNOWN
A previous patch made ld fail early on Thumb-only where branch_type is
ST_BRANCH_UNKNOWN.

However, this fails erroneously when the target is undefweak: in that
case the branch should be replaced by a branch to the next instruction
(or nop.w on thumb2).  This patch accepts this case and restores the
previous behaviour in such cases.

This was reported by failures in the GCC testsuite, where we fail to
link executables because __deregister_frame_info is undefweak:

(__deregister_frame_info): Unknown destination type (ARM/Thumb) in ...crtbegin.o
crtbegin.o: in function `__do_global_dtors_aux':
crtstuff.c:(.text+0x52): dangerous relocation: unsupported relocation
2024-09-11 15:34:32 +00:00
Mark Harmstone
8e98b4aa43 ld/testsuite: exclude relocs from section contributions PDB test
A bug in ld meant that we were erroneously generating image relocations
for .secrel32 ops, which we then reflected in our PDB section
contributions because the linker was adding a .reloc section.

This was incidental to what we were testing for, so pass
--disable-reloc-section to ld in order to ensure a consistent output.
2024-09-11 01:02:44 +01:00
Xin Wang
9a33a0b9c6 Not append rela for absolute symbol
LoongArch: Not append rela for absolute symbol

Use la.global to get absolute symbol like la.abs.
la.global put address of a global symbol into a got
entry and append a rela for it, which will be used
to relocate by dynamic linker. Dynamic linker should
not relocate for got entry of absolute symbol as it
stores symval not symbol's address.
2024-09-07 10:07:13 +08:00
Christophe Lyon
31ed3a9d69 arm: Do not insert stubs needing Arm code on Thumb-only cores.
We recently fixed a bug in libgcc
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115360)
where a symbol was missing a %function .type decoration.

This meant the linker would silently pick the wrong type of 'farcall
stub', involving Arm-mode instructions on Thumb-only CPUs.

This patch emits an error instead, and warns in some other cases, to
encourage users to add the missing '.type foo,%function' directive.

In practice: in arm_type_of_stub() we no longer try to infer which
stub to use if the destination is of unknown type and the CPU is
Thumb-only; so we won't lie to elf32_arm_size_stubs() which does not
check branch_type.

If branch_type is ST_BRANCH_TO_ARM but the CPU is Thumb-only, we now
convert it to ST_BRANCH_TO_THUMB only if the destination is of
absolute type.  This is to support the case where the destination of
the branch is defined by the linker script (see thumb-b-lks-sym.s and
thumb-bl-lks-sym.s testcases for instance).

The motivating case is covered by the new farcall-missing-type
testcase, where we now emit an error message.  We do not emit an error
when branch_type is ST_BRANCH_UNKNOWN and the CPU supports Arm-mode: a
lot of legacy code (e.g. newlib's crt0.S) lacks the corresponding
'.type foo, %function' directives and even a (too verbose) warning
could be perceived as a nuisance.

Existing testcases where such a warning would trigger:
arm-static-app.s (app_func, app_func2)
arm-rel32.s (foo)
arm-app.s (app_func)
rel32-reject.s () main)
fix-arm1176.s (func_to_branch_to)
but this list is not exhaustive.

For the sake of clarity, the patch replaces occurrences of
sym.st_target_internal = 0; with
sym.st_target_internal = ST_BRANCH_TO_ARM;

enum arm_st_branch_type is defined in include/elf/arm.h, and relies on
ST_BRANCH_TO_ARM==0, as sym.st_target_internal is also initialized to
0 in other target-independent parts of BFD code.  (For instance,
swapping the ST_BRANCH_TO_ARM and ST_BRANCH_TO_THUMB entries in the
enum definition leads to 'interesting' results...)

Regarding the testsuite:
* new expected warning for thumb-b-lks-sym and thumb-bl-lks-sym
* new testcase farcall-missing-type to check the new error case
* attr-merge-arch-2b.s, branch-futures (and bfs-1.s) updated to avoid
  a diagnostic

Tested on arm-eabi and arm-pe with no regression.
2024-09-04 13:35:10 +00:00
H.J. Lu
67e30b1521 x86: Check invalid TLS descriptor call
TLS descriptor call,

call *x@tlsdesc(%rax)

or

call *x@tlsdesc(%eax)

calls _dl_tlsdesc_return which expects that RAX/EAX points to the TLS
descriptor.  Update x86 linker to issue an error with or without TLS
transition.

bfd/

	PR ld/32123
	* elf32-i386.c (elf_i386_check_tls_transition): Move
	R_386_TLS_DESC_CALL to ...
	(elf_i386_tls_transition): Here.
	* elf64-x86-64.c (elf_x86_64_check_tls_transition): Move.
	R_X86_64_TLSDESC_CALL check to ...
	(elf_x86_64_tls_transition): Here.

ld/

	PR ld/32123
	* testsuite/ld-i386/i386.exp: Run tlsgdesc3.
	* testsuite/ld-i386/tlsgdesc3.d: New file.
	* testsuite/ld-x86-64/tlsdesc5.d: Likewise.
	* testsuite/ld-x86-64/x86-64.exp: Run tlsdesc5.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-08-30 05:18:10 -07:00
Alan Modra
9459f695d3 ld testsuite output files
In many cases the output of one run_cc_link_tests test is used as
input for another test.  I hit a case where some system change caused
errors when compiling object files, but the old .so output from a
previous test run was still there, and then was used in following
tests.

	* testsuite/lib/ld-lib.exp (run_ld_link_tests): Delete output
	file before building.
	(run_ld_link_exec_tests, run_cc_link_tests): Likewise.
2024-08-29 21:05:57 +09:30
H.J. Lu
c9652a062f x86: Report expected register for elf_x86_tls_error_indirect_call
Since R_386_TLS_DESC_CALL can only be used with

	call	*variable@TLSCALL(%eax)

and R_X86_64_TLSDESC_CALL can only be used with

	call	*variable@TLSCALL(%rax)

update TLS transition error report to display the expected register in
indirect CALL.

bfd/

	PR ld/32017
	* elfxx-x86.c (_bfd_x86_elf_link_hash_table_create): Initialize
	the ax_register field.
	(_bfd_x86_elf_link_report_tls_transition_error): Report the
	expected register in elf_x86_tls_error_indirect_call error.
	* elfxx-x86.h (elf_x86_link_hash_table): Add ax_register.

ld/

	PR ld/32017
	* testsuite/ld-i386/tlsgdesc2.d: Updated.
	* testsuite/ld-i386/tlsgdesc2.s: Change jmp to call via ECX.
	* testsuite/ld-x86-64/tlsdesc4.d: Updated.
	* testsuite/ld-x86-64/tlsdesc4.s: Change jmp to call via RCX.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-08-28 05:56:25 -07:00
Alan Modra
0eb85fc242 Re: x86: Allow R_386_TLS_LE_32 with KMOVD
Adjust the new test to pass on i686-pc-elf where it failed due to not
matching the _start address.
2024-08-28 22:19:38 +09:30
H.J. Lu
d660d20c0c x86: Allow R_386_TLS_LE_32 with KMOVD
Since there is no TLS IE transition, allow R_386_TLS_LE_32 with KMOVD.

gas/

	PR gas/28595
	* config/tc-i386.c (i386_assemble): Remove BFD_RELOC_386_TLS_LE_32
	from TLS code check.
	* testsuite/gas/i386/inval-tls.s: Remove foo@tpoff(%eax).
	* testsuite/gas/i386/inval-tls.l: Updated.

ld/

	PR gas/28595
	* testsuite/ld-i386/i386.exp: Run tlsle1.
	* testsuite/ld-i386/tlsle1.d: New file.
	* testsuite/ld-i386/tlsle1.s: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-08-27 13:16:40 -07:00
Mark Harmstone
91555eddd4 ld/PDB: handle pointers to members
If the CV_PTR_MODE_PMEM or CV_PTR_MODE_PMFUNC flags were set in an
LF_POINTER entry's attributes, there's a few extra bytes on the end that
we weren't accounting for.

Change handle_type so that we remap the containing_class field if it's
present, and add a test for this.
2024-08-26 16:12:52 +01:00
Lulu Cai
05b3e96495 LoongArch: Fix wrong relocation handling of symbols defined by PROVIDE
If the symbol defined by PROVIDE in the link script is not in SECTION,
the symbol is placed in the ABS section. The linker considers that
symbols in the ABS section do not need to calculate PC relative offsets.

Symbols in ABS sections should calculate PC relative offsets normally
based on relocations.
2024-08-26 17:25:45 +08:00
Jan Beulich
b994624fa5 RISC-V: process rs_align_code also when relaxing
riscv_handle_align() runs after all input was processed. Whether
relaxation is enabled for any particular piece of code is not recorded
anywhere. (This issue was even "worked around" in a gas testcase, which
is adjusted accordingly.) Furthermore, as demonstrated by an ld
testcase, tail padding in an object file's executable sections depended
on whether relaxation was enabled at the end of assembly: NOPs were
emitted only when relaxation was off; zeroes were emitted with
relaxation enabled. (It could probably be either way, but it should be
independent of relaxation state at the end of assembly. Except of course
write.c, in a comment ahead of #define-ing SUB_SEGMENT_ALIGN(),
explicitly says "proper nop-filling".)

While re-indenting, drop the "odd_padding" variable. It's used exactly
once, and having the actual expression right in the if() is imo helping
readers to understand what the intentions are.

While touching the ld testcase, also tighten the expectations for the
addresses of the two symbols: The last two digits have to have fixed
values.
2024-08-23 09:22:30 +02:00
H.J. Lu
eb53cbeca8 lto: Add a test for PR ld/32083
Add a test for PR ld/32083 and xfail the test for GCC without the fix:

commit a98dd536b1017c2b814a3465206c6c01b2890998
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Aug 21 07:25:25 2024 -0700

    Update LDPT_REGISTER_CLAIM_FILE_HOOK_V2 linker plugin hook

	PR ld/32083
	* testsuite/ld-plugin/common-2a.c: New file.
	* testsuite/ld-plugin/common-2b.c: Likewise.
	* testsuite/ld-plugin/lto.exp: Run PR ld/32083 test.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-08-22 06:34:53 -07:00
H.J. Lu
4184396d58 Revert "MIPS: correct macro use in gas and ld testsuites"
This reverts commit c0e9aca554.

commit 6ae8a30d44
Author: Jan Beulich <jbeulich@suse.com>
Date:   Fri Aug 9 11:59:31 2024 +0200

    gas: have scrubber retain more whitespace

has been reverted to fix PR gas/32073.
2024-08-15 09:49:33 -07:00
H.J. Lu
a6f8fe0a9e lto: Don't include unused LTO archive members in output
When plugin_object_p is called by elf_link_is_defined_archive_symbol to
check if a symbol in archive is a real definition, set archive member
plugin_format to bfd_plugin_yes_unused to avoid including the unused LTO
archive members in linker output.  When plugin_object_p is called as
known used, call plugin claim_file if plugin_format is bfd_plugin_unknown
or bfd_plugin_yes_unused.

To get the proper support for archives with LTO common symbols with GCC,
the GCC fix for

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116361

is needed.

bfd/

	PR ld/32083
	* archures.c (bfd_arch_get_compatible): Treat bfd_plugin_yes_unused
	the same as bfd_plugin_yes.
	* elflink.c (elf_link_is_defined_archive_symbol): Likewise.
	* bfd.c (bfd_plugin_format): Add bfd_plugin_yes_unused.
	* plugin.c (try_claim): Try claim_file_v2 first.
	* bfd-in2.h: Regenerated.

ld/

	PR ld/32083
	* plugin.c (plugin_call_claim_file): Add an argument to return
	if LDPT_REGISTER_CLAIM_FILE_HOOK_V2 is used.
	(plugin_object_p): When KNOWN_USED is false, we call plugin
	claim_file if plugin_format is bfd_plugin_unknown and set
	plugin_format to bfd_plugin_yes_unused on LTO object.  When
	KNOWN_USED is true, we call plugin claim_file if plugin_format
	is bfd_plugin_unknown or bfd_plugin_yes_unused.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-08-15 03:54:11 -07:00
H.J. Lu
8be33827cd ld: Add an LTO test for common symbol override
Linker checks if a symbol in an archive member is a real definition, not
common, before including the archive member in the link output so that
only a real definition in archive will override the common symbol in
object file.  Add an LTO test to verify that a real definition in archive
overrides the common symbol in object file.

	* testsuite/ld-plugin/common-1.c: New file.
	* testsuite/ld-plugin/definition-1.c: Likewise.
	* testsuite/ld-plugin/lto.exp: Run common tests.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-08-14 08:40:18 -07:00
Xi Ruoyao
91e0b46551 LoongArch: Fix assertion failure with DT_RELR
In the DT_RELR implementation I missed a code path emiting relative
reloc entries.  Then the already packed relative reloc entries will be
(unnecessarily) pushed into .rela.dyn but we've not allocated the space
for them, triggering an assertion failure.

Unfortunately I failed to notice the issue until profiled bootstrapping
GCC with LTO and -Wl,-z,pack-relative-relocs.  The failure can be easily
triggered by linking a "hello world" program with -fprofile-generate and
LTO:

    $ PATH=$HOME/ld-test:$PATH gcc hw.c -fprofile-generate -Wl,-z,pack-relative-relocs -flto
    /home/xry111/git-repos/binutils-build/TEST/ld: BFD (GNU Binutils) 2.43.50.20240802 assertion fail ../../binutils-gdb/bfd/elfnn-loongarch.c:2628
    /home/xry111/git-repos/binutils-build/TEST/ld: BFD (GNU Binutils) 2.43.50.20240802 assertion fail ../../binutils-gdb/bfd/elfnn-loongarch.c:2628
    collect2: error: ld returned 1 exit status

And the reduced test case is just incredibly simple (included in the
patch) so it seems I'm just stupid enough to fail to detect it before.
Let's fix it now anyway.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
2024-08-14 17:45:02 +08:00
H.J. Lu
602f5cf7e3 ld: Add PR ld/32067 tests
Add PR ld/32067 tests with the compiler driver since the -plugin option
is needed to trigger this --oformat binary bug.

	PR ld/32067
	* testsuite/ld-i386/i386.exp: Run PR ld/32067 test.
	* testsuite/ld-x86-64/x86-64.exp: Likewise.
	* testsuite/ld-i386/start.s: Add .note.GNU-stack section.
	* testsuite/ld-x86-64/pr32067.s: New file.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-08-10 07:26:40 -07:00
Alan Modra
b93dcd1cdd loongarch ld testsuite xpasses
Some tests started passing with commit 3a83f0342e.  However,
supporting a changed ld output format is not so simple, and the change
to the loongarch_elf_hash_table macro needs further changes to the
rest of the code.  It is true that some uses of
loongarch_elf_hash_table do not need to check the type of the hash
table, but others like loongarch_elf_relax_section do need to check.
bfd_relax_section is called in lang_size_sections using the input bfd,
not the output bfd.  If the input bfd may be of different type to the
output, then the hash table type must be checked before accessing
elements of the hash table.  This patch corrects
loongarch_elf_relax_section.  I haven't checked all the uses of the
hash table throughout the loongarch backend.

bfd/
	* elfnn-loongarch.c (loongarch_elf_relax_section): Don't relax
	unless the hash table is loongarch_elf_link_hash_table.
	Move variable declarations.  Formatting.
ld/
	* testsuite/ld-elf/pr21884.d: Don't xfail loongarach.
	* testsuite/ld-unique/pr21529.d: Likewise.
2024-08-07 09:37:12 +09:30
mengqinggang
3a83f0342e LoongArch: Add support for OUTPUT_FORMAT("binary")
In binary output format, loongarch_elf_hash_table return NULL and result
in segment fault.

When ld output binary file, it seems that elf related functions should
not be called. But loongarch_elf_relax_section be called and
loongarch_elf_hash_table cause segment fault.

Just redefined loongarch_elf_hash_table and always return
link_info->hash.

The tests of binutils, glibc and gcc is ok.

0  loongarch_elf_relax_section ()
1  0x000055555557ab28 in lang_size_sections_1 ()
2  0x000055555557a16c in lang_size_sections_1 ()
3  0x000055555557b0a8 in one_lang_size_sections_pass ()
4  0x000055555557b478 in lang_size_sections ()
5  0x000055555557e65c in lang_relax_sections ()
6  0x000055555559f9c8 in ldelf_map_segments ()
7  0x000055555559783c in gldelf64loongarch_after_allocation ()
8  0x000055555558dac0 in ldemul_after_allocation ()
9  0x000055555557f6c0 in lang_process ()
10 0x0000555555585314 in main ()
2024-08-05 17:38:49 +08:00
H.J. Lu
3221c470f0 LTO: Restore the wrapper symbol check for standard function
Call unwrap_hash_lookup to restore the wrapper symbol check for standard
function since reference to standard function may not show up in LTO
symbol table:

[hjl@gnu-tgl-3 pr31956-3]$ nm foo.o
00000000 T main
         U __real_malloc
00000000 T __wrap_malloc
[hjl@gnu-tgl-3 pr31956-3]$  lto-dump -list foo.o
Type   Visibility  Size  Name
function  default     0  malloc
function  default     0  __real_malloc
function  default     3  main
function  default     5  __wrap_malloc
[hjl@gnu-tgl-3 pr31956-3]$ make
gcc -O2 -flto -Wall   -c -o foo.o foo.c
gcc -Wl,--wrap=malloc -O2 -flto -Wall -o x foo.o
/usr/local/bin/ld: /tmp/ccsPW0a9.ltrans0.ltrans.o: in function `main':
<artificial>:(.text.startup+0xa): undefined reference to `__wrap_malloc'
collect2: error: ld returned 1 exit status
make: *** [Makefile:22: x] Error 1
[hjl@gnu-tgl-3 pr31956-3]$

Also add a test to verify that the unused wrapper is removed.

	PR ld/31956
	* plugin.c (get_symbols): Restore the wrapper symbol check for
	standard function.
	* testsuite/ld-plugin/lto.exp: Run the malloc test and the
	unused test.
	* testsuite/ld-plugin/pr31956c.c: New file.
	* testsuite/ld-plugin/pr31956d.c: New file.
	* testsuite/ld-plugin/pr31956d.d: New file.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2024-08-03 13:52:38 +09:30
Lulu Cai
ca5915512b LoongArch: Fix ld FAIL test cases
To avoid differences in C library paths on different systems
use gcc instead of ld to perform the test.

Problems caused by adding options to different distributions
will not be fixed.
2024-08-01 10:23:37 +08:00
Mark Harmstone
e859b93a8c ld/PDB: handle empty LF_FIELDLIST types
Empty structs in C++ lead to empty LF_FIELDLIST types in the .debug$T
section, but we were mistakenly rejecting these as invalid. Allow
CodeView types of two bytes, and add a test for this.
2024-08-01 01:33:34 +01:00
Nick Alcock
5f1077e69e
libctf: improve ECTF_NOPARENT error message
This erorr doesn't just indicate that there is no parent dictionary
(that's routine, and true of all dicts that are parents themselves)
but that a parent is *needed* but wasn't found.

include/
	* ctf-api.h (_CTF_ERRORS) [ECTF_NOPARENT]: Improve error message.

ld/
	* testsuite/ld-ctf/diag-parname.d: Adjust.
2024-07-31 21:02:05 +01:00