Skip to content

Commit 4318ef8

Browse files
authored
fix aarch64 shared extensions segfault with zig 0.16.0 (#1110)
2 parents bfe4a01 + 8630fd5 commit 4318ef8

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

src/SPC/builder/Extension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use SPC\exception\WrongUsageException;
1212
use SPC\store\Config;
1313
use SPC\store\FileSystem;
14+
use SPC\toolchain\ToolchainManager;
15+
use SPC\toolchain\ZigToolchain;
16+
use SPC\util\GlobalEnvManager;
1417
use SPC\util\SPCConfigUtil;
1518
use SPC\util\SPCTarget;
1619

@@ -231,7 +234,7 @@ public function patchBeforeSharedMake(): bool
231234
if (preg_match('/^(.*_SHARED_LIBADD\s*=\s*)(.*)$/m', $makefileContent, $matches)) {
232235
$prefix = $matches[1];
233236
$currentLibs = trim($matches[2]);
234-
$newLibs = trim("{$currentLibs} {$staticLibs} {$lstdcpp}");
237+
$newLibs = clean_spaces("{$currentLibs} {$staticLibs} {$lstdcpp}");
235238
$deduplicatedLibs = deduplicate_flags($newLibs);
236239

237240
FileSystem::replaceFileRegex(
@@ -543,6 +546,11 @@ public function getLibraryDependencies(bool $recursive = false): array
543546
*/
544547
protected function getSharedExtensionEnv(): array
545548
{
549+
$compiler_extra = getenv('SPC_COMPILER_EXTRA') ?: '';
550+
if (!str_contains($compiler_extra, '-lcompiler_rt') && ToolchainManager::getToolchainClass() === ZigToolchain::class) {
551+
$compiler_extra = trim($compiler_extra . ' -lcompiler_rt');
552+
GlobalEnvManager::putenv("SPC_COMPILER_EXTRA={$compiler_extra}");
553+
}
546554
$config = (new SPCConfigUtil($this->builder, ['no_php' => true]))->getExtensionConfig($this);
547555
[$staticLibs, $sharedLibs] = $this->splitLibsIntoStaticAndShared($config['libs']);
548556
$preStatic = PHP_OS_FAMILY === 'Darwin' ? '' : '-Wl,--start-group ';

src/SPC/builder/linux/LinuxBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use SPC\store\DirDiff;
1212
use SPC\store\FileSystem;
1313
use SPC\store\SourcePatcher;
14+
use SPC\toolchain\ToolchainManager;
15+
use SPC\toolchain\ZigToolchain;
1416
use SPC\util\GlobalEnvManager;
1517
use SPC\util\SPCConfigUtil;
1618
use SPC\util\SPCTarget;
@@ -65,7 +67,8 @@ public function buildPHP(int $build_target = BUILD_TARGET_NONE): void
6567
// php 8.5 contains opcache extension by default,
6668
// if opcache_jit is enabled for 8.5 or opcache enabled,
6769
// we need to disable undefined behavior sanitizer.
68-
f_putenv('SPC_COMPILER_EXTRA=-fno-sanitize=undefined');
70+
$compiler_extra = getenv('SPC_COMPILER_EXTRA') ?: '';
71+
f_putenv('SPC_COMPILER_EXTRA=' . trim($compiler_extra . ' -fno-sanitize=undefined'));
6972
}
7073

7174
if ($this->getOption('enable-zts', false)) {
@@ -266,6 +269,11 @@ protected function buildFpm(): void
266269
*/
267270
protected function buildEmbed(): void
268271
{
272+
$compiler_extra = getenv('SPC_COMPILER_EXTRA') ?: '';
273+
if (!str_contains($compiler_extra, '-lcompiler_rt') && ToolchainManager::getToolchainClass() === ZigToolchain::class) {
274+
$compiler_extra = trim($compiler_extra . ' -lcompiler_rt');
275+
GlobalEnvManager::putenv("SPC_COMPILER_EXTRA={$compiler_extra}");
276+
}
269277
$sharedExts = array_filter($this->exts, static fn ($ext) => $ext->isBuildShared());
270278
$sharedExts = array_filter($sharedExts, static function ($ext) {
271279
return Config::getExt($ext->getName(), 'build-with-php') === true;

src/globals/common-tests/embed.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
#include <sapi/embed/php_embed.h>
22

3-
int main(int argc,char **argv){
4-
5-
PHP_EMBED_START_BLOCK(argc,argv)
6-
3+
int main(int argc, char **argv) {
4+
PHP_EMBED_START_BLOCK(argc, argv)
75
zend_file_handle file_handle;
8-
9-
zend_stream_init_filename(&file_handle,"embed.php");
10-
11-
if(!php_execute_script(&file_handle)){
6+
zend_stream_init_filename(&file_handle, "embed.php");
7+
if(!php_execute_script(&file_handle)) {
128
php_printf("Failed to execute PHP script.\n");
139
}
14-
1510
PHP_EMBED_END_BLOCK()
1611
return 0;
1712
}

0 commit comments

Comments
 (0)