Skip to content

lld: fix Linx HI/LO relocation pairing#24

Open
touzicongbupianren wants to merge 1 commit into
LinxISA:mainfrom
touzicongbupianren:lihan-lld-hi-lo-fix
Open

lld: fix Linx HI/LO relocation pairing#24
touzicongbupianren wants to merge 1 commit into
LinxISA:mainfrom
touzicongbupianren:lihan-lld-hi-lo-fix

Conversation

@touzicongbupianren

Copy link
Copy Markdown

This PR fixes Linx LLD HI/LO relocation pairing.

Problem:

  • Some R_LINX_LO12 relocations directly reference a final symbol with a non-zero addend.
  • The previous Linx HI/LO recovery could match by symbol only, which may pair a LO12 with the wrong HI20.
  • Linx block layout may also place the matching HI20 after the LO12 in the same input section.

Fix:

  • Match R_LINX_PCREL_HI20 by exact (symbol, addend).
  • Search both before and after the LO12 in the same input section.
  • Keep LO12 calculation based on the paired HI20 target address.

Validation:

  • Rebuilt ld.lld.
  • glibc G1b relink passed.
  • Dynamic Linx printf ELF links with no R_LINX_LO12 warning.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the relocation handling in lld/ELF/InputSection.cpp to support recovering R_LINX_PCREL_HI20 relocations in either direction by exact symbol and addend, and defers error reporting for unsupported defined anchors. It also updates getRelocTargetVA to compute the target address directly for RE_LINX_PC_INDIRECT. The reviewer suggested a performance optimization in findLinxPCRelHiBySymbol to avoid calling llvm::lower_bound twice by copying the iterator for the backward search.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread lld/ELF/InputSection.cpp
Comment on lines +778 to +796
const Relocation *before = nullptr;
while (it != isec->relocs().begin()) {
--it;
if (it->sym == sym && isLinxPCRelHi(it->type))
return &*it;
if (it->sym == sym && it->addend == addend && isLinxPCRelHi(it->type)) {
before = &*it;
break;
}
}
return nullptr;

const Relocation *after = nullptr;
it = llvm::lower_bound(
isec->relocs(), loOffset,
[](const Relocation &lhs, uint64_t rhs) { return lhs.offset < rhs; });
for (; it != isec->relocs().end(); ++it) {
if (it->sym == sym && it->addend == addend && isLinxPCRelHi(it->type)) {
after = &*it;
break;
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

We can avoid calling llvm::lower_bound twice by copying the iterator it for the backward search. This eliminates the redundant binary search overhead on the relocation list.

  const Relocation *before = nullptr;
  for (auto rollbackIt = it; rollbackIt != isec->relocs().begin();) {
    --rollbackIt;
    if (rollbackIt->sym == sym && rollbackIt->addend == addend && isLinxPCRelHi(rollbackIt->type)) {
      before = &*rollbackIt;
      break;
    }
  }

  const Relocation *after = nullptr;
  for (; it != isec->relocs().end(); ++it) {
    if (it->sym == sym && it->addend == addend && isLinxPCRelHi(it->type)) {
      after = &*it;
      break;
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant