Skip to content

Reject invalid absolute relocations in PIE/shared links#923

Open
quic-areg wants to merge 1 commit intoqualcomm:mainfrom
quic-areg:dynrelocs
Open

Reject invalid absolute relocations in PIE/shared links#923
quic-areg wants to merge 1 commit intoqualcomm:mainfrom
quic-areg:dynrelocs

Conversation

@quic-areg
Copy link
Copy Markdown
Contributor

@quic-areg quic-areg commented Mar 6, 2026

Reject invalid absolute relocations in PIE/shared links

ELD currently permits some absolute relocations that should be rejected when producing PIE/shared objects.

ELD would also allow truncated absolute relocations in 64-bit outputs. Now it correctly reports an error.

Symbols fully resolved at link time (eg defsym) should not require a dynamic relocation.

With -z text, reject absolute relocations in PIE/shared links.

With -z notext, split behavior by target bitness:

  • 32-bit targets may emit TEXTREL.
  • 64-bit targets error on truncated absolute dynamic relocations.

Closes #832.

@quic-areg quic-areg force-pushed the dynrelocs branch 3 times, most recently from 64c5622 to 164156c Compare March 10, 2026 00:04
Copy link
Copy Markdown
Contributor

@quic-seaswara quic-seaswara left a comment

Choose a reason for hiding this comment

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

Please add tests for -z text with -shared links.

It might be helpful to add tests that will fail with -z text and not with -z notext

Comment thread lib/Target/X86/x86_64Relocator.cpp Outdated
Comment thread lib/Target/GNULDBackend.cpp
Comment thread lib/Target/Relocator.cpp
// Truncated absolute relocations cannot be safely represented as dynamic
// relocations for 64-bit outputs.
if (isAbs && config().targets().is64Bits() && getSize(reloc.type()) < 64)
return reportNonPICRelocation(reloc);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I could not follow this. This is not a dynamic relocation if the size of the reloc < 64.

Are we creating a dynamic relocation for this ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The point is that we cannot emit truncated absolute relocations for pie/shared links for 64-bit outputs. We currently do, and that's what this code aims to address. See pie-abs32-64.s for a test case: lld and bfd will reject the truncated relocation while today's eld will accept it.

Comment thread lib/Target/Relocator.cpp
return reportNonPICRelocation(reloc);
}

bool Relocator::checkDynamicRelocAllowed(const Relocation &reloc,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of introducing a new function, can we change the function helper_DynRel_init to error out if we are ending up creating a dynamic relocation from read only sections ?

Copy link
Copy Markdown
Contributor Author

@quic-areg quic-areg Mar 11, 2026

Choose a reason for hiding this comment

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

I think it is easier and cleaner to keep legality decisions in a separate function.

Comment thread lib/Target/Relocator.cpp Outdated
Comment thread lib/Target/AArch64/AArch64Relocator.h Outdated
Comment thread test/Common/standalone/PIEAbsoluteRelocs/pie-abs32-64.s
@quic-areg quic-areg force-pushed the dynrelocs branch 2 times, most recently from c957003 to 77c45b1 Compare March 11, 2026 21:25
@quic-areg
Copy link
Copy Markdown
Contributor Author

Please add tests for -z text with -shared links.
Done.

It might be helpful to add tests that will fail with -z text and not with -z notext
Already there.

@quic-areg quic-areg force-pushed the dynrelocs branch 2 times, most recently from 0a0bafb to f20eeaa Compare April 6, 2026 21:37
ELD currently permits some absolute relocations that should be rejected
when producing PIE/shared objects.

ELD would also allow truncated absolute relocations in 64-bit outputs. Now
it correctly reports an error.

With -z text, reject absolute relocations in PIE/shared links.

With -z notext, split behavior by target bitness:

- 32-bit targets may emit TEXTREL.
- 64-bit targets error on truncated absolute dynamic relocations.

Closes qualcomm#832.

Signed-off-by: quic-areg <aregmi@qti.qualcomm.com>
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.

Add support for -z text

3 participants