1078 Commits

Author SHA1 Message Date
Vignesh Balasubramanian
e2b607517e bfd: Add minimal support to handle note that describes xsave layout
This note section is already supported by Linux kernel.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/arch/x86/kernel/fpu/xstate.c?id=ba386777a30b38dabcc7fb8a89ec2869a09915f7

Co-Authored-By: Jini Susan George <jinisusan.george@amd.com>
2025-12-12 14:59:49 +01:00
Alan Modra
4e397ea944 PR 33473 SEGV in _bfd_elf_gc_mark_debug_special_section_group
The code that faulted made the assumption that a group section always
had at least one valid member.  Fix that assumption.  Also fail if all
entries in a SHT_GROUP section are invalid.  (An empty group will not
result in a call to process_sht_group_entries.)

	PR 33473
	* elflink.x (_bfd_elf_gc_mark_debug_special_section_group): Don't
	segfault on empty group.
	* elf.c (process_sht_group_entries): Return false if all
	entries are invalid.
2025-11-25 07:50:17 +10:30
Alan Modra
f5336048ae bfd_copy_private_symbol_data
Allow copy_private_symbol_data to replace osym if a target desires.
Change isym similarly for symmetry.  The idea is to make it possible
to give the asymbol an output target specific extension.  Some
targets, eg. som, use such an extension when outputting symbols,
behaving badly if the input object is not som.  There are no
functional changes in this patch; It just changes the signatures.

bfd/
	* elf-bfd.h (_bfd_elf_copy_private_symbol_data): Replace
	asymbol* params with asymbol**.
	* elf.c (_bfd_elf_copy_private_symbol_data): Likewise.
	* libbfd-in.h (_bfd_bool_bfd_asymbol_bfd_asymbol_true): Likewise.
	* libbfd.c (_bfd_bool_bfd_asymbol_bfd_asymbol_true): Likewise.
	* mach-o.c (bfd_mach_o_bfd_copy_private_symbol_data): Likewise.
	* mach-o.h (bfd_mach_o_bfd_copy_private_symbol_data): Likewise.
	* plugin.c (bfd_plugin_bfd_copy_private_symbol_data): Likewise.
	* som.c (som_bfd_copy_private_symbol_data): Likewise.
	* targets.c (bfd_target <_bfd_copy_private_symbol_data>): Likewise.
	* bfd-in2.h: Regenerate.
	* libbfd.h: Regenerate.
binutils/
	* objcopy.c (copy_object): Adjust bfd_copy_private_symbol_data call.
gas/
	* symbols.c symbol_clone): Adjust bfd_copy_private_symbol_data call.
2025-11-22 09:13:26 +10:30
Alan Modra
0feda62e04 copy_private and merge_private functions
These are all called via BFD_SEND on the output bfd xvec.  Thus there
is no need to verify the output bfd flavour.  There *is* a need to
verify the input bfd flavour.  Also of course target specific data
shouldn't be accessed until the input target is verified.  Tidy these
issues in many places.

