Commit Graph

7853 Commits

Author SHA1 Message Date
Alan Modra
4739b9137e Close elements of output archive
When cleaning up an archive, close all its elements.  This fixes a
number of ar memory leaks.

bfd/
	* archive.c (_bfd_archive_close_and_cleanup): Close elements
	of an archive open for writing.
binutils/
	* objcopy.c (copy_archive): Don't close output archive
	elements here.
	* dlltool.c (gen_lib_file): Likewise.
ld/
	* pe-dll.c (pe_dll_generate_implib): Don't close output
	archive elements here.
2025-01-01 22:52:06 +10:30
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
Nick Clifton
14848fc2b2
Fix examples of the use of the linker script TYPE keyword 2024-12-20 11:00:15 +00:00
Jan Beulich
e521df4892 ld/doc: properly separate @samp from @item
At least makeinfo 4.13 doesn't tolerate @item@samp, considering it a
single command.
2024-12-16 12:43:37 +01:00
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
Alan Modra
e3fa5cf492 Revert bfd_use_reserved_id patch
Commit fc1cfaa5f1 and bc110b6e40 were made to avoid testsuite
regressions on a number of targets that used bfd id in symbol hashing.
Since it no longer seems necessary to start plugin bfd id's from -1
and count down, revert the functional changes in those patches.
2024-12-12 16:04:51 +10:30
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
Nick Clifton
edd8e9d6c3
Add linker diagnostic message about missing static libraries 2024-12-09 13:13:38 +00:00
Alan Modra
1217d56392 fix dependencies for ld/emultemp/nto.em
Don't use "." to source .em files, use "source_em".
2024-12-07 09:05:56 +10:30
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
Nick Clifton
a042adc985
Fix clang compile time warning about optarg parameter shadowing optarg global variable. 2024-12-05 09:58:06 +00:00
Lulu Cai
eb06e39641 LoongArch: Fix the infinite loop caused by calling undefweak symbol
The undefweak symbol value of non-default visibility is 0 and does
not use plt entry, and will not be relocated in the relocate_secion
function. As a result, an infinite loop is generated because
bl %plt(sym) => bl 0.

Fix this by converting the call into a jump address 0.
2024-12-03 19:52:35 +08:00
Matthieu Longo
4de92ce8b2 aarch64: add tests for combinations of GCS options and marked/unmarked inputs 2024-12-02 15:18:41 +00:00
Matthieu Longo
9957996fa8 aarch64: add tests to check the correct merge of the GCS feature with others. 2024-12-02 15:18:41 +00:00
Srinath Parvathaneni
b75ce33f0c aarch64: GCS feature check in GNU note properties for input objects
This patch adds support for Guarded Control Stack in AArch64 linker.

This patch implements the following:
1) Defines GNU_PROPERTY_AARCH64_FEATURE_1_GCS bit for GCS in
GNU_PROPERTY_AARCH64_FEATURE_1_AND macro.

2) Adds readelf support to read and print the GCS feature in GNU
properties in AArch64.

Displaying notes found in: .note.gnu.property
[      ]+Owner[        ]+Data size[    ]+Description
  GNU                  0x00000010      NT_GNU_PROPERTY_TYPE_0
      Properties: AArch64 feature: GCS

3) Adds support for the "-z gcs" linker option and document all the values
allowed with this option (-z gcs[=always|never|implicit]) where "-z gcs" is
equivalent to "-z gcs=always". When '-z gcs' option is omitted from the
command line, it defaults to "implicit" and relies on the GCS feature
marking in GNU properties.

4) Adds support for the "-z gcs-report" linker option and document all the
values allowed with this option (-z gcs-report[=none|warning|error]) where
"-z gcs-report" is equivalent to "-z gcs-report=warning". When this option
is omitted from the command line, it defaults to "warning".

The ABI changes adding GNU_PROPERTY_AARCH64_FEATURE_1_GCS to the GNU
property GNU_PROPERTY_AARCH64_FEATURE_1_AND is merged into main and
can be found in [1].

[1] https://github.com/ARM-software/abi-aa/blob/main/sysvabi64/sysvabi64.rst

Co-authored-by: Matthieu Longo <matthieu.longo@arm.com>
Co-authored-by: Yury Khrustalev <yury.khrustalev@arm.com>
2024-12-02 15:18:41 +00:00
Matthieu Longo
f1cd84f4c5 aarch64: rename BTI error/warning message
The previous message for missing BTI feature in GNU properties was
not very clear. The new message explains that a missing GNU property
marking is lacking on this specific input.
2024-12-02 15:18:41 +00:00
Matthieu Longo
3c3c758747 aarch64: delete duplicated BTI tests 2024-12-02 15:18:41 +00:00
Matthieu Longo
83c22eb44e aarch64: improve test coverage for combination of BTI options 2024-12-02 15:18:41 +00:00
Matthieu Longo
1bd9dbeaf3 aarch64: update ld documentation with bti and pac options 2024-12-02 15:18:40 +00:00
Matthieu Longo
caded0cf73 aarch64: use only one type for feature marking report 2024-12-02 15:18:40 +00:00
Matthieu Longo
23189e9db7 aarch64: group software protection options under a same struct.
- declare a new struc aarch_protection_opts to store all the
  configuration options related to software protections (i.e. bti-plt,
  pac-plt, bti-report level).
