This is an archive of the discontinued LLVM Phabricator instance.

[ELF] -z notext: avoid dynamic relocations in .eh_frame
ClosedPublic

Authored by MaskRay on Feb 1 2023, 9:37 PM.

Details

Summary

Fix https://github.com/llvm/llvm-project/issues/60392

// a.cc
void raise() { throw 42; }
bool foo() {
  try { raise(); } catch (int) { return true; }
  return false;
}
int main() { foo(); }
clang++ --target=x86_64-linux-gnu -fno-pic -mcmodel=large -no-pie -fuse-ld=lld -z notext a.cc -o a && ./a
clang++ --target=aarch64-linux-gnu -fno-pic -no-pie -fuse-ld=lld -Wl,--dynamic-linker=/usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1 -Wl,-rpath=/usr/aarch64-linux-gnu/lib -z notext a.cc -o a && ./a

Both commands fail because we produce a dynamic relocation for
R_X86_64_64/R_AARCH64_ABS64 in .eh_frame which will be adjusted to a wrong
offset by SectionBase::getOffset after D122459.

Since GNU ld uses a canonical PLT entry instead of a dynamic relocation for
.eh_frame, we follow suit as well to avoid the issue.

Mips has an ABI issue (https://github.com/llvm/llvm-project/issues/5837) and we
don't implement GNU ld's DW_EH_PE_absptr conversion. mips64-eh-abs-reloc.s wants
a dynamic relocation, so keep the original behavior for EM_MIPS.

Diff Detail

Event Timeline

MaskRay created this revision.Feb 1 2023, 9:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 1 2023, 9:37 PM
MaskRay requested review of this revision.Feb 1 2023, 9:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 1 2023, 9:37 PM
MaskRay edited the summary of this revision. (Show Details)Feb 1 2023, 9:40 PM
peter.smith accepted this revision.Feb 3 2023, 9:53 AM

LGTM as avoiding text segment relocations when we can is probably better than trying to add dynamic relocations.

I'm not sure I fully understand why our sectionBase::getOffset isn't working for this particular case, I expect it would become clear if I traced through the code though. However I think the canonical PLT is good solution regardless.

Some suggestions for the language in the comments.

lld/ELF/Relocations.cpp
1082–1090

Use a simple -z notext rule that treats all sections except .eh_frame as writable. GNU ld does not produce dynamic relocations in .ehframe (and if we do, our sectionBase::getOffset will incorrectly adjust the offset).

lld/test/ELF/eh-frame-znotext.s
5

Typo and and.

5

There's a typo (and and), could be useful to restrict the canonical PLT to .ehframe. Probably implicit from the test name though.

suggest "try avoiding that in .ehframe (https://github.com/llvm/llvm-project/issues/60392) and use a canonical PLT entry instead.

This revision is now accepted and ready to land.Feb 3 2023, 9:53 AM
MaskRay updated this revision to Diff 494684.Feb 3 2023, 10:24 AM
MaskRay marked 3 inline comments as done.

comments

This revision was landed with ongoing or failed builds.Feb 3 2023, 10:27 AM
This revision was automatically updated to reflect the committed changes.