Gary-Hobson commented on code in PR #11448:
URL: https://github.com/apache/nuttx/pull/11448#discussion_r1440600860


##########
libs/libc/symtab/symtab_findbyvalue.c:
##########
@@ -92,7 +92,7 @@ symtab_findbyvalue(FAR const struct symtab_s *symtab,
         }
       else if (symtab[mid].sym_value < value)
         {
-          if (symtab[mid + 1].sym_value >= value)
+          if (symtab[mid + 1].sym_value > value)

Review Comment:
   In arm, the lowest bit of the instruction is 1, which is a thumb 
instruction, and 0, which is an arm instruction.
   The nm command was used in mkallsym.sh before, and the result it will return 
will set the lowest bit of the thumb instruction to 0. There will be a one-byte 
deviation during binary search, so mkallsyms.py will also set the lowest bit to 
0 according to the previous format.
   ```sh
   arm-none-eabi-nm -Cn nuttx | grep hello
   0801c384 T hello_main
   arm-none-eabi-objdump nuttx -t |grep hello
   0801c384 g F .text 0000004c hello_main
   arm-none-eabi-readelf nuttx -s |grep hello
   4558: 0801c385 76 FUNC GLOBAL DEFAULT 1 hello_main
   ```
   
   However, in the following case, when you need to find the function address 
according to the symbol name and execute the corresponding function, the lowest 
address obtained is 0. It will follow the arm instruction, causing an exception.
   ```c
   void sym_test(void)
   {
     printf("call sym_test\n");
   }
   
   int main(int argc, FAR char *argv[])
   {
     size_t size;
     void (*func)(void);
     const struct symtab_s *sym;
     void *addr = sym_test;
   
     printf("sym_test:%p %pS\n",addr, addr);
     printf("sym_test - 1: %pS\n", (char *)addr - 1);
     printf("sym_test + 1: %pS\n", (char *)addr + 1);
   
     sym = allsyms_findbyname("sym_test", &size);
     printf("sym_test:%p %pS\n",sym, sym);
     func = sym->sym_value;
     func();
   
     return 0;
   }
   ```
   
   Therefore, you need to change mkallsyms.py back to the correct result and 
correct the binary search.
   
   Now the algorithm can be used with thumb instructions and non-thumb 
instructions (or other arch), for the above code, I get the correct results on 
both stm32 and sim



-- 
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