ldlinker-scripts

How to fail on non-empty sections in a GNU ld linker script?


I have this in my GNU ld linker script:

  .ctors : ALIGN(4) SUBALIGN(4) {
    *(.ctors)
    ASSERT(0, "Constructors (.ctors) are not supported.");
  }

In earlier versions of GNU ld (such as 2.24), this triggered the assertion only if the section .ctors was nonempty. In newer version, it always triggers the assertion. How do I trigger it only if .ctors is non-empty?


Solution

  • In this example , the __ctors_start variable should be defined prior to being used in the section's definition.

    .ctors : ALIGN(4) SUBALIGN(4) {
        *(.ctors)
        __ctors_end = .;
        __ctors_size = __ctors_end - __ctors_start;
        __ctors_start = .;
        ASSERT(__ctors_size == 0, "Constructors (.ctors) are not supported.");
    }
    

    This is an updated version of the linker script that properly defines __ctors_start before utilizing it in the section definition

    .ctors : ALIGN(4) SUBALIGN(4) {
        __ctors_start = .;
        *(.ctors)
        __ctors_end = .;
        __ctors_size = __ctors_end - __ctors_start;
        ASSERT(__ctors_size == 0, "Constructors (.ctors) are not supported.");
    }
    

    good luck !