Skip to content

Comments

Refactoring linker#40

Merged
katsyoshi merged 20 commits intomainfrom
refactoring-linker
Feb 21, 2026
Merged

Refactoring linker#40
katsyoshi merged 20 commits intomainfrom
refactoring-linker

Conversation

@katsyoshi
Copy link
Owner

@katsyoshi katsyoshi commented Feb 21, 2026

Summary

This PR refactors linker write flow and adds PIE dynamic-link scaffolding around PLT relocations.

  • Refactor Writer phases (section write, relocation patching, dynamic patching, program-header patching, section-header finalization).
  • Keep Builder section order as the source of truth and remove implicit writer-side reorder assumptions.
  • Add .plt, .got.plt, and .rela.plt scaffolding and wire them through Builder/Writer.
  • Patch .rela.plt relocation info/offsets against final .dynsym/.got.plt layout.
  • Add explicit unsupported handling for GOTPCREL-family relocation types for now.

Main changes

Linker/ELF internals

  • lib/caotral/linker/writer.rb
    • Phase-oriented write flow cleanup.
    • .plt/.got.plt section writes + header updates.
    • .rela.plt symbol remap to dynamic symbol indexes before write.
    • sh_link/sh_info/sh_entsize handling for relocation sections adjusted for dynamic relocations.
  • lib/caotral/linker/builder.rb
    • PLT relocation path wiring for PIE (.rela.plt, .got.plt, .dynsym/.dynstr integration).
  • lib/caotral/binary/elf/section/rel.rb
    • Relocation type table updates used by linker paths.
  • lib/caotral/binary/elf/section/dynamic.rb
    • Dynamic tag helpers for PLT-related tags.
  • Reader/header/program-header helpers updated to support relocation/dynamic validation paths.

Test/fixtures

  • Added fixtures:
    • sample/C/plt-call.c
    • sample/C/gotpcrel-call.c
  • Added tests:
    • test/caotral/linker/plt-call_test.rb (PLT/rela.plt and dynamic-tag consistency)
    • test/caotral/linker/gotpc-call_test.rb (unsupported GOTPCREL path)
  • Updated existing linker tests for refactor compatibility.

Validation run

  • ruby test/caotral/linker/multi-link_test.rb
  • ruby test/caotral/linker/pie-object_test.rb
  • ruby test/caotral/linker/plt-call_test.rb
  • ruby test/caotral/linker/gotpc-call_test.rb

Follow-up

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f4e824a780

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +193 to +194
rela_plt_section.body << rps
next

Choose a reason for hiding this comment

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

P1 Badge Preserve call-site relocation for undefined PLT32 symbols

When handling undefined R_X86_64_PLT32 relocations, this branch appends a .rela.plt record and immediately nexts, so the original text relocation path is skipped and no call-site patching happens here. For executable/PIE outputs that call external symbols (like sample/C/plt-call.c), .rela.text is later removed, leaving the call displacement at its placeholder value and producing a runtime jump to the wrong address.

Useful? React with 👍 / 👎.

Comment on lines +66 to +67
sym = symtab_section.body[rel.sym]
dynsymndx = dynsym_section.body.index { |ds| ds.name_offset == dynstr_section.body.offset_of(sym.name_string) }

Choose a reason for hiding this comment

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

P1 Badge Remap .rela.plt symbol indexes after sorting .symtab

This lookup treats rel.sym as a stable .symtab index, but .rela.plt entries are created before symtab_section.body.sort_by! and are never reindexed afterward (only rel_sections get remapped). In multi-object links, sorting shifts symbol positions, so .rela.plt can resolve against the wrong symbol here or fail with cannot find symbol ... in .dynsym even though the undefined symbol exists.

Useful? React with 👍 / 👎.

@katsyoshi katsyoshi merged commit 3ae563b into main Feb 21, 2026
2 checks passed
@katsyoshi katsyoshi deleted the refactoring-linker branch February 21, 2026 14:21
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