Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHP static PIE ELF build #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions php/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/php-*

34 changes: 34 additions & 0 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Build PHP as static PIE

Build the [PHP](https://www.php.net/) interpreter as static PIE ELF running on Linux

## Requirements

Make sure the following packages are installed:

* gcc >= 8
* GNU Make
* autoconf
* bison
* re2c
* libxml2 development files
* libsqlite3 development files

On Ubuntu, you can install these using:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
On Ubuntu, you can install these using:
On Ubuntu, you can install these using:


```
sudo apt install -y pkg-config build-essential autoconf bison re2c \
libxml2-dev libsqlite3-dev
```

## Build

The `php` static PIE ELF file is located in the current directory.
In order to rebuild it you have to run the `build.sh` bash script

The scripts downloads, unpack, patches and build the `php` static PIE ELF file.

## Run

In order to run a script using the static PIE php you can pass it as an argument to the ELF file.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change

32 changes: 32 additions & 0 deletions php/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

PHP_VERSION=8.1.11
PHP_URL="https://www.php.net/distributions/php-$PHP_VERSION.tar.gz"

#clean up
rm -rf php-*

echo -n "Downloading PHP $PHP_VERSION ... "
wget -q "$PHP_URL"
echo ""

echo -n "Unpacking PHP $PHP_VERSION ... "
tar xzf php-$PHP_VERSION.tar.gz
echo ""

pushd php-$PHP_VERSION > /dev/null 2>&1 || exit 1

echo "Configuring PHP $PHP_VERSION ... "
#iconv and opcache seem to have problems with being in a static build
CFLAGS=-fPIC ./configure
#patch the Makefile
sed -e '/^BUILD_C/s/ -export-dynamic//g' -e 's/^EXTRA_LIBS = .*/EXTRA_LIBS = -lrt -lxml2 -lsqlite3 -lz -llzma -licuuc -licudata -lstdc++ -lm/g' -e '/^EXTRA_LDFLAGS_PROGRAM/s/$/ -static-pie/g' -i Makefile
echo ""

echo "Building PHP $PHP_VERSION ... "
make -j "$(nproc)"
popd > /dev/null 2>&1 || exit 1

ln -fn php-$PHP_VERSION/sapi/cli/php .

rm -rf php-*
2 changes: 2 additions & 0 deletions php/helloworld.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<? echo "Hello world!\n" ?>

Binary file added php/php
Binary file not shown.