assemblyriscvgnu-assemblermpu

In the RISC V Assembler, what does the ".weak" assembler directive do?


I'm learning RISC V Assembly language, and I have been going through some code (I like to see practical examples of how others do their coding). I found an odd Assembler directive in the startup code for the CH32003V (a new RISC V MPU). So far I have searched through a few materials and found nothing about the ".weak" directive. Below is a fragment of the code where I found it. Any assistance is appreciated. ;-)

    .section  .init, "ax", @progbits
    .globl  _start
    .align  2
_start:
    .option   norvc;
    j       handle_reset
    .word   0
    .word   NMI_Handler                  /* NMI Handler */
    .word   HardFault_Handler            /* Hard Fault Handler */
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   0
    .word   SysTick_Handler             /* SysTick Handler */
    .word   0
    .word   SW_Handler                  /* SW Handler */
    .word   0
    /* External Interrupts */
    .word   WWDG_IRQHandler             /* Window Watchdog */
    .word   PVD_IRQHandler              /* PVD through EXTI Line detect */
    .word   FLASH_IRQHandler            /* Flash */
    .word   RCC_IRQHandler              /* RCC */
    .word   EXTI7_0_IRQHandler          /* EXTI Line 7..0 */
    .word   AWU_IRQHandler              /* AWU */
    .word   DMA1_Channel1_IRQHandler    /* DMA1 Channel 1 */
    .word   DMA1_Channel2_IRQHandler    /* DMA1 Channel 2 */
    .word   DMA1_Channel3_IRQHandler    /* DMA1 Channel 3 */
    .word   DMA1_Channel4_IRQHandler    /* DMA1 Channel 4 */
    .word   DMA1_Channel5_IRQHandler    /* DMA1 Channel 5 */
    .word   DMA1_Channel6_IRQHandler    /* DMA1 Channel 6 */
    .word   DMA1_Channel7_IRQHandler    /* DMA1 Channel 7 */
    .word   ADC1_IRQHandler             /* ADC1 */
    .word   I2C1_EV_IRQHandler          /* I2C1 Event */
    .word   I2C1_ER_IRQHandler          /* I2C1 Error */
    .word   USART1_IRQHandler           /* USART1 */
    .word   SPI1_IRQHandler             /* SPI1 */
    .word   TIM1_BRK_IRQHandler         /* TIM1 Break */
    .word   TIM1_UP_IRQHandler          /* TIM1 Update */
    .word   TIM1_TRG_COM_IRQHandler     /* TIM1 Trigger and Commutation */
    .word   TIM1_CC_IRQHandler          /* TIM1 Capture Compare */
    .word   TIM2_IRQHandler             /* TIM2 */

    .option rvc;
    .section  .text.vector_handler, "ax", @progbits
    .weak   NMI_Handler
    .weak   HardFault_Handler
    .weak   SysTick_Handler
    .weak   SW_Handler
    .weak   WWDG_IRQHandler
    .weak   PVD_IRQHandler
    .weak   FLASH_IRQHandler 


    .weak   RCC_IRQHandler

Solution

  • I assume you're using the GCC assemblers. The .weak directive is the same as for other processors, like ARM. These symbols (which obviously point to handler functions) are used in your code but defined elsewhere. They should be solved at link time. You can also check the related .symdepend directive.

    The manual: https://sourceware.org/binutils/docs/as/Weak.html