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
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
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

// non-preemptible, or we are linking statically.
if (pSym.isAbsolute() &&
(pSym.binding() == ResolveInfo::Local || config().isCodeStatic()))
(pSym.binding() == ResolveInfo::Local || !isSymbolPreemptible(pSym) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

This fix needs to be part of a different PR.

Please raise a issue and fix accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it should be part of this PR since it is part of making PIC relocation legality correct. Without this change, the existing defsym case (which isn't actually checked by the following readelf line) would incorrectly fail in ABS64RelocationPIE.test with recompile with -fPIC.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree it is an issue. We may need to cherry pick the change to another release branch if needed.

For this reason, this change needs to be in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can push it to the release branch when it's needed

Copy link
Contributor

Choose a reason for hiding this comment

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

Bug fixes / features need to be separated. Please push that as a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I am not sure if absolute symbols ever need a dynamic relocation, should it return false for all cases ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is all the same bug fix in my view

// 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
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
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.

return reportNonPICRelocation(reloc);
}

bool Relocator::checkDynamicRelocAllowed(const Relocation &reloc,
Copy link
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
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.

const bool isReadOnly = !(section.getFlags() & llvm::ELF::SHF_WRITE);

// -z text (default) rejects dynamic relocations in read-only sections.
if (section.isAlloc() && isReadOnly &&
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be checking for output section instead of input section.

cat > 1.s << 'S'
.globl foo
.section .foo, "a", @progbits
foo:
  .word bar
  ret

.data
.hidden bar
bar:
  .word 0
S

cat > s.t << \!
SECTIONS {
  .t : {
            *(.foo*)
            *(.data*)
  }
}
!


clang -c 1.s -target riscv32 -fPIC
$ riscv-ld -shared 1.o -o lib1.so -z text -T s.t && llvm-readelf -r lib1.so

Relocation section '.rela.dyn' at offset 0x1050 contains 1 entries:
 Offset     Info    Type                Sym. Value  Symbol's Name + Addend
0000005c  00000003 R_RISCV_RELATIVE                  62

the bfd linker does not error here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this. Updated.

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 qualcomm#832.

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

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