https://sourceware.org/bugzilla/show_bug.cgi?id=33431

            Bug ID: 33431
           Summary: Section ALIGN does not work with MEMORY
           Product: binutils
           Version: 2.45
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: ld
          Assignee: unassigned at sourceware dot org
          Reporter: davidegrayson at gmail dot com
  Target Milestone: ---

When your linker script defines memories, and there's a memory that does not
start at 0 and its first non-empty section has an ALIGN attribute immediately
after the section name, the GNU Linker malfunctions and gives error messages
that indicate it probably ignored the memory specification and just added the
section right after the last section.

Furthermore, if you work around the bug by putting a dummy non-empty section at
the beginning of the memory region, the ALIGN attribute appears to align the
section, but it has no effect on the sh_addralign member in the section header
of the ELF output.

I know I can just align things using commands like ". = ALIGN(4);" inside the
sections, so this isn't a big issue.  And also the sh_addralign member probably
isn't used for anything after the final linking.  However, I think both of
these things are still bugs.

I tested with two target architectures: RISCV (32-bit) and AVR.  I used
Binutils 2.45 compiled from source in MSYS2's UCRT64 environment.

To reproduce the problem, you can use these files:

test.ld
====
MEMORY {
  ROM (r): ORIGIN = 0x1000, LENGTH = 4K
  RAM (r): ORIGIN = 0x2000, LENGTH = 4K
}
ENTRY(_start)
SECTIONS {
  .text : {
    *(.text)
  } > ROM
  #.workaround : { _workaround = .; } >RAM
  .data ALIGN(4) : {
    __idata_start = .;
    *(.data)
    __idata_end = .;
  } >RAM
  .bss : {
    __bss_start = .;
    *(.bss)
    __bss_end = .;
  } >RAM
}


test.asm
====
    .text
    .global _start
    .type _start, @function
_start:
    ret
    .word _var0
    .word _var1
    .bss
_var0:
    .byte 0
    .data
_var1:
    .byte 1


build.sh
===
#!/usr/bin/env bash
set -ue
riscv64-unknown-elf-as -march=rv32im_zicsr_zifencei -mabi=ilp32 test.asm -o
riscv.o
riscv64-unknown-elf-ld -melf32lriscv --relax -T test.ld riscv.o -o riscv.elf ||
echo FAILED
avr-as -mavr5 test.asm -o avr.o
avr-ld -mavr5 --relax -T test.ld avr.o -o avr.elf || echo FAILED

====

This results in the following error messages (EXE path removed):

riscv64-unknown-elf-ld.exe: address 0x100d of riscv.elf section `.data' is not
within region `RAM'
riscv64-unknown-elf-ld.exe: riscv.elf section `.bss' will not fit in region
`RAM'
riscv64-unknown-elf-ld.exe: address 0x100d of riscv.elf section `.data' is not
within region `RAM'
riscv64-unknown-elf-ld.exe: region `RAM' overflowed by 4294959118 bytes
FAILED
avr-ld.exe: address 0x1009 of avr.elf section `.data' is not within region
`RAM'
avr-ld.exe: avr.elf section `.bss' will not fit in region `RAM'
avr-ld.exe: address 0x1009 of avr.elf section `.data' is not within region
`RAM'
avr-ld.exe: region `RAM' overflowed by 4294959114 bytes
FAILED

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to