Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 19, 2023
1 parent afe57e9 commit 0702d41
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions elf/arch-riscv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -527,16 +527,23 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
//
// For non-dlopen'd DSO, we may relax the instructions to the following:
//
// <deleted>
// <deleted>
// auipc a0, %gottp_hi(a0)
// l[d|w] a0, %gottp_lo(a0)
//
// For executable, if the TP offset is small enough, we'll relax
// it to the following:
//
// <deleted>
// <deleted>
// <deleted>
// addi a0, zero, %tpoff_lo(a0)
//
// Otherwise, the following sequence is used:
//
// <deleted>
// <deleted>
// lui a0, %tpoff_hi(a0)
// addi a0, a0, %tpoff_lo(a0)
if (removed_bytes == 0)
Expand All @@ -545,6 +552,9 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
case R_RISCV_TLSDESC_LOAD_LO12:
case R_RISCV_TLSDESC_ADD_LO12:
case R_RISCV_TLSDESC_CALL: {
if (removed_bytes == 4)
break;

i64 idx2 = find_paired_reloc();
const ElfRel<E> &rel2 = rels[idx2];
Symbol<E> &sym2 = *file.symbols[rel2.r_sym];
Expand All @@ -555,27 +565,24 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {

switch (rel.r_type) {
case R_RISCV_TLSDESC_LOAD_LO12:
if (sym2.has_tlsdesc(ctx))
write_itype(loc, sym2.get_tlsdesc_addr(ctx) + A - P);
write_itype(loc, sym2.get_tlsdesc_addr(ctx) + A - P);
break;
case R_RISCV_TLSDESC_ADD_LO12:
if (sym2.has_tlsdesc(ctx)) {
write_itype(loc, sym2.get_tlsdesc_addr(ctx) + A - P);
} else if (sym2.has_gottp(ctx)) {
*(ul32 *)loc = 0x517; // auipc a0,<hi20>
*(ul32 *)loc = 0x517; // auipc a0,<hi20>
write_utype(loc, sym2.get_gottp_addr(ctx) + A - P);
} else {
if (removed_bytes == 0) {
*(ul32 *)loc = 0x537; // lui a0,<hi20>
write_utype(loc, S + A - ctx.tp_addr);
}
*(ul32 *)loc = 0x537; // lui a0,<hi20>
write_utype(loc, S + A - ctx.tp_addr);
}
break;
case R_RISCV_TLSDESC_CALL:
if (sym2.has_tlsdesc(ctx)) {
// Do nothing
} else if (sym2.has_gottp(ctx)) {
// {ld,lw} a0, <lo12>(a0)
// l[d|w] a0,<lo12>
*(ul32 *)loc = E::is_64 ? 0x53503 : 0x52503;
write_itype(loc, sym2.get_gottp_addr(ctx) + A - P);
} else {
Expand Down

0 comments on commit 0702d41

Please sign in to comment.