snikeguo opened a new issue, #17040:
URL: https://github.com/apache/nuttx/issues/17040

   ### Description
   
   I encountered an issue with the ARM-none-eabi GCC toolchain built from the 
[xpack-dev-tools](https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack) 
repository, where modifying the configuration from --disable-tls to 
--enable-tls resulted in incorrect assembly generation violating AAPCS register 
preservation rules.
   
   In the following code snippet, the compiler generates assembly that fails to 
preserve the caller-saved register R3 before calling __aeabi_read_tp, which 
internally modifies R3. This leads to incorrect post-call arithmetic and memory 
corruption:
   ```
   inline Thread * ThreadStore::RawGetCurrentThread()
   {
       Thread* th= (Thread *) &tls_CurrentThread;
        C0017D7A   LDR            R3, =0x001A5528               ; [PC, #24] 
[0xC0017D94] 
        C0017D7C   ADD            R3, PC
        C0017D7E   LDR            R3, [R3]  ; R3 now holds TLS offset (must be 
preserved!)
        C0017D80   BL             __aeabi_read_tp               ; 0xC009F40C
        C0017D84   MOV            R2, R0
        C0017D86   ADD            R3, R2  ; R3 += R2 (uses corrupted R3 value!)
        C0017D88   STR            R3, [R7, #4]
       return th;
   }
   ```
   Issue:​​
   
   AAPCS mandates that R0-R3 are ​caller-saved. The compiler must save R3 
before BL __aeabi_read_tp if its value is needed post-call.
   __aeabi_read_tp legally modifies R3, breaking the subsequent ADD R3, R2 
operation.
   This results in calculating an incorrect address for the TLS variable, 
causing memory access errors.
   
   gcc version:
   ```
   .\arm-none-eabi-gcc -v
   Using built-in specs.
   
COLLECT_GCC=D:\Soft\ArmGcc\xpack-arm-none-eabi-gcc-13.3.1-1.1\bin\arm-none-eabi-gcc.exe
   
COLLECT_LTO_WRAPPER=D:/Soft/ArmGcc/xpack-arm-none-eabi-gcc-13.3.1-1.1/bin/../libexec/gcc/arm-none-eabi/13.3.1/lto-wrapper.exe
   Target: arm-none-eabi
   Configured with: 
/home/david/projects/xbb_work/arm-none-eabi-gcc-xpack-13.3.1-1.1/build-assets/build/win32-x64/sources/gcc/configure
 
--prefix=/home/david/projects/xbb_work/arm-none-eabi-gcc-xpack-13.3.1-1.1/build-assets/build/win32-x64/application
 
--with-sysroot=/home/david/projects/xbb_work/arm-none-eabi-gcc-xpack-13.3.1-1.1/build-assets/build/win32-x64/application/arm-none-eabi
 --with-native-system-header-dir=/include 
--infodir=/home/david/projects/xbb_work/arm-none-eabi-gcc-xpack-13.3.1-1.1/build-assets/build/win32-x64/x86_64-w64-mingw32/install/share/info
 
--mandir=/home/david/projects/xbb_work/arm-none-eabi-gcc-xpack-13.3.1-1.1/build-assets/build/win32-x64/x86_64-w64-mingw32/install/share/man
 
--htmldir=/home/david/projects/xbb_work/arm-none-eabi-gcc-xpack-13.3.1-1.1/build-assets/build/win32-x64/x86_64-w64-mingw32/install/share/html
 
--pdfdir=/home/david/projects/xbb_work/arm-none-eabi-gcc-xpack-13.3.1-1.1/build-assets/build/win32-x64/x86_64-w64-mingw32/install/share/pdf
 --build
 =x86_64-pc-linux-gnu --host=x86_64-w64-mingw32 --target=arm-none-eabi 
--disable-nls --disable-shared --disable-threads --enable-tls 
--enable-checking=release --enable-languages=c,c++ --enable-mingw-wildcard 
--with-gmp=/home/david/projects/xbb_work/arm-none-eabi-gcc-xpack-13.3.1-1.1/build-assets/build/win32-x64/x86_64-w64-mingw32/install
 --with-newlib --with-pkgversion='xPack GNU Arm Embedded GCC x86_64' 
--with-gnu-as --with-gnu-ld --with-system-zlib --with-headers=yes 
--enable-multilib --with-multilib-list=aprofile,rmprofile
   Thread model: single
   Supported LTO compression algorithms: zlib zstd
   gcc version 13.3.1 20240614 (xPack GNU Arm Embedded GCC x86_64)
   ```
    **--disable-threads --enable-tls**
   
   ### Verification
   
   - [x] I have verified before submitting the report.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to