Page MenuHomeFreeBSD

Do not allow to load ET_DYN object with DF_1_PIE flag set.
ClosedPublic

Authored by kib on May 31 2020, 10:48 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Apr 20, 7:00 PM
Unknown Object (File)
Sat, Apr 20, 5:45 PM
Unknown Object (File)
Feb 11 2024, 12:36 PM
Unknown Object (File)
Jan 13 2024, 11:06 AM
Unknown Object (File)
Dec 21 2023, 11:09 PM
Unknown Object (File)
Dec 20 2023, 7:21 AM
Unknown Object (File)
Dec 3 2023, 4:40 AM
Unknown Object (File)
Oct 7 2023, 1:13 AM

Details

Summary

Linkers are supposed to mark PIE binaries with DF_1_PIE, such binary cannot be correctly and usefully loaded neither by dlopen(3) nor as a dependency of other object.

Requested by: emaste

Test Plan

I cannot test, because it seems -fPIE -Wl,-pie is broken for all current toolchains, both base and ports.

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

kib requested review of this revision.May 31 2020, 10:48 PM

lld 10 refuses to link with -fPIE -Wl,--pie with errors like

ld: error: cannot preempt symbol: _DYNAMIC
>>> defined in 
>>> referenced by crt1.c:62 (/usr/home/emaste/src/freebsd-svn/head/lib/csu/amd64/crt1.c:62)
>>>               /usr/lib/crt1.o:(_start)
...

lld 8 proceeds:

$ cc -fuse-ld=lld80 -fPIE -Wl,--pie -Wl,-z,notext ~/hello.c
$

but the resulting binary segfaults on startup

lld 10 refuses to link with -fPIE -Wl,--pie with errors like

ld: error: cannot preempt symbol: _DYNAMIC
>>> defined in 
>>> referenced by crt1.c:62 (/usr/home/emaste/src/freebsd-svn/head/lib/csu/amd64/crt1.c:62)
>>>               /usr/lib/crt1.o:(_start)
...

lld 8 proceeds:

$ cc -fuse-ld=lld80 -fPIE -Wl,--pie -Wl,-z,notext ~/hello.c
$

but the resulting binary segfaults on startup

Problem is that all drivers link with crt1.o instead of Scrt1.o. See r200038.

Problem is that all drivers link with crt1.o instead of Scrt1.o. See r200038.

Hrm,

$ cc -g -pie -Wl,--pie -Wl,-z,notext ~/hello.c
$ ./a.out
Hello, world
$ file a.out
a.out: ELF 64-bit LSB shared object, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 13.0 (1300087), FreeBSD-style, with debug_info, not stripped

Following up after discussions on IRC, the canonical way do do this is:
CFLAGS=-fPIC
LDFLAGS=-pic
or if compiling a test app on the command line,
cc -fPIC -pic ...

This looks reasonable to me. We'll need to bring in a couple of LLVM commits to be able to test.

This revision is now accepted and ready to land.Jun 2 2020, 1:07 AM

This looks reasonable to me. We'll need to bring in a couple of LLVM commits to be able to test.

I tested it with gcc 10.1 / binutils 2.34.

arichardson added inline comments.
head/libexec/rtld-elf/rtld.c
2586

Since this could be a user-visible message, maybe spell out dynamic shared object or use uppercase DSO to indicate that it's an abbreviation?