BFD: Fix undefined shift behavior in m32r_elf_do_10_pcrel_reloc
Corrects a shift operation in the M32R ELF relocation handler to avoid undefined behavior.
This commit resolves an issue in the BFD library’s M32R ELF relocation handling where a left shift of a negative value was being performed, leading to undefined behavior according to the C99 standard. The fix replaces the use of bfd_signed_vma with bfd_vma for the relocation calculation, ensuring that the shift operation is performed on an unsigned type, thus preventing the undefined behavior and promoting more robust relocation processing.
In Details
In bfd/elf32-m32r.c, the m32r_elf_do_10_pcrel_reloc function calculated a PC-relative relocation value. The original implementation used bfd_signed_vma and performed operations that could result in a left shift of a negative value, which is undefined behavior per ISO C99. This commit changes relocation to be of type bfd_vma (unsigned) and adjusts the overflow check condition to relocation + 0x200 > 0x3ff, correctly handling the unsigned value and avoiding the UB. The limited bit-field nature of the relocation field means signed right shifts are unnecessary and rely on implementati…
- BFD
- Binary File Descriptor. A library within GNU Binutils that provides a common interface for accessing and manipulating various executable file formats.
- M32R
- A 32-bit microcontroller architecture developed by Renesas Electronics.
- PC-relative relocation
- A type of relocation where the target address is calculated relative to the current program counter (PC). This is common for position-independent code.
- Undefined Behavior (UB)
- Behavior that results from an erroneous program condition for which the C and C++ standards impose no requirements. Compilers may take any action, including returning an incorrect result, or crashing.
- ISO C99
- The 1999 revision of the ISO C programming language standard, which introduced new features and clarified existing behaviors.
- bfd_vma
- A data type in the BFD library representing a virtual memory address, typically an unsigned integer type suitable for holding addresses.
- bfd_signed_vma
- A data type in the BFD library representing a signed virtual memory address, used when arithmetic might involve negative values.