Code in .subsection 0 should be prior to .subsection 1

Georg-Johann Lay avr@gjlay.de
Mon Nov 24 10:02:32 GMT 2025


So I have problems grasping why code in .subsection 1
is located PRIOR to code in .subsection 0:

;; init.S
.section .init2,"ax",@progbits
.subsection 0
.type __init_sp,@function
.global __init_sp
__init_sp:
     ldi r22, 0

;; main.S
.section .init2,"ax",@progbits
.subsection 1
     ldi r22, 1
.global __init_sp

So the expected disassembly is

ldi r22, 0  ; .subsection 0
ldi r22, 1  ; .subsection 1

But when I run

avr-ld --version | head -1
avr-as main.S -o main.o

avr-as init.S -o init.o
avr-ar cru libinit.a init.o
avr-ranlib libinit.a

avr-ld main.o -o main.elf -L. -linit
avr-objdump -d main.elf

The output is:

GNU ld (GNU Binutils) 2.45.50.20250718

main.elf:     file format elf32-avr


Disassembly of section .text:

00000000 <__ctors_end>:
    0:	61 e0       	ldi	r22, 0x01	; 1

00000002 <__init_sp>:
    2:	60 e0       	ldi	r22, 0x00	; 0

The .init2 section is part of text and mentioned in the
default ld script:

   .text   :
   {
     ....
     *(.init2)  /* Clear __zero_reg__, set up stack pointer.  */
     KEEP (*(.init2))
     ....
   } > text

What am I doing / understanding wrong?
Is this a bug?

Johann



More information about the Binutils mailing list