bfd_copy_private_section_data, bfd_copy_private_symbol_data, and
bfd_merge_private_bfd_data are macros.  Delete prototypes created via
synopsis entry in comments.

	* aout-target.h (MY_bfd_copy_private_section_data): Remove
	unnecessary output bfd flavour check.
	* coff-arm.c (coff_arm_merge_private_bfd_data): Likewise.
	* elf-m10300.c (_bfd_mn10300_elf_merge_private_bfd_data): Likewise.
	* ecoff.c (_bfd_ecoff_bfd_copy_private_bfd_data): Likewise, and
	move setup of pointers into target specific data after check.
	* elf.c (_bfd_elf_copy_private_bfd_data): Likewise.
	(_bfd_elf_copy_private_symbol_data): Likewise.
	(copy_private_bfd_data): Remove checks duplicating those done
	in caller.
	(_bfd_elf_copy_private_section_data): Remove unnecessary
	output bfd flavour check.
	(_bfd_elf_copy_private_header_data): Likewise.
	* elf32-arc.c (arc_elf_print_private_bfd_data): Likewise.
	(arc_elf_merge_private_bfd_data): Likewise.
	* elf32-arm.c (elf32_arm_copy_private_bfd_data): Likewise.
	(elf32_arm_merge_private_bfd_data): Likewise.
	* elf32-cr16.c (_bfd_cr16_elf_merge_private_bfd_data): Likewise.
	* elf32-cris.c (cris_elf_merge_private_bfd_data): Likewise.
	(cris_elf_merge_private_bfd_data): Likewise.
	* elf32-csky.c (csky_elf_merge_private_bfd_data): Likewise.
	* elf32-h8300.c (elf32_h8_merge_private_bfd_data): Likewise.
	* elf32-lm32.c (lm32_elf_fdpic_copy_private_bfd_data): Likewise.
	* elf32-m32r.c (m32r_elf_merge_private_bfd_data): Likewise.
	* elf32-m68hc1x.c (_bfd_m68hc11_elf_merge_private_bfd_data): Likewise.
	* elf32-m68k.c (elf32_m68k_merge_private_bfd_data): Likewise.
	* elf32-mcore.c (mcore_elf_merge_private_bfd_data): Likewise.
	* elf32-nds32.c (nds32_elf_merge_private_bfd_data): Likewise.
	* elf32-or1k.c (elf32_or1k_merge_private_bfd_data): Likewise, and
	move setup of pointers into target specific data after check.
	* elf32-ppc.c (ppc_elf_merge_private_bfd_data): Remove unnecessary
	output bfd flavour check.
	* elf32-s390.c (elf32_s390_merge_private_bfd_data): Likewise.
	* elf32-score.c (s3_elf32_score_merge_private_bfd_data): Likewise,
	and move check before target specific data accesss.
	* elf32-sh.c (sh_elf_copy_private_data): Remove unnecessary
	output bfd flavour check.
	(sh_elf_merge_private_data): Likewise.
	* elf32-sparc.c (elf32_sparc_merge_private_bfd_data): Likewise.
	* elf32-v850.c (v850_elf_merge_private_bfd_data): Likewise.
	* elf32-vax.c (elf32_vax_merge_private_bfd_data): Likewise.
	* elf32-visium.c (visium_elf_copy_private_bfd_data): Likewise.
	* elf32-xtensa.c (elf_xtensa_merge_private_bfd_data): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_merge_private_bfd_data): Likewise.
	* elf64-ppc.c (ppc64_elf_merge_private_bfd_data): Likewise.
	* elf64-s390.c (elf64_s390_merge_private_bfd_data): Likewise.
	* elf64-sparc.c (elf64_sparc_merge_private_bfd_data): Likewise.
	* elfnn-aarch64.c (elfNN_aarch64_merge_private_bfd_data): Likewise.
	* elfnn-ia64.c (elfNN_ia64_merge_private_bfd_data): Likewise.
	* elfnn-kvx.c (elfNN_kvx_merge_private_bfd_data): Likewise.
	* elfnn-loongarch.c (elfNN_loongarch_merge_private_bfd_data): Likewise.
	* elfnn-riscv.c (_bfd_riscv_elf_merge_private_bfd_data): Likewise.
	* elfxx-mips.c (_bfd_mips_elf_merge_private_bfd_data): Likewise.
	* mach-o.c (bfd_mach_o_bfd_copy_private_section_data): Likewise.
	(bfd_mach_o_bfd_copy_private_header_data): Likewise.
	* mmo.c (mmo_bfd_copy_private_bfd_data): Likewise.
	* peXXigen.c (_bfd_XX_bfd_copy_private_bfd_data_common): Likewise.
	(_bfd_XX_bfd_copy_private_section_data): Likewise.
	* som.c (som_bfd_copy_private_section_data): Likewise.
	(som_bfd_copy_private_bfd_data): Likewise.
	(som_bfd_copy_private_symbol_data): Likewise, and move setup
	of pointers into target specific data after check.

	* elf32-bfin.c (elf32_bfin_merge_private_bfd_data): Check
	input bfd flavour.
	* elf32-frv.c (frv_elf_merge_private_bfd_data): Likewise.
	* elf32-iq2000.c (iq2000_elf_merge_private_bfd_data): Likewise.
	* elf32-m32c.c (m32c_elf_merge_private_bfd_data): Likewise.
	* elf32-mep.c (mep_elf_merge_private_bfd_data): Likewise.
	* elf32-rl78.c (rl78_elf_merge_private_bfd_data): Likewise.
	* elf32-rx.c (rx_elf_merge_private_bfd_data): Likewise.
	* elf32-score7.c (s7_elf32_score_merge_private_bfd_data): Likewise.
	* elf32-tic6x.c (elf32_tic6x_merge_private_bfd_data): Likewise.
	* elf32-v850.c (v850_elf_copy_notes): Likewise.
	* elf32-visium.c (visium_elf_merge_private_bfd_data): Likewise.
	* mach-o.c (bfd_mach_o_bfd_copy_private_symbol_data): Likewise.

	* linker.c (bfd_merge_private_bfd_data): Delete SYNOPSIS.
	* section.c (bfd_copy_private_section_data): Likewise.
	* syms.c (bfd_copy_private_symbol_data): Likewise.
	* bfd-in2.h: Regenerate.
