Skip to content

LLD: Fix getFdePC with sdata2 or sdata4 #92228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,23 @@ uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff,
// the .eh_frame section.
size_t off = fdeOff + 8;
uint64_t addr = readFdeAddr(buf + off, enc & 0xf);
if ((enc & 0x70) == DW_EH_PE_absptr)
return addr;
if ((enc & 0x70) == DW_EH_PE_pcrel)
return addr + getParent()->addr + off + outSecOff;
if ((enc & 0x70) == DW_EH_PE_absptr) {
if ((enc & 0xf) == DW_EH_PE_sdata2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not desired for ELFCLASS64.

See the alternative fix: #92438 , but thanks for investigation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, yes. we won't support any platform with 16bit absptr with ELF.

return (uint64_t)(uint16_t)addr;
else if ((enc & 0xf) == DW_EH_PE_sdata4)
return (uint64_t)(uint32_t)addr;
else
return addr;
}
if ((enc & 0x70) == DW_EH_PE_pcrel) {
uint64_t addr_base = getParent()->addr + off + outSecOff;
if ((enc & 0xf) == DW_EH_PE_sdata2)
return (int16_t)addr + addr_base;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary.

else if ((enc & 0xf) == DW_EH_PE_sdata4)
return (int32_t)addr + addr_base;
else
return addr + addr_base;
}
fatal("unknown FDE size relative encoding");
}

Expand Down
8 changes: 8 additions & 0 deletions lld/test/ELF/Inputs/mips32-kseg0.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
OUTPUT_ARCH(mips)
SECTIONS
{
. = 0x80000000;
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss) }
}
5 changes: 5 additions & 0 deletions lld/test/ELF/mips-eh_frame-pic.s
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@
# RUN: llvm-dwarfdump --eh-frame %t-nopic32.o | FileCheck %s --check-prefix=ABS32-EH-FRAME
# RUN: llvm-readobj -r %t-nopic32.o | FileCheck %s --check-prefixes=RELOCS,ABS32-RELOCS
# RUN: not ld.lld -shared %t-nopic32.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=NOPIC32-ERR
# RUN: ld.lld %t-nopic32.o -T %p/Inputs/mips32-kseg0.lds -eh-frame-hdr -static -o /dev/null 2>&1 \
# RUN: | FileCheck %s --check-prefix=NOPIC32-ABSPTR
## Note: ld.bfd can link this file because it rewrites the .eh_frame section to use
## relative addressing.
# NOPIC32-ERR: ld.lld: error: relocation R_MIPS_32 cannot be used against local symbol
# NOPIC32-ABSPTR: cannot find entry symbol __start

## For -fPIC, .eh_frame should contain DW_EH_PE_pcrel | DW_EH_PE_sdata4 values:
# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux --position-independent %s -o %t-pic32.o
# RUN: llvm-readobj -r %t-pic32.o | FileCheck %s --check-prefixes=RELOCS,PIC32-RELOCS
# RUN: ld.lld -shared %t-pic32.o -o %t-pic32.so
# RUN: ld.lld -shared -T %p/Inputs/mips32-kseg0.lds %t-pic32.o -o %t-pic32-kseg0.so
# RUN: llvm-dwarfdump --eh-frame %t-pic32.so | FileCheck %s --check-prefix=PIC32-EH-FRAME
# RUN: llvm-dwarfdump --eh-frame %t-pic32-kseg0.so | FileCheck %s --check-prefix=PIC32-EH-FRAME

# RELOCS: .rel{{a?}}.eh_frame {
# ABS32-RELOCS-NEXT: 0x1C R_MIPS_32 .text
Expand Down