compilationembeddedldgnu

What is the Effect of EXTERN command in ld linker script


In the ld linker manual section 3.4.5 version 2.37 , the manual says about the EXTERN Command : "force symbol to be entered in the output file as an undefined symbol. ...etc", I understand the symbol is always kept in the output file , But My question now : is it necessarily kept undefined even if its defined in any file , or just forced to be kept only keeping its state being defined or not defined ? that is confusing for me , Second issue after some research I could get that is used with interrupt tables to keep symbols about the service routines , but I don't get how that actually works with interrupts , so kindly I need some example to help me understand this also

Thanks


Solution

  • Primarily to ensure that certain symbols are included during linking, especially when:

    Example: Interrupt Vector Table

    Suppose your interrupt vector table is defined in C as:

    void (* const interrupt_vector_table[])() __attribute__((section(".vectors"))) = {
        Reset_Handler,
        Timer_Handler,
        UART_Handler
    };
    

    But these handlers may not be referenced directly anywhere in your code. The linker might discard them unless you do:

    EXTERN(Reset_Handler Timer_Handler UART_Handler)

    This tells the linker to treat those symbols as used so it keeps them during garbage collection and ensures the function addresses are properly placed in the vector table.