- add a new option "-z bti-report" to configure the log level of reported
  issues when BTI PLT is forced.
- encapsulate the BTI report inside _bfd_aarch64_elf_check_bti_report.
2024-12-02 15:18:40 +00:00
Matthieu Longo
79aff6ea0e aarch64: adapt BTI tests to use selectable GNU properties 2024-12-02 15:18:40 +00:00
Matthieu Longo
8949929a51 aarch64: adapt bti-far* tests to use selectable GNU properties 2024-12-02 15:18:40 +00:00
Matthieu Longo
a32bc164a6 aarch64: adapt tests for PAC PLT to use selectable GNU properties 2024-12-02 15:18:40 +00:00
Matthieu Longo
be7561dbfd aarch64: delete old tests for PAC & BTI PLT 2024-12-02 15:18:40 +00:00
Matthieu Longo
3e7a866acc aarch64: new tests for BTI & PAC PLT to use selectable GNU properties 2024-12-02 15:18:40 +00:00
Matthieu Longo
db61887f96 aarch64: adapt bti-plt-so to use selectable GNU properties 2024-12-02 15:18:40 +00:00
Matthieu Longo
29d10da8a9 aarch64: delete old tests covering the merge of feature markings 2024-12-02 15:18:40 +00:00
Matthieu Longo
0db9d90edf aarch64: new tests covering the merge of feature markings 2024-12-02 15:18:40 +00:00
Matthieu Longo
2d2d86a687 aarch64: move tests for AArch64 protections (BTI, PAC) into a subfolder
- moved all the BTI and PAC tests into a new subfolder: "protections".
    bti-far-*
    bti-plt-*
    bti-pac-plt-*
- move several procedures used only for AArch64 linker tests to a new exp
  library file aarch64-elf-lib.exp in ld/testsuite/ld-aarch64/lib.
- use aarch64-elf-lib.exp in aarch64-ld.exp and aarch64-protections.exp.
2024-12-02 15:18:40 +00:00
Jan Beulich
a96a8b7367 x86: always set ISA_1_BASELINE property for 64-bit objects
The baseline was, afaik, specifically chosen to align with the baseline
ISA of x86-64. It therefore makes no sense to emit that property only
conditionally; if anything it confuses tools analyzing the difference
between generated object files, which may result from just
added / changed / removed (entirely ISA-independent) code, without any
change to the enabled extensions. Compilers, after all, are free to use
these baseline "extensions" when generating 64-bit code.

