0

I am working on a project using the Kendryte K210 which is a 64-bit duel-core RISC-V machine. I am using the Kendryte GNU toolchain and the starting point was the Kendryte standalone SDK.

I am experiencing some frustrating core faults. The fault, a misaligned load, is happening after a balr to _Balloc in _ldtoa_r, where a ld instruction in _Balloc is attempting to load from an invalid address pointed to by one of the function argument registers (a0 in this case).

I have been trying to figure out when/where/how _Balloc and _ldtoa_r are used, but they are part of libc. My map file shows _ldtoa_r is somehow related to lib_a-ldtoa.o and lib_a-svfprintf.o and _Balloc is somehow related to lib_a-strtod.o and lib_a-mprec.o. I'm not sure because I am new to interpreting map files.

If anyone can help educate me on what _ldtoa_r and _Balloc are, how they are used, their relationship to these object files, and possibly how to properly interpret relevant lines in the map file, then I would be very grateful.

Thank you.

c:/sysgcc/kendryte/bin/../lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib\libc.a(lib_a-strtod.o) (_Balloc)

...
 
c:/sysgcc/kendryte/bin/../lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib\libc.a(lib_a-ldtoa.o)
c:/sysgcc/kendryte/bin/../lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib\libc.a(lib_a-svfprintf.o) (_ldtoa_r)

...

 .text._ldtoa_r
0x0000000080027456      0xa74 
c:/sysgcc/kendryte/bin/../lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib\libc.a(lib_a-ldtoa.o)
0x0000000080027456      _ldtoa_r

...

 .text._Balloc
0x000000008002814c       0x6c
c:/sysgcc/kendryte/bin/../lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib\libc.a(lib_a-mprec.o)
0x000000008002814c       _Balloc

...

 .rodata._ldtoa_r.str1.8
0x000000008003cfb8       0x34 
c:/sysgcc/kendryte/bin/../lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib\libc.a(lib_a-ldtoa.o)
Mark Watson
  • 103
  • 5

1 Answers1

1

Cannyone explain the use of libc functions _ldtoa_r

Is used to convert a long double value to a string, with many customization options, and is reentrant in the Newlib sense takes _REENT state.

It used for example when printing a double value.

https://github.com/bminor/newlib/blob/80cda9bbda04a1e9e3bee5eadf99061ed69ca5fb/newlib/libc/stdlib/dtoa.c

and _Balloc?

Balloc allocate an _Bigint value. _Bigint represents a floating point value as a "big integer".

It is used in various places, mostly when printing and reading a long double values from/to a string.

https://github.com/bminor/newlib/blob/80cda9bbda04a1e9e3bee5eadf99061ed69ca5fb/newlib/libc/stdlib/mprec.c#L97

their relationship to these object files

The compiled code is stored in "these object files".

how to properly interpret relevant lines in the map file

 .text._ldtoa_r
#^^^^^^^^^^^^^^ - section name
0x0000000080027456      0xa74 
#                       ^^^^^ - length of the section
# ^^^^^^^^^^^^^^^ - location of the section in output file
c:/sysgcc/kendryte/bin/../lib/gcc/riscv64-unknown-elf/8.2.0/../../../../riscv64-unknown-elf/lib\libc.a
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - filename
# ..  (lib_a-ldtoa.o)
#      ^^^^^^^^^^^^^  - object filename (used when compiling libc)
0x0000000080027456      _ldtoa_r
#                       ^^^^^^^^ - symbol name
# ^^^^^^^^^^^^^^^^ - location in output file
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanks for your comment. Would these functions be used in stdlib when calling functions like sprintf or strncpy?? Going back to my fault, would it be reasonable to assume the fault is due to an invalid pointer being passed into sprintf, etc. during runtime? – Mark Watson Mar 09 '22 at 16:22
  • 1
    `like sprintf` yes. But there is newlib-nano, and `_printf_float` symbol, it can be disabled. `or strncpy?` no. `would it be reasonable to assume the fault is due to an invalid pointer being passed into sprintf during runtime?` sure. `those are not the official libc libraries, correct?` no, see https://sourceware.org/newlib/ . Github has just nice looking interface and search options. Uhm, there is no universal "libc libraries", just _your specific setup_ uses newlib as the implementation. – KamilCuk Mar 09 '22 at 16:24
  • Here is the mprec.c file used by my [toolchain](https://github.com/kendryte/kendryte-newlib/blob/01de41f55e386c935b593402855622fb7aba25c8/newlib/libc/stdlib/mprec.c). It looks like it's pretty much the same file you provided. – Mark Watson Mar 09 '22 at 16:28
  • For the record, here is the [mprec.c](https://github.com/kendryte/kendryte-newlib/blob/01de41f55e386c935b593402855622fb7aba25c8/newlib/libc/stdlib/mprec.c) file used by my toolchain which defines _Balloc. And here is the [ldtoa.c](https://github.com/kendryte/kendryte-newlib/blob/01de41f55e386c935b593402855622fb7aba25c8/newlib/libc/stdlib/ldtoa.c#L2787) file used by my toolchain which defines _ldtoa_r. – Mark Watson Mar 09 '22 at 16:36