This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] RISC-V: two patches used to improve the relocation error message


Dear binutils,

The issue is reported originally at
http://51.15.138.76/patch/22958/
https://sourceware.org/ml/binutils/2019-09/msg00286.html

There are two patches used to improve the current mechanism for
reporting the relocation error.  Also, these two patches were tested
with 32/64-bit elf/linux cross binutils build and checks:

[PATCH 1/2] RISC-V: Report unresolved relocation error via linker's
callback function.
[PATCH 2/2] RISC-V: Improve the error message when linking non-pic
code into shared library.

It is better to use the linker's callback functions to handle the link
time error when relocating.  The unresolved relocation error can be
regarded as an unsupported relocation.  To make user easier to
understand different errors, we need to extend the current error
message format of the callback function since the format is fixed.
The first patch try to resolved the fixed format problem, use asprintf
to extend the error message if needed, and then store the result into
the `msg_buf`.  Finally, remember to free the unused `msg_buf`.

Furthermore, we can improve the error message for one of the resolved
relocations that linking non-pic code into shared library.
R_RISCV_CALL, R_RISCV_JAL and R_RISCV_RVC_JUMP are pc-relative
relocation.  For now, we do not allow the object with these relocation
links into a shared library since the referenced symbols may be loaded
to the places that are too far from the pc.  We can improve the error
message for these unsupported relocation to notice user that they
should recompile their code with `fPIC`.


* The example from the above link (http://51.15.138.76/patch/22958/)
can reproduce the unresolved relocation problem:

rohan:2030$ cat libtmp.c
extern int sub2 (int);
int sub (int i) { return sub2 (i + 10); }
rohan:2031$ cat libtmp2.c
extern int sub (int);
int sub2 (int i) { return sub (i + 10); }
rohan:2032$ riscv64-unknown-linux-gnu-gcc --shared -o libtmp.so libtmp.c
rohan:2033$ riscv64-unknown-linux-gnu-gcc --shared -o libtmp2.so
libtmp2.c libtmp.so
...
/home/jimw/FOSS/BINUTILS/X-riscv64-linux/ld/ld-new:
libtmp2.o(.text+0x18): unresolvable R_RISCV_CALL relocation against
symbol `sub'
/home/jimw/FOSS/BINUTILS/X-riscv64-linux/ld/ld-new: final link failed: bad value
rohan:2060$ echo $?
1
rohan:2061$ ls -lt libtmp2.so
ls: cannot access 'libtmp2.so': No such file or directory

* After attaching the first patch, I get the error message as follows:

/home/nelson/toolchains/build-rv64gc-linux/build-install/lib/gcc/riscv64-unknown-linux-gnu/9.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld:
unresolvable R_RISCV_CALL relocation against symbol `sub'
collect2: error: ld returned 1 exit status
nelson@LAPTOP-QFSGI1F2:~/test/error$ echo $?
1

* And the improved error message for the case that linking non-pic
code into shared library:

/home/nelson/toolchains/build-rv64gc-linux/build-install/lib/gcc/riscv64-unknown-linux-gnu/9.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld:
relocation R_RISCV_CALL against `sub' can not be used when making a
shared object; recompile with -fPIC
collect2: error: ld returned 1 exit status
nelson@LAPTOP-QFSGI1F2:$ echo $?
1

Thanks
Best Regards
Nelson

Attachment: 0001-RISC-V-Report-unresolved-relocation-error-via-linker.patch
Description: Binary data

Attachment: 0002-RISC-V-Improve-the-error-message-when-linking-non-pi.patch
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]