Commit Graph

1064 Commits

Author SHA1 Message Date
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
Jan Beulich
d41df13ab3 bfd/ELF: slightly "better" file alignment for object files
PR gas/32435

Commit 1f1b5e506b ("bfd/ELF: restrict file alignment for object
files") caused an issue in the Linux kernels modpost utility, which was
building upon .rodata sections to be 4-byte aligned in the file when
they have 4-byte alignment. While we don't want to revert back to
original behavior, apply the same alignment "capping" as done originally
in two other places also for "ordinary" sections.
2025-01-13 13:32:19 +01:00
Alan Modra
e8e7cf2abe Update year range in copyright notice of binutils files 2025-01-01 18:29:57 +10:30
Jan Beulich
d5827a0e83 bfd/ELF: refine segment index in filepos assignment diag
Reporting an internal loop index isn't helpful for the user to determine
which segment the problem is with. Report the PHDR index instead.
2024-12-27 11:37:42 +01: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
Alan Modra
aa7a348b2f Remove bfd_elf_allocate_object object_id param
This is another case where the proper object_id can be read from
elf_backend_data.
2024-12-19 08:12:07 +10:30
Alan Modra
5ba2d7a0a3 Delete asection.symbol_ptr_ptr
This field is always set to point to asection.symbol, and no code ever
changes it from its initial value.  With one exception.  elfxx-mips.c
creates two sections with separate pointers to their symbols, and uses
those as asection.symbol_ptr_ptr.  Those pointers aren't modified,
so they disappear in this patch too.
2024-12-14 19:19:32 +10:30
Jan Beulich
af3394d97a ELF: SHF_STRINGS isn't really tied to SHF_MERGE
It's not overly useful without it, but the spec doesn't name any
dependency between the two. People may want to use it for purely
informational purposes, for example. Adjust, in particular, entity size
processing to be engaged if either flag is set, as mandated by the spec.
2024-11-18 11:36:57 +01:00
Alan Modra
76eab8f47a PR32266, segv when linking libclang_rt.asan-powerpc64.so
Change the mmap support added with commit 9ba56acee5 to always mmap
memory with PROT_READ | PROT_WRITE.  Prior to that commit most file
contents were read into a buffer allocated with bfd_alloc or
bfd_malloc and thus the memory was read/write.  Even after that commit
any section contents with relocations must be read/write to apply the
relocs.  Making them all read/write is not a major change, and it
should not introduce any measurable linker slowdown for contents that
are not modified.  More importantly, it removes a BFD behaviour
difference that only triggers when large files are involved.

	PR 32266
	PR 32109
	* libbfd.c (bfd_mmap_local): Remove prot param.  Always mmap
	with PROT_READ | PROT_WRITE.  Adjust all calls.
	(_bfd_mmap_temporary): Rename from _bfd_mmap_readonly_temporary.
	(_bfd_munmap_temporary): Rename from _bfd_munmap_readonly_temporary.
	_bfd_mmap_persistent): Rename from _bfd_mmap_readonly_persistent.
	(_bfd_generic_get_section_contents): Use PROT_READ | PROT_WRITE
	regardless of relocs.
	* libbfd-in.h: Update decls to suit.  Make non-USE_MMAP variants
	static inline functions.
	* elflink.c: Update all uses of _bfd_mmap functions.
	* elf.c: Likewise.
	(bfd_elf_get_str_section): Revert commit 656f8fbaae.
	* libbfd.h: Regenerate.
2024-10-16 14:23:27 +10:30
Jan Beulich
29ed50151a bfd/ELF: drop align_file_position()
Switch the sole user to BFD_ALIGN() instead. (It's comment was partly
wrong [stale?] anyway, talking of some maximum that was nowhere in
sight.)
2024-10-11 08:20:06 +02: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
e5375abb67 Revert "bfd_elf_sym_name_raw"
This reverts commit 265757dc6e.
2024-10-08 15:12:14 +10:30
Alan Modra
9b09ceea7d Revert "get_synthetic_symtab fixes for commit 68bbe1183379"
This reverts commit 0c13ac533e.
2024-10-08 15:12:14 +10:30