Skip to content

Commit

Permalink
Fix build with _map_libc defined
Browse files Browse the repository at this point in the history
Defining _map_libc (by passing -D_map_libc in CCFLAGS) causes
features/map.c to rename virtually all AST library functions with
an _ast_ prefix. But this type of build was broken becasue some
functions were renamed that weren't compiled in if they exist in
the system libraries, such as strlcpy.

If their C source file disables the function if a native version
exists, then the renaming of that function should be equally
disabled in features/map.c, otherwise we get linking errors.
  • Loading branch information
McDutchie committed Aug 25, 2024
1 parent f58153d commit 13c6f5d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/cmd/ksh93/data/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

#undef basename
#undef dirname
#undef mktemp /* undo possible map-libc mktemp => _ast_mktemp */

/*
* IMPORTANT: The order of these struct members must be synchronous
Expand Down Expand Up @@ -127,7 +128,6 @@ const struct shtable3 shtab_builtins[] =
"type", NV_BLTIN|BLT_ENV, bltin(whence),
"whence", NV_BLTIN|BLT_ENV, bltin(whence),
#ifdef SHOPT_CMDLIB_HDR
#undef mktemp /* undo possible map-libc mktemp => _ast_mktemp */
#include SHOPT_CMDLIB_HDR
#else
#if SHOPT_ALL_LIBCMD
Expand Down
13 changes: 7 additions & 6 deletions src/lib/libast/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ make install virtual
exec - invoke_iffe %{<}
done

note * ast_random.h *
make FEATURE/random
makp features/random
exec - invoke_iffe %{<}
done

note * sig.h: AST replacement for <signal.h>
make FEATURE/sig
makp features/sig.sh
Expand Down Expand Up @@ -187,6 +193,7 @@ make install virtual
make FEATURE/map
make features/map.c
prev sig.h
prev FEATURE/random
prev FEATURE/api
prev FEATURE/eaccess
prev FEATURE/options
Expand Down Expand Up @@ -576,12 +583,6 @@ make install virtual
exec - invoke_iffe %{<} -I.
done

note * ast_random.h *
make FEATURE/random
makp features/random
exec - invoke_iffe %{<}
done

note * copy most of the FEATURE/* results above to canonically named header files; separating
note * this allows all of the above tests, which don't depend on each other, to run in parallel

Expand Down
44 changes: 41 additions & 3 deletions src/lib/libast/features/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@
#include "FEATURE/options"
#include "FEATURE/eaccess"
#include "FEATURE/api"
#include "FEATURE/random"
#include <sig.h>

#if _opt_map_libc && !defined(_map_libc)
#define _map_libc 1
#endif

/*
* NOTE: for conditionally compiled AST functions, the preprocessor directives below
* must be kept in sync with the directives in the corresponding C source files, so
* that the rename is not done if the AST version of the function is not compiled
* in; otherwise a build with _map_libc defined will fail with linking errors.
*/