While changing the one testcase that needs adjustment, also correct its
misleading name (to be in sync with the filename).
2024-12-02 09:38:47 +01:00
Jens Remus
919a671bcc s390: Fix linker test TLS -fpic and -fno-pic exec transitions
Commit 36bbf8646c ("s390: Treat addressing operand sequence as one in
disassembler") changed how plain "nop" gets disassembled and missed to
update any affected linker tests accordingly.

ld/testsuite/
	* ld-s390/tlsbin.dd: "nop" disassembles into "nop".

Fixes: 36bbf8646c ("s390: Treat addressing operand sequence as one in disassembler")
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2024-11-29 16:57:01 +01:00
Liu Hao
db00f6c3ac
PE LD: Merge .CRT .ctors and .dtors into .rdata
PR 32264
2024-11-27 14:27:53 +00:00
Nick Clifton
fe217087a4
Tidy up the default ELF linker script 2024-11-27 11:23:38 +00:00
Alan Modra
7a55d8e4df Re: nios2: Remove binutils support for Nios II target
Remove a now unused config file, regenerate POTFILES to remove nios2
refs, and modify config.bfd to report the target is obsolete.
2024-11-27 10:34:08 +10:30
Sandra Loosemore
e7a16d9fd6 nios2: Remove binutils support for Nios II target.
The Nios II architecture has been EOL'ed by the vendor.  This patch
removes all binutils, bfd, gas, binutils, and opcodes support for this
target with the exception of the readelf utility.  (The ELF EM_*
number remains valid and the relocation definitions from the Nios II
ABI will never change in future, so retaining the readelf support
seems consistent with its purpose as a utility that tries to parse the
headers in any ELF file provided as an argument regardless of target.)
2024-11-26 19:13:07 +00:00
Nick Clifton
3b00205cd8
Updated Bulgarian, Romanian and French translations for various sub-directories. New Georgian translation for the gold sub-directory. 2024-11-25 15:53:22 +00:00
Martin Storsjö
3c557e1ae9 ld/PE: Handle MS style import libraries for files named *.exe too
When handling MS style import libraries (also called short import
libraries, or ILF), we need to detect the kind of library.

So far, this has been done by looking at the member file names
in the import library - in an MS style import library, all the
member files for a specific library have the same member file
name - the name of the runtime module to link against. Usually
this is a DLL - thus we do a case insensitive comparison and
check if the suffix is .dll.

However, an .exe can also export symbols which can be linked
against in the same way. In particular, if linking against
WDK (Windows Driver Kit) import libraries, e.g. wdmsec.lib, the
import libraries can provide imports for ntoskrnl.exe.

Instead of specifically checking for *.dll (and *.exe, etc),
invert the condition and skip archive members named *.o and *.obj.
For any remaining archive members, that do contain .idata
sections, apply the renaming. (The renaming is also mostly
harmless if applied where it isn't needed; if archive members
already have unique file names, their relative ordering should
remain intact except for very contrieved cases.)

Signed-off-by: Martin Storsjö <martin@martin.st>
2024-11-22 12:39:42 +02:00
Nelson Chu
cb45bd5948 RISC-V: Don't report warnings when linking different privileged spec objects.
Since only the abandoned privileged spec v1.9.1 will have conflict csrs, to
keep the compatible we still report warnings when linking privileged spec
v1.9.1 objects with others.  But don't report warnings for other compatible
cases because it is actually a bit noisy and useless...

bfd/
	* elfnn-riscv.c (riscv_merge_attributes): Only report warnings when
	linking the abandoned privileged spec v1.9.1 object with others.
ld/
	* testsuite/ld-riscv-elf/attr-merge-priv-spec-failed-01.d: Removed.
	* testsuite/ld-riscv-elf/attr-merge-priv-spec-failed-02.d: Removed.
	* testsuite/ld-riscv-elf/attr-merge-priv-spec-failed-03.d: Removed.
	* testsuite/ld-riscv-elf/attr-merge-priv-spec-failed-04.d: Removed.
	* testsuite/ld-riscv-elf/attr-merge-priv-spec-failed-05.d: Removed.
	* testsuite/ld-riscv-elf/attr-merge-priv-spec-failed-06.d: Removed.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated.
2024-11-19 13:44:49 +08:00
Lulu Cai
77bcfb741c LoongArch: Do not relax pcalau12i+ld.d when there is overflow
There is no overflow check for the relaxation of pcalau12i+ld.d =>
pcalau12i+addi.d. For instruction sequences that can be relaxed,
they are directly relaxed to pcalau12i+addi.d. However, when the
relative distance between the symbol and the pc exceeds the 32-bit
range, the symbol value cannot be obtained correctly.

Adds an overflow check for the relaxation of pcalau12i+ld.d.
If it is found that the relaxation will overflow, it will not
be relaxed.
2024-11-19 09:42:23 +08:00
Matthieu Longo
1b3b96ba32 aarch64: renaming of arm to AArch64 2024-11-18 18:03:56 +00:00
Benjamin Drung
b0cc81e870 ld: Support percent-encoded JSON in --package-metadata
Specifying the compiler flag `-Wl,--package-metadata=<JSON>` will not
work in case the JSON contains a comma, because compiler drivers eat
commas. Example:

```
$ echo "void main() { }" > test.c
$ gcc '-Wl,--package-metadata={"type":"deb","os":"ubuntu"}' test.c
/usr/bin/ld: cannot find "os":"ubuntu"}: No such file or directory
collect2: error: ld returned 1 exit status
```

The quotation marks in the JSON value do not work well with shell nor
make. Specifying the `--package-metadata` linker flag in a `LDFLAGS`
environment variable might loose its quotation marks when it hits the
final compiler call.

So support percent-encoded and %[string] encoded JSON data in the
`--package-metadata` linker flag. Percent-encoding is used because it is
a standard, simple to implement, and does take too many additional
characters. %[string] encoding is supported for having a more readable
encoding.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32003
Bug-Ubutru: https://bugs.launchpad.net/bugs/2071468
Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
2024-11-18 11:38:25 +01:00
Sam James
bf447eec6d
ld: fix -std=gnu23 compatibility wrt _Bool
GCC trunk now defaults to -std=gnu23. We return false in a few places
which can't work when true/false are a proper type (_Bool). Return NULL
where appropriate instead of false. All callers handle this appropriately.

ChangeLog:
	PR ld/32372

	* pdb.c (add_stream): Return NULL.
2024-11-18 03:09:16 +00:00
Barnabás Pőcze
8eba92c9e2 PR 32359, --dependency-file: wrong error message if fopen fails
Use of %E in ld error messages requires bfd_error to be set.
2024-11-16 10:07:12 +10:30
Alan Modra
a3741bc6c7 Re: ld: Move note sections after .rodata section
Fix csky-linux-gnu FAIL of ld-elf/pr32341, due to that target having
its own .bss directive.

	PR ld/32341
	* testsuite/ld-elf/pr32341.s: Don't use .bss directive.  Specify
	progbits/nobits on all .section directives.
2024-11-12 17:06:35 +10:30