[riscv-sw] Relocation problem in RISC-V GNU Linker

171 views
Skip to first unread message

Chang, Abner

unread,
Dec 30, 2015, 9:40:52 PM12/30/15
to isa...@lists.riscv.org

Hi,

I am working on RISC-V UEFI port recently. I found two RISC-V GNU linker issues which cause platform hangs when UEFI driver is launched.

In elfnn-riscv.c,

1.       When handle R_RISCV_LO12_S in riscv_elf_relocate_section () ,

It uses X0 register as base address if the relative address is < 800h. This causes we lost high 20-bit of relocation.

2.       In _bfd_riscv_relax_lui (),

This removes AUIPC when the relative address is < (RISCV_IMM_REACH/2), this causes relocation failed when launch UEFI image.

I also found there is some update of these two functions on Github. Just want to make sure are those update intended to fix the issues I mentioned above?

Thanks and happy new year!

Abner

 

 

 

Tommy Thorn

unread,
Dec 30, 2015, 10:18:54 PM12/30/15
to Chang, Abner, isa...@lists.riscv.org
Hi Abner,

it might be helpful if you could provide sample files to reproduce the issue.

Tommy

Andrew Waterman

unread,
Dec 31, 2015, 6:36:14 PM12/31/15
to Chang, Abner, isa...@lists.riscv.org
On Wed, Dec 30, 2015 at 6:40 PM, Chang, Abner <abner...@hpe.com> wrote:
> Hi,
>
> I am working on RISC-V UEFI port recently. I found two RISC-V GNU linker
> issues which cause platform hangs when UEFI driver is launched.
>
> In elfnn-riscv.c,
>
> 1. When handle R_RISCV_LO12_S in riscv_elf_relocate_section () ,
>
> It uses X0 register as base address if the relative address is < 800h. This
> causes we lost high 20-bit of relocation.

R_RISCV_LO12_S should only be used for absolute relocations, so
dropping the high part is correct. R_RISCV_PCREL_LO12_S should be
used for pc-relative addressing.

Either use the la macro, use absolute addressing, or hand-write the
auipc sequence as

1: auipc t0, %pcrel_hi(sym)
...
addi t0, t0 %pcrel_lo(1b)

>
> 2. In _bfd_riscv_relax_lui (),
>
> This removes AUIPC when the relative address is < (RISCV_IMM_REACH/2), this
> causes relocation failed when launch UEFI image.

This sounds like the same issue as above (using R_RISCV_HI20 instead
of R_RISCV_PCREL_HI20).

Chang, Abner

unread,
Jan 3, 2016, 3:53:56 AM1/3/16
to Tommy Thorn, isa...@lists.riscv.org

Hi Tommy,

Thanks to your reply. I met this issue 1.5 month ago and fixed this by comment out some code in RISC-V GNU linker.

I will try to roll back my code and provide the sample files. However, I think I have to check the latest RISC-V tool chain on Github (mine is pretty old, it was Aug 2015). This issue probably was already fixed.

Thanks

Abner

Chang, Abner

unread,
Jan 3, 2016, 12:10:55 PM1/3/16
to Andrew Waterman, Tommy Thorn, isa...@lists.riscv.org
Hi Tommy and Andrew,
I just reproduced one of the relocation issues in "_bfd_riscv_relax_lui ()" (This is #2 issue). You can see below piece of my source code.
In FaultTolerantWriteInitialize(),the address of function "FvbNotificationEvent" is passed to EfiCreateProtocolNotifyEvent(). FvbNotificationEvent needs to be relocated when the image is loaded into memory.

VOID
EFIAPI
FvbNotificationEvent (
IN EFI_EVENT Event,
IN VOID *Context
)
{
.....
.....
}

EFI_STATUS
EFIAPI
FaultTolerantWriteInitialize (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
....
....
EfiCreateProtocolNotifyEvent (
&gEfiFirmwareVolumeBlockProtocolGuid,
TPL_CALLBACK,
FvbNotificationEvent, <--- **** Require relocation.
(VOID *)FtwDevice,
&mFvbRegistration
);
....
....
}

However, the relocation entry is deleted by "_bfd_riscv_relax_lui ()" function during the link process. See below ELF reloc dump.

00000000099c 000000000000 R_RISCV_NONE 0
00000000099c 03170000001b R_RISCV_LO12_I 00000000000007b0 FvbNotificationEvent + 0

This causes the relocation problem and results in system hang. I saw some change on this function in "elfnn-riscv.c" on Github. Not sure if the change fixed this issue or not. Will try the latest GNU tool chain later.

Thanks
Abner

-----Original Message-----
From: Andrew Waterman [mailto:and...@sifive.com]
Sent: Friday, January 01, 2016 7:36 AM
To: Chang, Abner
Cc: isa...@lists.riscv.org
Subject: Re: [riscv-sw] Relocation problem in RISC-V GNU Linker

Reply all
Reply to author
Forward
0 new messages