On 2025-12-10 11:55, Andrew Cooper wrote:
On 09/12/2025 9:47 pm, Jason Andryuk wrote:
. = ALIGN(4);
__alt_instructions = .;
- *(.altinstructions)
+ KEEP(*(.altinstructions))
__alt_instructions_end = .;
Thinking about this, what we need is for there to be a reference tied to
the source location, referencing the replacement metadata and
replacement instructions.
Looking at https://maskray.me/blog/2021-02-28-linker-garbage-collection
might be able to do this with .reloc of type none. In fact,
BFD_RELOC_NONE seems to have been invented for precisely this purpose.
This means that if and only if the source function gets included, so
does the metadata and replacement.
With Jan's -Wa,--sectname-subst changes added to CFLAGS, this seems to
work somewhat. I'm trying to make the ALTERNATIVE place relocations
against the .altinstructions.%%S and .altinstr_replacement sections:
diff --git c/xen/arch/x86/include/asm/alternative.h
w/xen/arch/x86/include/asm/alternative.h
index 18109e3dc5..e871bfc04c 100644
--- c/xen/arch/x86/include/asm/alternative.h
+++ w/xen/arch/x86/include/asm/alternative.h
@@ -90,18 +90,25 @@ extern void alternative_instructions(void);
/* alternative assembly primitive: */
#define ALTERNATIVE(oldinstr, newinstr, feature) \
OLDINSTR_1(oldinstr, 1) \
- ".pushsection .altinstructions, \"a\", @progbits\n" \
+ ".reloc ., BFD_RELOC_NONE, 567f\n" \
+ ".reloc ., BFD_RELOC_NONE, 568f\n" \
+ ".pushsection .altinstructions.%%S, \"a\", @progbits\n" \
+ "567:\n" \
ALTINSTR_ENTRY(feature, 1) \
".section .discard, \"a\", @progbits\n" \
".byte " alt_total_len "\n" /* total_len <= 255 */ \
DISCARD_ENTRY(1) \
".section .altinstr_replacement, \"ax\", @progbits\n" \
+ "568:\n" \
ALTINSTR_REPLACEMENT(newinstr, 1) \
".popsection\n"
--print-gc-sections shows a few .altinstructions.%%S dropped - as an
example:
ld: removing unused section '.text.reserve_lapic_nmi' in file 'prelink.o'
ld: removing unused section '.text.release_lapic_nmi' in file 'prelink.o'
...
ld: removing unused section '.altinstructions..text.reserve_lapic_nmi'
in file 'prelink.o'
ld: removing unused section '.altinstructions..text.release_lapic_nmi'
in file 'prelink.o'
The full list is below.
Unfortunately, EFI doesn't like it with ~14000 lines of:
ld: error: 0-bit reloc in dll
Also, my test of removing the path to memory_add() still doesn't drop
memory_add().
There is still something wrong where I get a fault in some shadow code.
I'm still investigating that.
Regards,
Jason
ld: removing unused section '.text.__bitmap_full' in file 'prelink.o'
ld: removing unused section '.text.__bitmap_xor' in file 'prelink.o'
ld: removing unused section '.text.__bitmap_set' in file 'prelink.o'
ld: removing unused section '.text.__bitmap_clear' in file 'prelink.o'
ld: removing unused section '.text.bitmap_find_free_region' in file
'prelink.o'
ld: removing unused section '.text.bitmap_release_region' in file
'prelink.o'
ld: removing unused section '.text.safe_copy_string_from_guest' in file
'prelink.o'
ld: removing unused section '.text.domain_has_ioreq_server' in file
'prelink.o'
ld: removing unused section '.text.compat_kexec_op' in file 'prelink.o'
ld: removing unused section '.text.in_atomic' in file 'prelink.o'
ld: removing unused section '.text.radix_tree_next_hole' in file 'prelink.o'
ld: removing unused section '.text.radix_tree_prev_hole' in file 'prelink.o'
ld: removing unused section '.text.radix_tree_gang_lookup_slot' in file
'prelink.o'
ld: removing unused section '.text._nrspin_trylock' in file 'prelink.o'
ld: removing unused section '.text.xen_compile_host' in file 'prelink.o'
ld: removing unused section '.text.vm_event_cancel_slot' in file 'prelink.o'
ld: removing unused section '.text.vscnprintf' in file 'prelink.o'
ld: removing unused section '.text.wake_up_one' in file 'prelink.o'
ld: removing unused section '.text.xmem_pool_get_used_size' in file
'prelink.o'
ld: removing unused section '.text.xmem_pool_get_total_size' in file
'prelink.o'
ld: removing unused section '.text.xmem_pool_destroy' in file 'prelink.o'
ld: removing unused section '.text.xmem_pool_maxalloc' in file 'prelink.o'
ld: removing unused section '.text.xlat_start_info' in file 'prelink.o'
ld: removing unused section '.text.elf_sym_by_name' in file 'prelink.o'
ld: removing unused section '.text.elf_sym_by_index' in file 'prelink.o'
ld: removing unused section '.text.elf_get_ptr' in file 'prelink.o'
ld: removing unused section '.text.elf_lookup_addr' in file 'prelink.o'
ld: removing unused section '.text.serial_vuart_info' in file 'prelink.o'
ld: removing unused section '.text.pci_find_next_cap' in file 'prelink.o'
ld: removing unused section '.text.free_hvm_irq_dpci' in file 'prelink.o'
ld: removing unused section '.text.iommu_has_feature' in file 'prelink.o'
ld: removing unused section '.text.__erst_get_next_record_id' in file
'prelink.o'
ld: removing unused section '.text.__erst_read' in file 'prelink.o'
ld: removing unused section '.text.erst_get_record_count' in file
'prelink.o'
ld: removing unused section '.text.erst_get_next_record_id' in file
'prelink.o'
ld: removing unused section '.text.erst_read' in file 'prelink.o'
ld: removing unused section '.text.erst_read_next' in file 'prelink.o'
ld: removing unused section '.text.erst_clear' in file 'prelink.o'
ld: removing unused section '.text.mce_barrier_init' in file 'prelink.o'
ld: removing unused section '.text.mce_barrier_dec' in file 'prelink.o'
ld: removing unused section '.text.mce_barrier' in file 'prelink.o'
ld: removing unused section '.text.apei_read_mce' in file 'prelink.o'
ld: removing unused section '.text.apei_check_mce' in file 'prelink.o'
ld: removing unused section '.text.apei_clear_mce' in file 'prelink.o'
ld: removing unused section '.text.efi_halt_system' in file 'prelink.o'
ld: removing unused section '.text.get_vvmcs_virtual_safe' in file
'prelink.o'
ld: removing unused section '.text.get_vvmcs_real_safe' in file 'prelink.o'
ld: removing unused section '.text.set_vvmcs_real' in file 'prelink.o'
ld: removing unused section '.text.set_vvmcs_virtual_safe' in file
'prelink.o'
ld: removing unused section '.text.set_vvmcs_real_safe' in file 'prelink.o'
ld: removing unused section '.init.text.early_page_fault' in file
'prelink.o'
ld: removing unused section '.text.domain_set_alloc_bitsize' in file
'prelink.o'
ld: removing unused section '.text.reserve_lapic_nmi' in file 'prelink.o'
ld: removing unused section '.text.release_lapic_nmi' in file 'prelink.o'
ld: removing unused section '.text.watchdog_enabled' in file 'prelink.o'
ld: removing unused section '.text.unset_nmi_callback' in file 'prelink.o'
ld: removing unused section '.text.sha2_256_init' in file 'prelink.o'
ld: removing unused section '.text.xxh64_copy_state' in file 'prelink.o'
ld: removing unused section '.text.xxh64' in file 'prelink.o'
ld: removing unused section '.discard' in file 'prelink.o'
ld: removing unused section
'.altinstructions..text.safe_copy_string_from_guest' in file 'prelink.o'
ld: removing unused section '.rodata.xen_compile_host.str1.1' in file
'prelink.o'
ld: removing unused section
'.altinstructions..text.vm_event_cancel_slot' in file 'prelink.o'
ld: removing unused section '.rodata.xmem_pool_destroy.str1.8' in file
'prelink.o'
ld: removing unused section '.altinstructions..text.xmem_pool_destroy'
in file 'prelink.o'
ld: removing unused section '.rodata.elf_lookup_addr.str1.1' in file
'prelink.o'
ld: removing unused section '.altinstructions..text.iommu_has_feature'
in file 'prelink.o'
ld: removing unused section '.rodata.__erst_read.str1.8' in file 'prelink.o'
ld: removing unused section
'.altinstructions..text.erst_get_record_count' in file 'prelink.o'
ld: removing unused section
'.altinstructions..text.erst_get_next_record_id' in file 'prelink.o'
ld: removing unused section '.altinstructions..text.erst_read' in file
'prelink.o'
ld: removing unused section '.altinstructions..text.erst_read_next' in
file 'prelink.o'
ld: removing unused section '.altinstructions..text.erst_clear' in file
'prelink.o'
ld: removing unused section '.rodata.apei_read_mce.str1.8' in file
'prelink.o'
ld: removing unused section '.rodata.efi_halt_system.str1.8' in file
'prelink.o'
ld: removing unused section '.rodata.play_dead.str1.1' in file 'prelink.o'
ld: removing unused section '.altinstructions..text.reserve_lapic_nmi'
in file 'prelink.o'
ld: removing unused section '.altinstructions..text.release_lapic_nmi'
in file 'prelink.o'
ld: removing unused section '.data.rel.ro.local.fetch_type_names' in
file 'prelink.o'
ld: removing unused section '.data.lapic_nmi_owner_lock' in file 'prelink.o'