37
37
#include "libc/fmt/itoa.h"
38
38
#include "libc/intrin/atomic.h"
39
39
#include "libc/intrin/bits.h"
40
+ #include "libc/intrin/kprintf.h"
40
41
#include "libc/intrin/strace.internal.h"
41
42
#include "libc/limits.h"
42
43
#include "libc/nt/dll.h"
@@ -147,7 +148,7 @@ static int is_file_newer_than(const char *path, const char *other) {
147
148
}
148
149
if (stat (other , & st2 )) {
149
150
if (errno == ENOENT ) {
150
- return true ;
151
+ return 2 ;
151
152
} else {
152
153
return -1 ;
153
154
}
@@ -347,6 +348,7 @@ static dontinline void elf_exec(const char *file, char **envp) {
347
348
size_t argsize = (1 + 2 + 1 + envc + 1 + auxc * 2 + 1 + 3 ) * 8 ;
348
349
size_t mapsize = (stksize + argsize + (pagesz - 1 )) & - pagesz ;
349
350
size_t skew = (mapsize - argsize ) & (stkalign - 1 );
351
+ if (IsFreebsd ()) skew += 8 ; // FreeBSD calls _start() like a C function
350
352
map = __sys_mmap (0 , mapsize , PROT_READ | PROT_WRITE ,
351
353
MAP_PRIVATE | MAP_ANONYMOUS , -1 , 0 , 0 );
352
354
if (map == MAP_FAILED ) return ;
@@ -569,12 +571,17 @@ static dontinline bool foreign_compile(char exe[hasatleast PATH_MAX]) {
569
571
strlcat (exe , "dlopen-helper" , PATH_MAX );
570
572
571
573
// skip build if helper exists and this program is older
574
+ bool helper_exe_exists ;
572
575
switch (is_file_newer_than (GetProgramExecutableName (), exe )) {
573
576
case -1 :
574
577
return false;
575
- case false :
578
+ case 0 :
576
579
return true;
577
- case true:
580
+ case 1 :
581
+ helper_exe_exists = true;
582
+ break ;
583
+ case 2 :
584
+ helper_exe_exists = false;
578
585
break ;
579
586
default :
580
587
__builtin_unreachable ();
@@ -586,12 +593,14 @@ static dontinline bool foreign_compile(char exe[hasatleast PATH_MAX]) {
586
593
char sauce [sizeof (HELPER )];
587
594
strlcpy (src , exe , PATH_MAX );
588
595
strlcat (src , ".c" , PATH_MAX );
589
- if ((fd = open (src , O_RDONLY | O_CLOEXEC )) != -1 ) {
590
- ssize_t got = pread (fd , sauce , sizeof (HELPER ), 0 );
591
- close (fd );
592
- if (got == sizeof (HELPER ) - 1 &&
593
- !memcmp (sauce , HELPER , sizeof (HELPER ) - 1 )) {
594
- return true;
596
+ if (helper_exe_exists ) {
597
+ if ((fd = open (src , O_RDONLY | O_CLOEXEC )) != -1 ) {
598
+ ssize_t got = pread (fd , sauce , sizeof (HELPER ), 0 );
599
+ close (fd );
600
+ if (got == sizeof (HELPER ) - 1 &&
601
+ !memcmp (sauce , HELPER , sizeof (HELPER ) - 1 )) {
602
+ return true;
603
+ }
595
604
}
596
605
}
597
606
@@ -623,7 +632,7 @@ static dontinline bool foreign_compile(char exe[hasatleast PATH_MAX]) {
623
632
return false;
624
633
}
625
634
int pid , ws ;
626
- char * args [] = {"cc" , "-pie" , "-fPIC" , src , "-o" , tmp , "-ldl" , 0 };
635
+ char * args [] = {"cc" , "-pie" , "-fPIC" , src , "-o" , tmp , 0 };
627
636
errno_t err = posix_spawnp (& pid , args [0 ], NULL , NULL , args , environ );
628
637
if (err ) {
629
638
unlink (tmp );
0 commit comments