int
main(void)
{
Expand All @@ -59,6 +67,20 @@ main(void)
printf("\n");
printf("#define _map_libc 1\n");
#endif
#if _map_libc
#if !_lib_arc4random
printf("#undef arc4random\n");
printf("#define arc4random _ast_arc4random\n");
#endif
#if !_lib_arc4random_buf
printf("#undef arc4random_buf\n");
printf("#define arc4random_buf _ast_arc4random_buf\n");
#endif
#if !_lib_arc4random_uniform
printf("#undef arc4random_uniform\n");
printf("#define arc4random_uniform _ast_arc4random_uniform\n");
#endif
#endif
#if _map_libc || defined(__linux__)
printf("#undef basename\n");
printf("#define basename _ast_basename\n");
Expand Down Expand Up @@ -110,8 +132,10 @@ main(void)
printf("#undef sigunblock\n");
printf("#define sigunblock _ast_sigunblock\n");
#if _map_libc
#if !_lib_memdup
printf("#undef memdup\n");
printf("#define memdup _ast_memdup\n");
#endif
printf("#undef memhash\n");
printf("#define memhash _ast_memhash\n");
printf("#undef memsum\n");
Expand Down Expand Up @@ -158,6 +182,8 @@ main(void)
printf("#define pathfind _ast_pathfind\n");
printf("#undef pathgetlink\n");
printf("#define pathgetlink _ast_pathgetlink\n");
printf("#undef pathicase\n");
printf("#define pathicase _ast_pathicase\n");
printf("#undef pathinclude\n");
printf("#define pathinclude _ast_pathinclude\n");
printf("#undef pathkey\n");
Expand Down Expand Up @@ -251,21 +277,27 @@ main(void)
printf("#undef regsubfree\n");
printf("#define regsubfree _ast_regsubfree\n");
#if _map_libc
#if !(_std_remove || !_lib_unlink)
printf("#undef remove\n");
printf("#define remove _ast_remove\n");
printf("extern int remove(const char*);\n");
#endif
printf("#undef resolvepath\n");
printf("#define resolvepath _ast_resolvepath\n");
printf("extern int resolvepath(const char*, char*, size_t);\n");
#if !_lib_setenv
printf("#undef setenv\n");
printf("#define setenv _ast_setenv\n");
printf("extern int setenv(const char*, const char*, int);\n");
#endif
printf("#undef setenviron\n");
printf("#define setenviron _ast_setenviron\n");
printf("#undef sigcritical\n");
printf("#define sigcritical _ast_sigcritical\n");
#if !_lib_stracmp
printf("#undef stracmp\n");
printf("#define stracmp _ast_stracmp\n");
#endif
printf("#undef strcopy\n");
printf("#define strcopy _ast_strcopy\n");
printf("#undef strelapsed\n");
Expand All @@ -288,12 +320,16 @@ main(void)
printf("#define strhash _ast_strhash\n");
printf("#undef strkey\n");
printf("#define strkey _ast_strkey\n");
#if !_lib_strlcat
printf("#undef strlcat\n");
printf("#define strlcat _ast_strlcat\n");
printf("extern size_t strlcat(char*, const char*, size_t);\n");
#endif
#if !_lib_strlcpy
printf("#undef strlcpy\n");
printf("#define strlcpy _ast_strlcpy\n");
printf("extern size_t strlcpy(char*, const char*, size_t);\n");
#endif
printf("#undef strlook\n");
printf("#define strlook _ast_strlook\n");
printf("#undef strmatch\n");
Expand All @@ -304,8 +340,10 @@ main(void)
printf("#define strmode _ast_strmode\n");
#endif
#if _map_libc
#if !_lib_strnacmp
printf("#undef strnacmp\n");
printf("#define strnacmp _ast_strnacmp\n");
#endif
printf("#undef strncopy\n");
printf("#define strncopy _ast_strncopy\n");
printf("#undef strntod\n");
Expand Down Expand Up @@ -365,8 +403,10 @@ main(void)
printf("#define wordexp _ast_wordexp\n");
printf("#undef wordfree\n");
printf("#define wordfree _ast_wordfree\n");
#if !_lib_unsetenv
printf("#undef unsetenv\n");
printf("#define unsetenv _ast_unsetenv\n");
#endif
#endif
/* we always use the libast strdup implementation */
printf("#undef strdup\n");
Expand Down Expand Up @@ -472,11 +512,9 @@ main(void)
do
{
for (s = t; *t && *t != ' '; t++);
printf("#define %-.*s _ast_%-.*s\n", t - s, s, t - s, s);
printf("#define %-.*s _ast_%-.*s\n", (int)(t - s), s, (int)(t - s), s);
} while (*t++);
}
#endif
printf("\n");
printf("#undef extern\n");
return 0;
}
2 changes: 1 addition & 1 deletion src/lib/libast/features/random
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tst note{ does arc4random(3) become predictable after forking }end output{
pid_t child;
uint32_t r[3], r2[3];
/* create data file */
while (*t = *f++ && t < &file[PATHSIZE-4])
while ((*t = *f++) && t < &file[PATHSIZE-4])
t++;
*t++ = '.', *t++ = 'D', *t = 0;
if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/libast/man/strperm.3
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ not 0 then
is set to point to the first unrecognized character in
.IR s .
.PP
A tape device specification is composed of one or more
A file permission expression is composed of one or more
.I who-op-permission
terms separated by
.BR , .
Expand Down

0 comments on commit 13c6f5d

Please sign in to comment.