2025-11-22 09:09:12 +10:30
Ezra Sitorus
3c3f58814b bfd/ELF: Core file support for AArch64 FPMR
The Floating Point Mode Register is a new register which controls the
behaviour of FP8 instructions. This is handled by the Linux kernel
through a new NT_ARM_FPMR register set.

This patch adds required code to support core file dumps with
NT_ARM_FPMR in them.
2025-11-17 12:47:09 +00:00
Simon Marchi
6d30159f9a Revert "bfd/ELF: make is_debuginfo_file() static"
This reverts commit 5e648fc6a0, since it
breaks the GDB build:

      CXX    elfread.o
    /home/smarchi/src/binutils-gdb/gdb/elfread.c: In function ‘symfile_segment_data_up elf_symfile_segments(bfd*)’:
    /home/smarchi/src/binutils-gdb/gdb/elfread.c:145:12: error: ‘is_debuginfo_file’ was not declared in this scope
      145 |       if (!is_debuginfo_file (abfd)
          |            ^~~~~~~~~~~~~~~~~

Change-Id: I180a9f6936365c365a853c7dae2af01f5207a84e
2025-11-07 16:43:28 +00:00
Jan Beulich
e3dd2eb6f5 bfd/ELF: mark functions exposed to ld as non-private
As non-private functions, these shouldn't have "_bfd_" prefixes,
but merely "bfd_" ones:
- _bfd_elf_size_group_sections(),
- _bfd_elf_match_sections_by_type(),
- _bfd_elf_strtab_{str,len}(),
- _bfd_elf_map_sections_to_segments(),
- _bfd_elf_tls_setup().
2025-11-07 15:00:25 +01:00
Jan Beulich
8b322fc15a bfd/ELF: _bfd_elf_large_com_section is exposed to gas and x86-only
As a non-private data item, it shouldn't have a "_bfd_" prefix, but merely
a "bfd_" one. Furthermore, as being x86-only (forever since its
introduction), it doesn't need to be present in libbfd.{a,so} at all for
other targets.
2025-11-07 14:59:45 +01:00
Jan Beulich
85c8b58e2d bfd/ELF: properly mark elf_read_notes() as private
Add a _bfd_ prefix and make it hidden.
2025-11-07 14:57:16 +01:00
Jan Beulich
479613f261 bfd/ELF: properly mark bfd_elf_print_symbol() as private
Add a leading underscore and make it hidden.
2025-11-07 14:57:01 +01:00
Jan Beulich
5e648fc6a0 bfd/ELF: make is_debuginfo_file() static
No need to expose it, even less so in the dynamic symbol table.
2025-11-07 14:56:19 +01:00
Jan Beulich
1e1701c4d8 bfd: simplify _bfd_{link,write,discard}_section_stabs() interface
... as well as that of _bfd_stab_section_offset(): As sec_info is now
hanging off of sec, there's no need for the extra 4th parameter anymore.
Along these line struct struct coff_section_tdata's stab_info member then
isn't needed anymore either.

Furthermore there also hasn't been a good reason to have the caller of
_bfd_link_section_stabs() set sec_info_type.
2025-10-24 15:10:35 +02:00
Jan Beulich
f2dbe10f66 bfd: simplify _bfd_merged_section_offset() interface
As sec_info is now hanging off of sec, there's no need for the extra 3rd
parameter anymore; all callers pass as 2nd argument the address of a
section pointer that sec_info can be fetched from.
2025-10-24 15:10:09 +02:00
Jan Beulich
eb92a17c47 bfd: move sec_info from ELF to general section struct
This is in preparation of supporting section merging also when the output
isn't ELF (or not of the same class). Note that it's also more consistent
this way, as the related sec_info_type field also live in the same struct.
2025-10-24 15:09:11 +02:00
Abhay Kandpal
30300ceca3 PowerPC: Fix typo in PowerPC note pseudo-section names
The recent tidy-up commit 09e56f0515 ("Code tidy: bfd/elf.c:
Tidy up core note handling code.") introduced regressions in the
PowerPC core note handling.  The pseudo-section names for TM_CVSX,
TM_SPR, VSX and VMX notes were defined with an underscore instead of a
hyphen, breaking GDB test cases.  The same goes for one riscv and one
s390 define.

This patch restores the correct section names by using hyphens in the
definitions, fixing 897 GDB test failures on PowerPC.

bfd/
	* elf.c: (NOTE_PSEUDO_SECTION_PPC_TM_CVSX),
	(NOTE_PSEUDO_SECTION_PPC_TM_SPR, NOTE_PSEUDO_SECTION_PPC_VSX),
	(NOTE_PSEUDO_SECTION_PPC_VMX, NOTE_PSEUDO_SECTION_RISCV_CSR),
	(NOTE_PSEUDO_SECTION_S390_CTRS): Use hyphens in section names
	instead of underscores.
2025-09-26 06:46:42 +09:30
Nick Clifton
b6ebe65cda Fix compile time warning when building with Clang.
PR 33282
2025-08-18 15:27:34 +01:00
Nick Clifton
c3eb17bae4 Fix typo in recent update to elf.c's core note handling code.
PR 33282
2025-08-18 13:58:01 +01:00
Alan Modra
f586f9b61d buffer overflow in process_sht_group_entries
An oss-fuzz testcase with a SHT_GROUP section named .debug managed to
break objcopy --compress-debug-sections.  The underlying problem is
that SEC_DEBUGGING is set by section name tests, thus the SHT_GROUP
section gets compressed.  The compressed section data is smaller than
the original section sh_size, and process_sht_group_entries tries to
look at sh_size worth of entries.  The patch fixes this mess by simply
not setting SEC_DEBUGGING on SHT_GROUP sections.

Note that it isn't correct to restrict SEC_DEBUGGING to SHT_PROGBITS
sections, as that will break processor/os special sections for debug.
eg. SHT_MIPS_DEBUG.

	* elf.c (_bfd_elf_make_section_from_shdr): Don't set
	SEC_DEBUGGING on SEC_GROUP sections no matter their name.
2025-08-17 16:02:56 +09:30
Nick Clifton
09e56f0515 Code tidy: bfd/elf.c: T|idy up core note handling code. 2025-08-15 14:03:59 +01:00
Jan Beulich
98e6d3f5bd gas/ELF: allow specifying entity size for arbitrary sections
The spec doesn't tie entity size to just SHF_MERGE and SHF_STRINGS
sections. Introduce a new "section letter" 'E' to allow recording (and
checking) of entity size even without 'M' or 'S'.
2025-08-15 12:19:59 +02:00
Jan Beulich
b3743a2c05 bfd: have objcopy retain unknown ELF section flags
Silently zapping them is certainly wrong. When they're not replaced due
to user request, simply keeping them may not always be correct (we don't
know what such a flag means, after all), but is certainly at least
closer to having the output object still represent what the input object
had.

This introduces new binutils/ testsuite failures, but only for two
targets where most of the tests there fail anyway (amdgcn-elf and
nfp-elf), due to there not being an assembler available.
2025-08-15 12:16:22 +02:00
Andrew Burgess
ea6ec00ff4 bfd: support for NT_386_TLS notes
In a later commit I'd like to add support to GDB for including the
NT_386_TLS note in the core files that GDB creates (using 'gcore'
command).

To achieve this we need some standard boilerplate code added to bfd.

The only part of this patch which I think needs consideration is the
name I selected for the pseudo section to hold the note contents when
a core file is loaded.  I chose '.reg-i386-tls'.  The '.reg' prefix is
the standard used by most other pseudo sections, and the '-i386-tls'
suffix seemed to match the note name, though I added the 'i' to
'i386', instead of just using '.reg-386-tls'.  I thought 'i386' seemed
clearer.

There's no test included here, but when I merge the NT_386_TLS
creation to GDB it will depend on this and act as a test.  I plan to
post that work to the GDB list once this patch is merged.
2025-08-14 16:06:47 +01:00
Jan Beulich
1e118fe363 bfd/ELF: make three local arrays static
... and const. There's no reason to have the compiler copy anonymous
objects onto the stack. And there's also no reason to allow the arrays
to be modifiable.
2025-08-08 11:44:12 +02:00
Rainer Orth
4cd91af023 Only parse attributes in ELF sections with the SHT_GNU_ATTRIBUTES type if the OS is not Solaris. Set the is_solaris flag for Sparc solaris
PR 33153
2025-07-15 11:15:46 +01:00
Indu Bhagat
6ab3f09a68 gas: elf: binutils: add new section type SHT_GNU_SFRAME
So far, SFrame sections were of type SHT_PROGBITS.

As per ELF specification, SHT_PROGBITS indicates that the section holds
information defined by the program, whose format and meaning are
determined solely by the program.

On the linker side, SHT_PROGBITS should be reserved for the simple "cat
contents after applying relocs" semantics.

Currently, the only way to know that a section contains SFrame stack
trace data is if consumer checks for section name.  Such a check for
section name is not quite conformant to ELF principles.

Some of this was discussed here
https://sourceware.org/pipermail/binutils/2025-March/140181.html

With this change, the SFrame sections generated by gas, ld will have
section type set to SHT_GNU_SFRAME.   The new section type is defined in
the SHT_LOOS/SHT_HIOS space.  The SFrame parsing routine
_bfd_elf_parse_sframe () now admits sections only when the the section
type is SHT_GNU_SFRAME.

No special handling / validation is done at the moment for the case of
manual creation of SFrame sections via obj_elf_section ().  Add function
level comments for now to add a note about this.

Although the default handling for (sh_type >= SHT_LOOS && sh_type <=
SHT_HIOS) is sufficient when SHT_GNU_SFRAME is in that range, it makes
sense to add it as a case of its own.

bfd/
	* elf-sframe.c (_bfd_elf_parse_sframe): Check if section type is
	SHT_GNU_SFRAME.
	(_bfd_elf_set_section_sframe): Set SHT_GNU_SFRAME for output
	SFrame section.
	* elflink.c (obj_elf_section): Use section type for check
	instead of section name.
	* elfxx-x86.c: Set SHT_GNU_SFRAME for SFrame sections for
	.plt* sections.
	* elf.c (bfd_section_from_shdr): Add case for SHT_GNU_SFRAME.
binutils/
	* readelf.c (get_os_specific_section_type_name): Add
	SHT_GNU_SFRAME.
gas/
	* NEWS: Announce emitted SFrame sections have SHT_GNU_SFRAME
	set.
	* config/obj-elf.c (obj_elf_attach_to_group): Add comments to
	indicate no special handling for SFrame yet.
	* dw2gencfi.c (cfi_finish): Set SHT_GNU_SFRAME for emitted
	SFrame section.
ld/
	* NEWS: Announce emitted SFrame sections have SHT_GNU_SFRAME
	set.
gas/testsuite/
	* gas/cfi-sframe/cfi-sframe.exp: Add new test.
	* gas/cfi-sframe/cfi-sframe-common-1b.d: New test.
	* gas/cfi-sframe/cfi-sframe-common-1b.s: New test.
include/
	* elf/common.h (SHT_GNU_SFRAME): Add new section type for SFrame
	stack trace information.
libsframe/doc/
	* sframe-spec.texi: Add expected ELF section type.
2025-07-11 23:32:58 -07:00
Alan Modra
5942dd9913 Merge init_private_section_data with copy_private_section_data
init_private_section_data is used by the linker and is a special case
of copy_private_section_data that copies a reduced set of section data
from input to output.  Merge the two functions, adding a link_info
param to copy_private_section_data and remove init_private_section_data.
2025-07-09 11:13:55 +09:30
Indu Bhagat
0924a1ca2e ld: bfd: sframe: fix incorrect r_offset in RELA entries
PR/32666  Incorrect .rela.sframe when using ld -r

Input SFrame sections are merged using _bfd_elf_merge_section_sframe (),
which clubs all SFrame FDEs together in one blob and all SFrame FREs in
another.  This, of course, means the offset of an SFrame FDE in the output
section cannot be simply derived from the output_offset of the sections.

Fix this by providing _bfd_elf_sframe_section_offset () which returns
the new offset of the SFrame FDE in the merged SFrame section.

Unlike EH_Frame sections, which also use the _bfd_elf_section_offset (),
to update the r_offset, SFrame sections have distinct merging semantics.
In case of SFrame, the SFrame FDE will not simply sit at location
"sec->output_offset + offset of SFrame FDE in sec".  Recall that information
layout in an SFrame section is as follows:
   SFrame Header
   SFrame FDE 1
   SFrame FDE 2
   ...
   SFrame FDEn
   SFrame FREs (Frame Row Entries)
Note how the SFrame FDEs and SFrame FREs are clubber together in groups
of their own.

Next, also note how the elf_link_input_bfd () does a:
            irela->r_offset += o->output_offset;
This, however, needs to be avoided for SFrame sections because the
placement of all FDEs is at the beginning of the section.  So, rather than
conditionalizing this as follows:
          if (o->sec_info_type != SEC_INFO_TYPE_SFRAME)
            irela->r_offset += o->output_offset;
the implementation in _bfd_elf_sframe_section_offset () does a reverse
adjustment, so that the generic parts of the linking process in
elf_link_input_bfd () are not made to do SFrame specific adjustments.

Add a new enum to track the current state of the SFrame input section
during the linking process (SFRAME_SEC_DECODED, SFRAME_SEC_MERGED) for
each input SFrame section.  This is then used to assert an assumption
that _bfd_elf_sframe_section_offset () is being used on an input SFrame
sections which have not been merged (via
_bfd_elf_merge_section_sframe ()) yet.

bfd/
        * elf-bfd.h: New declaration.
        * elf-sframe.c (_bfd_elf_sframe_section_offset): New definition.
        * elf.c (_bfd_elf_section_offset): Adjust offset if SFrame
	section.
ld/testsuite/
        * ld-x86-64/x86-64.exp: New test.
        * ld-x86-64/sframe-reloc-1.d: New test.
2025-07-06 12:53:03 -07:00
Thiago Jung Bauermann
27e5f9c975 bfd/aarch64-linux: Support reading and writing the GCS core file note
Reviewed-By: Luis Machado <luis.machado@arm.com>
2025-07-03 13:26:40 -03:00
H.J. Lu
41461010eb elf: Report corrupted group section
Report corrupted group section instead of trying to recover.

	PR binutils/33050
	* elf.c (bfd_elf_set_group_contents): Report corrupted group
	section.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-06-23 22:28:14 +08:00
H.J. Lu
412164f0a9 elf: Return false if output_section is NULL
Return false if output_section is NULL so that on input

https://sourceware.org/bugzilla/attachment.cgi?id=16131

objcopy generates

objcopy: /tmp/objcopy-poc(OrcError.cpp.o): invalid entry (0x22000000) in group [3]
objcopy: /tmp/objcopy-poc(OrcError.cpp.o): invalid entry (0x21000000) in group [3]
objcopy: /tmp/objcopy-poc(OrcError.cpp.o)(.text._ZNK12_GLOBAL__N_116OrcErrorCategory7messageB5cxx11Ei): relocation 29 has invalid symbol index 1160982879
objcopy: /tmp/stv73zYw/OrcError.cpp.o[.text._ZN4llvm3orc8orcErrorENS0_12OrcErrorCodeE]: bad value

instead of

objcopy: /tmp/objcopy-poc(OrcError.cpp.o): invalid entry (0x22000000) in group [3]
objcopy: /tmp/objcopy-poc(OrcError.cpp.o): invalid entry (0x21000000) in group [3]
objcopy: /tmp/objcopy-poc(OrcError.cpp.o)(.text._ZNK12_GLOBAL__N_116OrcErrorCategory7messageB5cxx11Ei): relocation 29 has invalid symbol index 1160982879
Segmentation fault (core dumped)

	PR binutils/33075
	* elf.c (elf_map_symbols): Return false if output_section is
	NULL.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-06-13 15:07:07 +08:00
Nick Clifton
3e31877fbf Add checks for illegal symbol binding and type values when reading ELF symbols.
PR 33019
2025-06-03 17:07:03 +01:00
Alan Modra
dc5b07c23b PR 33020 segv in _bfd_elf_strtab_offset
The PR fuzzer testcase creates a SHT_NOBITS .debug_info section, then
triggers a bug in --compress-debug-sections=zlib whereby sh_name is
set to -1 in elf_fake_sections as a flag to indicate the name is not
set yet (may change to zdebug_*), but the section never hits the debug
compression code in assign_file_positions_for_non_load_sections that
is responsible for setting sh_name.

	PR 33020
	* elf.c (_bfd_elf_init_reloc_shdr): Rename delay_st_name_p
	param to delay_sh_name_p.
	(elf_fake_sections): Rename delay_st_name_p to delay_sh_name_p.
	Don't set delay_sh_name_p for no contents debug sections.
2025-05-31 08:17:40 +09:30
Alan Modra
47d7ab7057 Revert "Replace assertions with error return values, thus ensuring an illegal memory access does not occur."
This reverts commit 429fb15134.
2025-05-31 08:16:41 +09:30
Nick Clifton
429fb15134 Replace assertions with error return values, thus ensuring an illegal memory access does not occur.
PR 33020
2025-05-30 13:00:59 +01:00
Christina Schimpe
c44fc1d6b4 bfd: Handle note of type NT_X86_SHSTK 2025-05-19 04:33:54 +08:00
H.J. Lu
5e247da8af elf: Properly set sh_offset for .tbss sections
Set sh_offset for .tbss sections to their nominal offset after aligning.
They are not loaded from disk so the value doesn't really matter, except
when the .tbss section is the first one in a PT_TLS segment.  In that
case, it sets the p_offset for the PT_TLS segment, which according to
the ELF gABI ought to satisfy p_offset % p_align == p_vaddr % p_align.

bfd/

	PR ld/32896
	* elf.c (assign_file_positions_for_load_sections): Properly set
	sh_offset for .tbss sections.

ld/

	PR ld/32896
	* testsuite/ld-elf/tbss4.d: New file.
	* testsuite/ld-elf/tbss4.s: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-04-29 15:39:54 +08:00
Alan Modra
ebed0bfecc meaningless p_offset for zero p_filesz PT_LOAD
This patch avoids generating PT_LOAD segments that trip a bug in
glibc's loader.

	PR 25237
	PR 32763
	* elf.c (assign_file_positions_for_load_sections): Don't put
	p_offset zero for empty PT_LOAD.
2025-03-10 23:02:19 +10:30
Alan Modra
b8c5ada174 Further tidies to bed->p_align code
align_pagesize was used for two things, reducing p->p_align from
maxpagesize to the bed->p_align value (section alignment permitting),
and increasing p->p_align above maxpagesize if section alignment
required that.  This patch untangles those two, making align_pagesize
only do the former.  p->p_align is set directly for the latter.  I've
made that change to p->p_align only when D_PAGED to keep things
consistent with other early assignments to p->p_align.  p->p_align is
set later according to section alignment when not D_PAGED.

I've also moved the place where align_pagesize adjusts p->p_align to
be with other code setting p->p_align.  That seemed better to me than
leaving it until the last possible moment.  Note that it isn't
necessary to have this adjustment done inside a test for a PT_LOAD
header, since we never set align_pagesize non-zero outside a PT_LOAD
test.

	* elf.c (assign_file_positions_for_load_sections): Clear
	align_pagesize whenever we have a section alignment more than
	bed->p_align.  Set p->p_align rather than align_pagesize
	when section alignment exceeds maxpagesize.  Assign p->p_align
	from align_pagesize earlier.
2025-03-10 23:01:54 +10:30
Alan Modra
ce53bc06f6 Tidy code handling bed->p_align a little.
No functional changes here, just preparation for the next patch.

	* elf.c (assign_file_positions_for_load_sections): Replace
	p_align_p and p_align with align_pagesize.  Revise comments
	on code handling bed->p_align.
2025-03-10 23:00:10 +10:30
Alan Modra
d26161914c PR 32603, more ld -w misbehaviour
Commit 8d97c1a53f claimed to replace all einfo calls using %F with
a call to fatal.  It did so only for the ld/ directory.  This patch
adds a "fatal" to linker callbacks, and replaces those calls in bfd/
too.
2025-02-18 09:16:57 +10:30
Alan Modra
79e6242a1c PR32664, compressed debug section naming confusion
The pr326664 fuzzer testcase has two .debug_info sections, one
SHF_ALLOC, one not.  SEC_DEBUGGING is never set for SHF_ALLOC sections
that happen to be named .debug_info, nor are they compressed.  However
in this case we get an output section that is both SEC_ALLOC and
SEC_DEBUGGING which confuses code setting up the output section names
(.zdebug_* for compressed debug sections), resulting in a -1u index
into a string table.

	PR 32664
	* elf.c (elf_fake_sections): Do not delay naming of SEC_ALLOC
	sections.
2025-02-10 08:47:19 +10:30
H.J. Lu
bc61d5916a elf: Store __ehdr_start hash in elf_link_hash_table
Since

commit 97da0e2677
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Jan 12 23:42:23 2022 +1030

    tweak __ehdr_start visibility and flags for check_relocs

creates __ehdr_start hash in lang_symbol_tweaks, store __ehdr_start hash
in elf_link_hash_table so that we just need to lookup it up only once.

bfd/

	* elf-bfd.h (elf_link_hash_table): Add hehdr_start.
	* elf.c (assign_file_positions_for_load_sections): Use
	hehdr_start.

ld/

	* ldelf.c (ldelf_before_allocation): Use hehdr_start for
	__ehdr_start hash.
	* ldlang.c (lang_symbol_tweaks): Store hehdr_start hash in
	hehdr_start.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-02-04 13:02:58 +08:00
Alan Modra
40c7f80788 unusual eh_frame memory leak
This one happens with --gc-sections and a linker script that either
discards some or all .eh_frame sections (eg. ld-elf/pr14265 test) or
maps an input .eh_frame to some other named output section.  In that
case the discarded/renamed .eh_frame won't have local_cies freed.

	* elf-eh-frame.c (_bfd_elf_parse_eh_frame): Correct comment.
	* elf.c (_bfd_elf_free_cached_info): Free eh_frame cies.
2025-01-23 11:40:40 +10:30
Alan Modra
130bb6bbbd _bfd_elf_get_dynamic_symbols
This fixes an error path in _bfd_elf_get_dynamic_symbols, fixes the
minimum size required when reading DT_HASH header, and tidies
formatting in a few places.  Nit-fixes all.

Very likely we shouldn't be trying to mmap DT_DYNAMIC as it won't be
large enough for the mmap size threshold.

	* elf.c (_bfd_elf_get_dynamic_symbols): Use _bfd_munmap_temporary
	in error return path rather than free.  Corrent size passed to
	offset_from_vma when reading DT_HASH header.  Formatting.
2025-01-20 18:16:15 +10:30
Alan Modra
3a8864b3aa reloc caching
This arranges to free section relocs cached in elf_section_data.  To
do that, some relocs stored there need to use bfd_malloc buffers
rather than bfd_alloc ones.

	* elf.c (_bfd_elf_free_cached_info): Free relocs.
	* elf32-ppc.c (ppc_elf_relax_section): Realloc relocs rather
	than malloc, copy, free old.
	* elf64-ppc.c (get_relocs): bfd_malloc relocs.
	* elflink.c (_bfd_elf_link_info_read_relocs): Always
	bfd_malloc relocs.
2025-01-20 07:13:46 +10:30
Alan Modra
cb6326b5ce sec->alloced and freeing section contents
This modifies _bfd_elf_free_cached_info to unmap/free section
contents.  To do that we need to *not* free sections where contents
are bfd_alloc'd or point to constant strings or somesuch.  I've chosen
to implement this be adding another flag to struct bfd_section,
"alloced" to say the section contents can't be freed.  Most of the
patch is about setting that flag in many places.
2025-01-20 07:13:46 +10:30
Alan Modra
354ed4d4a2 _bfd_elf_munmap_section_contents
Do unmap/free cached contents to avoid some memory leaks we'd
otherwise see.

	* elf.c (_bfd_elf_munmap_section_contents): Clear pointers to
	contents that we unmap/free rather than not unmapping/freeing.
2025-01-20 07:13:46 +10:30
Alan Modra
bb9ea706c5 Tidy elf_mmap_section_contents
It is simpler to clear the buffer pointer in the caller than pass
a param that controls clearing.

	* elf.c (elf_mmap_section_contents): Remove final_link param.
	(_bfd_elf_mmap_section_contents): Instead set *buf to NULL here.
	(_bfd_elf_link_mmap_section_contents): Adjust.
2025-01-16 09:31:07 +10:30
Alan Modra
049467c758 Free symtab_hdr.contents and a cache_size correction
symtab_hdr.contents looks to be malloc'd memory, except in one case.
Change that one case to also be malloc'd and free when we are done.

	* elf.c (swap_out_syms): bfd_malloc outbound_syms.
	(_bfd_elf_free_cached_info): Free symtab_hdr.contents.
	* elflink.c (init_reloc_cookie): Correct cache_size.  locsyms
	is an array of Elf_Internal_Sym.
2025-01-15 22:37:16 +10:30
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