Skip to content

Don't emit PT_INTERP for -shared outputs#1577

Open
bamo wants to merge 1 commit into
rui314:mainfrom
bamo:fix/no-interp-on-shared
Open

Don't emit PT_INTERP for -shared outputs#1577
bamo wants to merge 1 commit into
rui314:mainfrom
bamo:fix/no-interp-on-shared

Conversation

@bamo
Copy link
Copy Markdown
Contributor

@bamo bamo commented May 8, 2026

When --dynamic-linker is passed alongside -shared, mold currently emits a .interp section and a PT_INTERP program header. Both lld and BFD ld silently drop --dynamic-linker for -shared outputs because shared libraries are loaded by the dynamic linker rather than bootstrapped by the kernel, so PT_INTERP serves no purpose there.

Gate .interp creation on (!shared || pie) so we keep it for regular executables, -pie executables, and -shared -pie runnable shared libraries (the libc.so.6-style case), and drop it only for the pure shared-library case. The PT_INTERP program header at output-chunks.cc:219 is conditional on ctx.interp being non-null, so the segment vanishes automatically with no second edit.

Fixes #1572

When --dynamic-linker is passed alongside -shared, mold currently emits
a .interp section and a PT_INTERP program header. Both lld and BFD ld
silently drop --dynamic-linker for -shared outputs because shared
libraries are loaded by the dynamic linker rather than bootstrapped by
the kernel, so PT_INTERP serves no purpose there.

Gate .interp creation on (!shared || pie) so we keep it for regular
executables, -pie executables, and -shared -pie runnable shared
libraries (the libc.so.6-style case), and drop it only for the pure
shared-library case. The PT_INTERP program header at
output-chunks.cc:219 is conditional on ctx.interp being non-null, so
the segment vanishes automatically with no second edit.

Fixes rui314#1572
@bamo
Copy link
Copy Markdown
Contributor Author

bamo commented May 8, 2026

btw, I empirically tested all four cases on lld 18.1.3 and BFD ld 2.42 (Ubuntu 24.04 / aarch64). mold's behavior after the fix is consistent with both linkers on every case, with one exception: lld rejects -shared -pie outright (ld.lld: error: -shared and -pie may not be used together), whereas mold and BFD both accept it and produce a runnable shared library (the libc.so.6 shape). The regression test pins that more permissive BFD-parity behavior.

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.

mold should not add .interp when -shared is used

1 participant