// QNX 8.0 'gcc' linker driver drop-in replacement by Pierre-Marie Baty <pm@pmbaty.com>
// RATIONALE
// until a proper QNXToolchain class is implemented in the Clang driver, Clang relies on GNU GCC to drive the system linker 'ld'.
// So we *HAVE* to provide a 'gcc' executable for Clang to be able to link QNX files.
// GNU GCC is itself a driver which can also invoke a compiler or a linker (gcc invokes collect2 which in turn invokes ld).
// This means our GCC will need to either chain-call 'clang' when it is called for *compiling* and chain-call 'ld' when it is called for *linking*.
// When proper support is added to it, GNU GCC contains a linker specification for the system which it uses to construct the linker command line.
// The linker specifications for QNX 8.0 can be extracted from '<target triple>-gcc -dumpspecs' and have been reimplemented in this file.
// The GNU specs language is documented here: https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html
// environment variables:
// DEBUG_LINKER_DRIVER - when defined to any value (incl. empty string), print the constructed command line to stderr before calling the linker.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#ifndef _WIN32
#include <unistd.h>
#define EXECUTABLE_SUFFIX
#else // _WIN32
#include <process.h>
#include <io.h>
#define strdup(s) _strdup ((s))
#define strcasecmp(a,b) _stricmp ((a), (b))
#define access(f,m) _access ((f), (m))
#define execvpe(exename,argv,envp) exit ((int) _spawnvpe (P_WAIT, (exename), (argv), (envp)))
int vasprintf (char **out_string, const char *fmt, va_list va_args) // vasprintf() implementation
{
int retval; va_list ap;
*out_string
= (char *) malloc (16); if (*out_string
== NULL
) return (-1);
va_copy (ap
, va_args
); retval
= vsnprintf
(*out_string
, 16, fmt
, ap
); va_end (ap
); if (retval
== -1) return (-1);
if (retval >= 16)
{
void *new_string
= realloc (*out_string
, (size_t) retval
+ 1); if (new_string
== NULL
) return (-1); *out_string
= (char *) new_string
;
va_copy (ap
, va_args
); retval
= vsnprintf
(*out_string
, (size_t) retval
+ 1, fmt
, va_args
); va_end (ap
);
}
return (retval);
}
int asprintf
(char **out_string
, const char *fmt
, ...
) { int retval
; va_list ap
; va_start (ap
, fmt
); retval
= vasprintf
(out_string
, fmt
, ap
); va_end (ap
); return (retval
); } // asprintf() implementation
#define EXECUTABLE_SUFFIX ".exe"
#endif // !_WIN32
#define BASEVER "12.2.0"
#if (defined(__x86_64__) || defined(_WIN64))
#define IS_TARGET_INTEL 1
#define TARGET_TRIPLE "x86_64-pc-nto-qnx8.0.0"
#elif defined(__aarch64__)
#define IS_TARGET_INTEL 0
#define TARGET_TRIPLE "aarch64-unknown-nto-qnx8.0.0"
#else
#error unsupported platform (not x86_64, not aarch64le). Please implement!
#endif
#define COMPILER "clang" EXECUTABLE_SUFFIX
#define LINKER TARGET_TRIPLE "-ld"
//#define LINKER "ld.lld"
#define ARGMODE_SINGLE 0
#define ARGMODE_HASVAL 1
#define ARGMODE_EQUALVAL 2
// handy macros that generate a version number in the format "YYYYMMDD" corresponding to the build date. Usage: printf ("version " VERSION_FMT_YYYYMMDD "\n", VERSION_ARG_YYYYMMDD);
#ifndef VERSION_ARG_YYYYMMDD
#define BUILDDATE_YEAR (&__DATE__[7]) // compiler will optimize this into a const string, e.g. "2021"
#define BUILDDATE_MONTH ( \
*((uint32_t *) __DATE__) == *((uint32_t *) "Jan ") ? "01" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Feb ") ? "02" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Mar ") ? "03" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Apr ") ? "04" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "May ") ? "05" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Jun ") ? "06" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Jul ") ? "07" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Aug ") ? "08" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Sep ") ? "09" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Oct ") ? "10" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Nov ") ? "11" : \
*((uint32_t *) __DATE__) == *((uint32_t *) "Dec ") ? "12" : \
"XX" \
) // compiler will optimize this into a const string, e.g. "11"
#define BUILDDATE_DAY ( \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 1 ") ? "01" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 2 ") ? "02" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 3 ") ? "03" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 4 ") ? "04" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 5 ") ? "05" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 6 ") ? "06" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 7 ") ? "07" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 8 ") ? "08" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 9 ") ? "09" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 10 ") ? "10" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 11 ") ? "11" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 12 ") ? "12" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 13 ") ? "13" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 14 ") ? "14" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 15 ") ? "15" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 16 ") ? "16" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 17 ") ? "17" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 18 ") ? "18" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 19 ") ? "19" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 20 ") ? "20" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 21 ") ? "21" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 22 ") ? "22" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 23 ") ? "23" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 24 ") ? "24" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 25 ") ? "25" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 26 ") ? "26" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 27 ") ? "27" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 28 ") ? "28" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 29 ") ? "29" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 30 ") ? "30" : \
*((uint32_t *) &__DATE__[3]) == *((uint32_t *) " 31 ") ? "31" : \
"XX" \
) // compiler will optimize this into a const string, e.g. "14"
#define VERSION_FMT_YYYYMMDD "%s%s%s"
#define VERSION_ARG_YYYYMMDD BUILDDATE_YEAR, BUILDDATE_MONTH, BUILDDATE_DAY
#endif // !VERSION_ARG_YYYYMMDD
char *array_contains (int argc, char **argv, char *needle, int mode)
{
char *arg;
int arg_index;
size_t needle_len
= strlen (needle
);
for (arg_index = 1; arg_index < argc; arg_index++)
{
arg = argv[arg_index];
if (mode == ARGMODE_SINGLE)
{
if (strcmp (arg
, needle
) == 0)
return ("");
}
else if (mode == ARGMODE_HASVAL)
{
if ((arg_index
+ 1 < argc
) && (strcmp (arg
, needle
) == 0))
return (argv[arg_index + 1]);
else if ((strncmp (arg
, needle
, needle_len
) == 0) && (arg
[needle_len
] != 0))
return (&arg[needle_len]);
}
else if (mode == ARGMODE_EQUALVAL)
{
if ((strncmp (arg
, needle
, needle_len
) == 0) && (arg
[needle_len
] == '='))
return (&arg[needle_len + 1]);
}
}
return (NULL);
}
char *resolve_envvars (char *str)
{
char *out_str = strdup (str), *new_str;
char *varstart, *varstop, *varval;
while ((varstart
= strstr (out_str
, "${")) != NULL
)
{
*varstart = 0;
varstart += 2;
varstop
= strchr (varstart
, '}');
*varstop = 0;
asprintf (&new_str, "%s%s%s", out_str, (varval != NULL ? varval : ""), varstop + 1);
out_str = new_str;
}
return (out_str);
}
char *makefmtstr (const char *fmt, ...)
{
va_list ap;
char *outstr = NULL;
vasprintf (&outstr, fmt, ap);
return (outstr);
}
int main (int argc, char **argv, char **envp)
{
#define ADD_CHAR_ARRAY(array_var,arraysize_var,val) do { \
void *reallocated_ptr = realloc (array_var, (arraysize_var + 2) * sizeof (char *)); \
if (reallocated_ptr == NULL) exit (1); \
array_var = reallocated_ptr; \
array_var[arraysize_var] = (val); \
array_var[arraysize_var + 1] = NULL; \
arraysize_var++; \
} while (0)
#define ADD_LINKER_ARG(val) ADD_CHAR_ARRAY (linker_argv, linker_argc, (val))
#define ADD_L_ARG(val) ADD_CHAR_ARRAY (largs, larg_count, (val))
#define ADD_O_ARG(val) ADD_CHAR_ARRAY (oargs, oarg_count, (val))
#define ADD_S_ARG(val) ADD_CHAR_ARRAY (sargs, sarg_count, (val))
#define ARGV_CONTAINS_SINGLEARG(val) ((arg = array_contains (argc, argv, (val), ARGMODE_SINGLE)) != NULL)
#define ARGV_CONTAINS_ARGWITHVAL(val) ((arg = array_contains (argc, argv, (val), ARGMODE_HASVAL)) != NULL)
#define ARGV_CONTAINS_ARGWITHEQUALVAL(val) ((arg = array_contains (argc, argv, (val), ARGMODE_EQUALVAL)) != NULL)
#define ARGV_SINGLEARG(val) array_contains (argc, argv, (val), ARGMODE_SINGLE)
#define ARGV_ARGWITHVAL(val) array_contains (argc, argv, (val), ARGMODE_HASVAL)
#define ARGV_ARGWITHEQUALVAL(val) array_contains (argc, argv, (val), ARGMODE_EQUALVAL)
static char filebuf[1024 * 1024];
char **linker_argv = NULL;
size_t linker_argc = 0;
char **largs = NULL; // array of "-lxxx" arguments
size_t larg_count = 0;
char **sargs = NULL; // array of source files arguments
size_t sarg_count = 0;
char **oargs = NULL; // array of object files arguments
size_t oarg_count = 0;
size_t read_count;
size_t byte_index;
bool was_cpluspluslib_included = false;
bool requires_libcplusplus = false;
int arg_index;
char *arg;
FILE *fp;
// special case: print the driver version and exit
if ((argc
== 2) && (strcmp (argv
[1], "--driver-version") == 0))
{
printf ("clang 'gcc' linker driver for QNX 8.0 version " VERSION_FMT_YYYYMMDD
" by Pierre-Marie Baty <pm@pmbaty.com>\n", VERSION_ARG_YYYYMMDD
);
return (0);
}
// if *any* of these flags are present, it means we don't want to link anything, so direct everything to the compiler
if ( ARGV_CONTAINS_SINGLEARG ("-fsyntax-only")
|| ARGV_CONTAINS_SINGLEARG ("-c")
|| ARGV_CONTAINS_SINGLEARG ("-M")
|| ARGV_CONTAINS_SINGLEARG ("-MM")
|| ARGV_CONTAINS_SINGLEARG ("-E")
|| ARGV_CONTAINS_SINGLEARG ("-S"))
{
if (ARGV_CONTAINS_SINGLEARG ("-c"))
for (arg_index = 1; arg_index < argc; arg_index++)
if (strcmp (argv
[arg_index
], "-shared") == 0)
argv[arg_index] = ""; // HACK for GNU autotools: when compiling, wipe out the -shared flag to avoid a warning
execvpe (COMPILER, argv, envp); // by leaving argv[0] untouched, we will be invoking Clang as 'gcc', which will give it a supplemental hint about how to behave
}
// collect source and object files in two separate lists
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strcmp (argv
[arg_index
- 1], "-o") != 0) // this arg is NOT preceded by "-o"...
&& (access (argv[arg_index], 0) == 0) // ... this arg is an existing file...
&& ((arg
= strrchr (argv
[arg_index
], '.')) != NULL
)) // this filename has an extension (FIXME: what if "-x <language>" is specified?)
{
if ( (strcasecmp (arg, ".s") == 0)
|| (strcasecmp (arg, ".c") == 0)
|| (strcasecmp (arg, ".cc") == 0)
|| (strcasecmp (arg, ".c++") == 0)
|| (strcasecmp (arg, ".cpp") == 0)
|| (strcasecmp (arg, ".cxx") == 0)
|| (strcasecmp (arg, ".m") == 0)
|| (strcasecmp (arg, ".mm") == 0)
|| (strcasecmp (arg, ".mxx") == 0))
ADD_S_ARG (argv[arg_index]); // this file is one of the known source file extensions: add to source files list
else
ADD_O_ARG (argv[arg_index]); // this file is NOT one of the known file extensions: add to object files list
}
// if we have zero .o file, OR if we have some source files, we have to compile first
if ((sarg_count > 0) || (oarg_count == 0))
{
if (ARGV_CONTAINS_SINGLEARG ("-c"))
for (arg_index = 1; arg_index < argc; arg_index++)
if (strcmp (argv
[arg_index
], "-shared") == 0)
argv[arg_index] = ""; // HACK for GNU autotools: when compiling, wipe out the -shared flag to avoid a warning
execvpe (COMPILER, argv, envp); // by leaving argv[0] untouched, we will be invoking Clang as 'gcc', which will give it a supplemental hint about how to behave
}
// collect -l flags
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strncmp (argv
[arg_index
], "-l", 2) == 0) && ((argv
[arg_index
])[2] != 0))
ADD_L_ARG (argv[arg_index]);
// LINKER " "
ADD_LINKER_ARG (LINKER);
// "%{!fno-use-linker-plugin:"
// "%{!fno-lto: "
// "-plugin ${QNX_HOST}/usr/lib/gcc/" TARGET_TRIPLE "/" BASEVER "/liblto_plugin.so "
// "-plugin-opt=${QNX_HOST}/usr/lib/gcc/" TARGET_TRIPLE "/" BASEVER "/lto-wrapper "
// "-plugin-opt=-fresolution=%u.res "
// "%{flinker-output=*:" "-plugin-opt=-linker-output-known" "} "
// "%{!nostdlib:"
// "%{!nodefaultlibs:" "%:pass-through-libs(%(link_gcc_c_sequence))" "}"
// "} "
// "}"
// "}"
if (!ARGV_CONTAINS_SINGLEARG ("-fno-use-linker-plugin") && !ARGV_CONTAINS_SINGLEARG ("-fno-lto"))
{
/*ADD_LINKER_ARG ("-plugin");
ADD_LINKER_ARG (resolve_envvars ("${QNX_HOST}/usr/lib/gcc/" TARGET_TRIPLE "/" BASEVER "/liblto_plugin.so"));
ADD_LINKER_ARG (resolve_envvars ("-plugin-opt=${QNX_HOST}/usr/lib/gcc/" TARGET_TRIPLE "/" BASEVER "/lto-wrapper"));
ADD_LINKER_ARG (makefmtstr ("-plugin-opt=-fresolution=%s.res", "TODO-UNKNOWN")); // LTO is not interoperable between clang and ld. See below.
if (ARGV_CONTAINS_ARGWITHEQUALVAL ("-flinker-output"))
ADD_LINKER_ARG ("-plugin-opt=-linker-output-known");
if (!ARGV_CONTAINS_SINGLEARG ("-nostdlib") && !ARGV_CONTAINS_SINGLEARG ("-nodefaultlibs"))
for (arg_index = 0; arg_index < larg_count; arg_index++)
ADD_LINKER_ARG (makefmtstr ("-plugin-opt=-pass-through=%s", largs[arg_index]));*/
// looks like LTO is a no-go. See: https://stackoverflow.com/questions/51259340/interoperability-between-clang-gcc-and-lto
}
// "%{flto|flto=*:" "%<fcompare-debug*" "} "
if (ARGV_CONTAINS_SINGLEARG ("-flto") || ARGV_CONTAINS_ARGWITHEQUALVAL ("-flto"))
for (arg_index = 1; arg_index < argc; arg_index++)
if (strncmp (argv
[arg_index
], "-fcompare-debug", 15) == 0)
argv[arg_index] = ""; // remove this arg
if (ARGV_CONTAINS_SINGLEARG ("-flto")) ADD_LINKER_ARG ("-flto"); // "%{flto} "
if (ARGV_CONTAINS_SINGLEARG ("-fno-lto")) ADD_LINKER_ARG ("-fno-lto"); // "%{fno-lto} "
if (ARGV_CONTAINS_ARGWITHEQUALVAL ("-flto")) ADD_LINKER_ARG (makefmtstr ("-flto=%s", arg)); // "%{flto=*} "
// "%{!static:" "--eh-frame-hdr" "} "
if (!ARGV_CONTAINS_SINGLEARG ("-static")) ADD_LINKER_ARG ("--eh-frame-hdr");
#if IS_TARGET_INTEL
// "%{!m32:" "-m elf_x86_64" "} "
// "%{m32:" "-m i386nto" "} "
ADD_LINKER_ARG ("-m");
if (ARGV_CONTAINS_SINGLEARG ("-m32"))
ADD_LINKER_ARG ("i386nto");
else
ADD_LINKER_ARG ("elf_x86_64");
#endif // IS_TARGET_INTEL
// "%{h*} "
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strcmp (argv
[arg_index
], "-h") == 0) && (arg_index
+ 1 < argc
))
{
ADD_LINKER_ARG ("-h");
ADD_LINKER_ARG (argv[++arg_index]);
}
else if ((strncmp (argv
[arg_index
], "-h", 2) == 0) && ((argv
[arg_index
])[2] != 0))
ADD_LINKER_ARG (argv[arg_index]);
// "%{v:" "-V" "} "
if (ARGV_CONTAINS_SINGLEARG ("-v")) ADD_LINKER_ARG ("-V");
// "%{b} "
if (ARGV_CONTAINS_SINGLEARG ("-b")) ADD_LINKER_ARG ("-b");
#if !IS_TARGET_INTEL
// "%{Wl,*:" "%*" "} "
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strcmp (argv
[arg_index
], "-Wl,") == 0) && (arg_index
+ 1 < argc
))
ADD_LINKER_ARG (argv[++arg_index]);
else if ((strncmp (argv
[arg_index
], "-Wl,", 4) == 0) && ((argv
[arg_index
])[4] != 0))
ADD_LINKER_ARG (argv[arg_index] + 4);
#endif // !IS_TARGET_INTEL
// "%{!r:" "--build-id=md5" "} "
if (!ARGV_CONTAINS_SINGLEARG ("-r")) ADD_LINKER_ARG ("--build-id=md5");
// "--warn-shared-textrel "
// "-zrelro "
// "-znow "
ADD_LINKER_ARG ("--warn-shared-textrel");
ADD_LINKER_ARG ("-zrelro"); // set relocations to be read-only
ADD_LINKER_ARG ("-znow"); // do immediate binding of dylibs (the opposite would be -zlazy, which is less secure)
// since clang doesn't seem to generate .note.GNU-stack sections in output ELF files we explicitly specify the linker that we don't want an executable stack
// TODO: fix this in clang itself
ADD_LINKER_ARG ("-znoexecstack");
#if IS_TARGET_INTEL
// "%{shared:" "-G -dy -z text" "} "
if (ARGV_CONTAINS_SINGLEARG ("-shared"))
{
ADD_LINKER_ARG ("-G");
ADD_LINKER_ARG ("-dy");
ADD_LINKER_ARG ("-z");
ADD_LINKER_ARG ("text");
}
// "%{symbolic:" "-Bsymbolic -G -dy -z text" "} "
if (ARGV_CONTAINS_SINGLEARG ("-symbolic"))
{
ADD_LINKER_ARG ("-Bsymbolic");
ADD_LINKER_ARG ("-G");
ADD_LINKER_ARG ("-dy");
ADD_LINKER_ARG ("-z");
ADD_LINKER_ARG ("text");
}
// "%{G:" "-G" "} "
if (ARGV_CONTAINS_SINGLEARG ("-G")) ADD_LINKER_ARG ("-G");
// "%{shared:" "-shared" "} "
// "%{!shared: "
// "%{!static: "
// "%{rdynamic:" "-export-dynamic" "} "
// "%{m32:"
// "%{!dynamic-linker:" "-dynamic-linker /usr/lib/ldqnx.so.2" "}"
// "} "
// "%{!m32:"
// "%{!dynamic-linker:" "-dynamic-linker /usr/lib/ldqnx-64.so.2" "}"
// "}"
// "} "
// "%{static:" "-static" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-shared"))
ADD_LINKER_ARG ("-shared");
else if (ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-static");
else
{
if (ARGV_CONTAINS_SINGLEARG ("-rdynamic"))
ADD_LINKER_ARG ("-export-dynamic");
if (!ARGV_CONTAINS_SINGLEARG ("-dynamic-linker"))
{
ADD_LINKER_ARG ("-dynamic-linker");
if (ARGV_CONTAINS_SINGLEARG ("-m32"))
ADD_LINKER_ARG ("/usr/lib/ldqnx.so.2");
else // not -m32
ADD_LINKER_ARG ("/usr/lib/ldqnx-64.so.2");
}
}
#else // !IS_TARGET_INTEL
// "%{static:" "-Bstatic" "} "
if (ARGV_CONTAINS_SINGLEARG ("-static")) ADD_LINKER_ARG ("-Bstatic");
// "%{shared} "
if (ARGV_CONTAINS_SINGLEARG ("-shared")) ADD_LINKER_ARG ("-shared");
// "%{symbolic:" "-Bsymbolic" "} "
if (ARGV_CONTAINS_SINGLEARG ("-symbolic")) ADD_LINKER_ARG ("-Bsymbolic");
// "%{G:" "-G" "} "
if (ARGV_CONTAINS_SINGLEARG ("-G")) ADD_LINKER_ARG ("-G");
// "%{MAP:" "-Map mapfile" "} "
if (ARGV_CONTAINS_SINGLEARG ("-MAP"))
{
ADD_LINKER_ARG ("-Map");
ADD_LINKER_ARG ("mapfile");
}
// "%{!shared: "
// "%{!static: "
// "%{rdynamic:" "-export-dynamic" "}"
// "} "
// "--dynamic-linker /usr/lib/ldqnx-64.so.2"
// "} "
if (!ARGV_CONTAINS_SINGLEARG ("-shared"))
{
if (!ARGV_CONTAINS_SINGLEARG ("-static") && ARGV_CONTAINS_SINGLEARG ("-rdynamic"))
ADD_LINKER_ARG ("-export-dynamic");
ADD_LINKER_ARG ("--dynamic-linker");
ADD_LINKER_ARG ("/usr/lib/ldqnx-64.so.2");
}
// "-X "
ADD_LINKER_ARG ("-X");
// "%{EB:" "-EB" "} "
// "%{!EB:" "-EL" "} "
if (ARGV_CONTAINS_SINGLEARG ("-EB"))
ADD_LINKER_ARG ("-EB");
else
ADD_LINKER_ARG ("-EL");
// "%{EL:" "-EL" "} "
if (ARGV_CONTAINS_SINGLEARG ("-EL"))
ADD_LINKER_ARG ("-EL");
#endif // IS_TARGET_INTEL
// "%{static|shared|r:;!no-pie:" "-pie" "} "
if (ARGV_CONTAINS_SINGLEARG ("-static") || !ARGV_CONTAINS_SINGLEARG ("-shared") || !ARGV_CONTAINS_SINGLEARG ("-r"))
; // nothing to do
else if (!ARGV_CONTAINS_SINGLEARG ("-no-pie"))
ADD_LINKER_ARG ("-pie");
// "%{fuse-ld=*:" "-fuse-ld=%*" "} "
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strncmp (argv
[arg_index
], "-fuse-ld=", 9) == 0) && ((argv
[arg_index
])[9] != 0))
ADD_LINKER_ARG (argv[arg_index]);
// "%{gz|gz=zlib:" "--compress-debug-sections=zlib" "} "
// "%{gz=none:" "--compress-debug-sections=none" "} "
// "%{gz=zlib-gnu:" "--compress-debug-sections=zlib-gnu" "} "
if (ARGV_CONTAINS_SINGLEARG ("-gz") || ARGV_CONTAINS_SINGLEARG ("-gz=zlib"))
ADD_LINKER_ARG ("--compress-debug-sections=zlib");
if (ARGV_CONTAINS_SINGLEARG ("-gz=none"))
ADD_LINKER_ARG ("--compress-debug-sections=none");
if (ARGV_CONTAINS_SINGLEARG ("-gz=zlib-gnu"))
ADD_LINKER_ARG ("--compress-debug-sections=zlib-gnu");
// "%X "
// nothing to do (nothing was accumulated by %x previously)
// "%{o*} "
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strcmp (argv
[arg_index
], "-o") == 0) && (arg_index
+ 1 < argc
))
{
ADD_LINKER_ARG ("-o");
ADD_LINKER_ARG (argv[++arg_index]);
}
else if ((strncmp (argv
[arg_index
], "-o", 2) == 0) && ((argv
[arg_index
])[2] != 0))
ADD_LINKER_ARG (argv[arg_index]);
// "%{e*} "
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strcmp (argv
[arg_index
], "-e") == 0) && (arg_index
+ 1 < argc
))
{
ADD_LINKER_ARG ("-e");
ADD_LINKER_ARG (argv[++arg_index]);
}
else if ((strncmp (argv
[arg_index
], "-e", 2) == 0) && ((argv
[arg_index
])[2] != 0))
ADD_LINKER_ARG (argv[arg_index]);
// "%{N} "
// "%{n} "
// "%{r} "
// "%{s} "
// "%{t} "
if (ARGV_CONTAINS_SINGLEARG ("-N")) ADD_LINKER_ARG ("-N");
if (ARGV_CONTAINS_SINGLEARG ("-n")) ADD_LINKER_ARG ("-n");
if (ARGV_CONTAINS_SINGLEARG ("-r")) ADD_LINKER_ARG ("-r");
if (ARGV_CONTAINS_SINGLEARG ("-s")) ADD_LINKER_ARG ("-s");
if (ARGV_CONTAINS_SINGLEARG ("-t")) ADD_LINKER_ARG ("-t");
// "%{u*} "
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strcmp (argv
[arg_index
], "-u") == 0) && (arg_index
+ 1 < argc
))
{
ADD_LINKER_ARG ("-u");
ADD_LINKER_ARG (argv[++arg_index]);
}
else if ((strncmp (argv
[arg_index
], "-u", 2) == 0) && ((argv
[arg_index
])[2] != 0))
ADD_LINKER_ARG (argv[arg_index]);
// "%{z} "
// "%{Z} "
if (ARGV_CONTAINS_SINGLEARG ("-z")) ADD_LINKER_ARG ("-z");
if (ARGV_CONTAINS_SINGLEARG ("-Z")) ADD_LINKER_ARG ("-Z");
// "%{!nostdlib:"
// "%{!r:"
// "%{!nostartfiles:" "%S" "}"
// "}"
// "} "
if (!ARGV_CONTAINS_SINGLEARG ("-nostdlib") && !ARGV_CONTAINS_SINGLEARG ("-r") && !ARGV_CONTAINS_SINGLEARG ("-nostartfiles"))
{
#if IS_TARGET_INTEL // Intel variant
// "%{!shared: "
// "%{!symbolic: "
// "%{pg: "
// "%{m32:" " %${QNX_TARGET}/x86/lib/mcrt1.o%s" "} "
// "%{!m32:" " %${QNX_TARGET}/x86_64/lib/mcrt1.o%s" "}"
// "} "
// "%{!pg: "
// "%{p: "
// "%{m32:" " %${QNX_TARGET}/x86/lib/mcrt1.o%s" "} "
// "%{!m32:" " %${QNX_TARGET}/x86_64/lib/mcrt1.o%s" "}"
// "}"
// "} "
// "%{!p: "
// "%{!pg: "
// "%{m32:" " %${QNX_TARGET}/x86/lib/crt1%{!no-pie:S}.o%s" "} "
// "%{!m32:" " %${QNX_TARGET}/x86_64/lib/crt1.o%s" "}"
// "}"
// "} "
// "}"
// "} "
if (!ARGV_CONTAINS_SINGLEARG ("-shared") && !ARGV_CONTAINS_SINGLEARG ("-symbolic"))
{
if (ARGV_CONTAINS_SINGLEARG ("-pg") || ARGV_CONTAINS_SINGLEARG ("-p"))
{
if (ARGV_CONTAINS_SINGLEARG ("-m32"))
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86/lib/mcrt1.o"));
else
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86_64/lib/mcrt1.o"));
}
else // neither "-pg" nor "-p"
{
if (ARGV_CONTAINS_SINGLEARG ("-m32"))
ADD_LINKER_ARG (resolve_envvars (makefmtstr ("${QNX_TARGET}/x86/lib/crt1%s.o", ARGV_CONTAINS_SINGLEARG ("-no-pie") ? "" : "S")));
else
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86_64/lib/crt1.o"));
}
}
// "%{m32:" " %${QNX_TARGET}/x86/lib/crti.o%s" "} "
// "%{!m32:" " %${QNX_TARGET}/x86_64/lib/crti.o%s" "} "
if (ARGV_CONTAINS_SINGLEARG ("-m32"))
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86/lib/crti.o"));
else
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86_64/lib/crti.o"));
#else // !IS_TARGET_INTEL
// "%{!shared: %${QNX_TARGET}/aarch64%{EB:be}%{!EB:le}/lib/%{pg:m}%{p:m}crt1.o } "
if (ARGV_CONTAINS_SINGLEARG ("-shared"))
if (ARGV_CONTAINS_SINGLEARG ("-EB"))
ADD_LINKER_ARG (resolve_envvars (makefmtstr ("${QNX_TARGET}/aarch64be/lib/%scrt1.o", ARGV_CONTAINS_SINGLEARG ("-pg") || ARGV_CONTAINS_SINGLEARG ("-p") ? "m" : "")));
else
ADD_LINKER_ARG (resolve_envvars (makefmtstr ("${QNX_TARGET}/aarch64le/lib/%scrt1.o", ARGV_CONTAINS_SINGLEARG ("-pg") || ARGV_CONTAINS_SINGLEARG ("-p") ? "m" : "")));
// "%${QNX_TARGET}/aarch64%{EB:be}%{!EB:le}/lib/crti.o "
if (ARGV_CONTAINS_SINGLEARG ("-EB"))
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/aarch64be/lib/crti.o"));
else
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/aarch64le/lib/crti.o"));
#endif // IS_TARGET_INTEL
// "crtbegin.o%s "
ADD_LINKER_ARG (resolve_envvars ("${QNX_HOST}/usr/bin/../lib/gcc/" TARGET_TRIPLE "/" BASEVER "/crtbegin.o"));
}
// "%{static|no-pie|static-pie:" "} "
// nothing to do
// "%@{L*} "
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strcmp (argv
[arg_index
], "-L") == 0) && (arg_index
+ 1 < argc
))
{
ADD_LINKER_ARG ("-L");
ADD_LINKER_ARG (argv[++arg_index]);
}
else if ((strncmp (argv
[arg_index
], "-L", 2) == 0) && ((argv
[arg_index
])[2] != 0))
ADD_LINKER_ARG (argv[arg_index]);
// "%(mfwrap) "
// nothing to do (looks like the libmudflap wrap spec has been officially removed from this version of GCC, except for uCLinux)
// "-L${QNX_HOST}/usr/bin/../lib/gcc/" TARGET_TRIPLE "/" BASEVER
// "-L${QNX_HOST}/usr/bin/../lib/gcc"
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_HOST}/usr/bin/../lib/gcc/" TARGET_TRIPLE "/" BASEVER));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_HOST}/usr/bin/../lib/gcc"));
#if IS_TARGET_INTEL
// "%{m32: " \
// "-L %${QNX_TARGET}/x86/lib/gcc/" BASEVER " "
// "-L %${QNX_TARGET}/x86/lib "
// "-L %${QNX_TARGET}/x86/usr/lib "
// "-L %${QNX_TARGET}/x86/opt/lib "
// "-rpath-link %${QNX_TARGET}/x86/lib/gcc/" BASEVER ":%${QNX_TARGET}/x86/lib:%${QNX_TARGET}/x86/usr/lib:%${QNX_TARGET}/x86/opt/lib"
// "} "
// "%{!m32: "
// "-L %${QNX_TARGET}/x86_64/lib/gcc/" BASEVER " "
// "-L %${QNX_TARGET}/x86_64/lib "
// "-L %${QNX_TARGET}/x86_64/usr/lib "
// "-L %${QNX_TARGET}/x86_64/opt/lib "
// "-rpath-link %${QNX_TARGET}/x86_64/lib/gcc/" BASEVER ":%${QNX_TARGET}/x86_64/lib:%${QNX_TARGET}/x86_64/usr/lib:%${QNX_TARGET}/x86_64/opt/lib"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-m32"))
{
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/x86/lib/gcc/" BASEVER));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/x86/lib"));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/x86/usr/lib"));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/x86/opt/lib"));
ADD_LINKER_ARG ("-rpath-link");
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86/lib/gcc/" BASEVER ":${QNX_TARGET}/x86/lib" ":${QNX_TARGET}/x86/usr/lib" ":${QNX_TARGET}/x86/opt/lib"));
}
else
{
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/x86_64/lib/gcc/" BASEVER));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/x86_64/lib"));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/x86_64/usr/lib"));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/x86_64/opt/lib"));
ADD_LINKER_ARG ("-rpath-link");
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86_64/lib/gcc/" BASEVER ":${QNX_TARGET}/x86_64/lib:${QNX_TARGET}/x86_64/usr/lib:${QNX_TARGET}/x86_64/opt/lib"));
}
#else // !IS_TARGET_INTEL
// "-L %${QNX_TARGET}/aarch64%{EB:be}%{!EB:le}/lib/gcc/" BASEVER " "
// "-L %${QNX_TARGET}/aarch64%{EB:be}%{!EB:le}/lib "
// "-L %${QNX_TARGET}/aarch64%{EB:be}%{!EB:le}/usr/lib "
// "-L %${QNX_TARGET}/aarch64%{EB:be}%{!EB:le}/opt/lib "
if (ARGV_CONTAINS_SINGLEARG ("-EB"))
{
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/aarch64be/lib/gcc/" BASEVER));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/aarch64be/lib"));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/aarch64be/usr/lib"));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/aarch64be/opt/lib"));
ADD_LINKER_ARG ("-rpath-link");
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/aarch64be/lib/gcc/" BASEVER ":${QNX_TARGET}/aarch64be/lib" ":${QNX_TARGET}/aarch64be/usr/lib" ":${QNX_TARGET}/aarch64be/opt/lib"));
}
else
{
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/aarch64le/lib/gcc/" BASEVER));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/aarch64le/lib"));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/aarch64le/usr/lib"));
ADD_LINKER_ARG (resolve_envvars ("-L${QNX_TARGET}/aarch64le/opt/lib"));
ADD_LINKER_ARG ("-rpath-link");
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/aarch64le/lib/gcc/" BASEVER ":${QNX_TARGET}/aarch64le/lib" ":${QNX_TARGET}/aarch64le/usr/lib" ":${QNX_TARGET}/aarch64le/opt/lib"));
}
#endif // IS_TARGET_INTEL
// "%{!nostdlib:"
// "%{!r:"
// "%{fvtable-verify=std:" " -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end" "} "
// "%{fvtable-verify=preinit:" " -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end" "}"
// "}"
// "} "
if (!ARGV_CONTAINS_SINGLEARG ("-nostdlib") && !ARGV_CONTAINS_SINGLEARG ("-r"))
{
if (ARGV_CONTAINS_SINGLEARG ("-fvtable-verify=std") || ARGV_CONTAINS_SINGLEARG ("-fvtable-verify=preinit"))
{
ADD_LINKER_ARG ("-lvtv");
ADD_LINKER_ARG ("-u_vtable_map_vars_start");
ADD_LINKER_ARG ("-u_vtable_map_vars_end");
}
// "%{!nodefaultlibs:"
if (!ARGV_CONTAINS_SINGLEARG ("-nodefaultlibs"))
{
#if IS_TARGET_INTEL
// "%{%:sanitize(address):" "} "
// "%{%:sanitize(hwaddress):" "} "
// "%{%:sanitize(thread):" "} "
// "%{%:sanitize(leak):" "}"
/* TODO */
#else // !IS_TARGET_INTEL
// "%{%:sanitize(address):"
// "%{!shared:" "libasan_preinit%O%s" "} "
// "%{static-libasan:" "%{!shared:" "-Bstatic --whole-archive -lasan --no-whole-archive -Bdynamic" "}" "}"
// "%{!static-libasan:" "-lasan" "}"
// "} "
// "%{%:sanitize(hwaddress):"
// "%{static-libhwasan:" "%{!shared:" "-Bstatic --whole-archive -lhwasan --no-whole-archive -Bdynamic" "}" "}"
// "%{!static-libhwasan:" "-lhwasan" "}"
// "} "
// "%{%:sanitize(thread):"
// "%{!shared:" "libtsan_preinit%O%s" "} "
// "%{static-libtsan:" "%{!shared:" "-Bstatic --whole-archive -ltsan --no-whole-archive -Bdynamic" "}" "}"
// "%{!static-libtsan:" "-ltsan" "}"
// "} "
// "%{%:sanitize(leak):"
// "%{!shared:" "liblsan_preinit%O%s" "} "
// "%{static-liblsan:" "%{!shared:" "-Bstatic --whole-archive -llsan --no-whole-archive -Bdynamic" "}" "}"
// "%{!static-liblsan:" "-llsan" "}"
// "}"
/* TODO */
#endif // IS_TARGET_INTEL
}
}
// "%o "
for (arg_index = 0; arg_index < oarg_count; arg_index++)
ADD_LINKER_ARG (oargs[arg_index]);
// QUOTE FROM https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html -- BEGIN
// "The following example inserts the link_gomp spec if the last -ftree-parallelize-loops= option given on the command line is greater than 1:
// %{ %:gt(%{ftree-parallelize-loops=*:%*} 1):%:include(libgomp.spec)%(link_gomp)}"
// QUOTE FROM https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html -- END
// "%{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):%:include(libgomp.spec)%(link_gomp)} "
// nothing to do (link_gomp is an empty string in this GCC)
// "%{fgnu-tm:" "%:include(libitm.spec)%(link_itm)" "} "
if (ARGV_CONTAINS_SINGLEARG ("-fgnu-tm"))
{
ADD_LINKER_ARG ("-litm");
ADD_LINKER_ARG ("-lpthread"); // FIXME: possibly wrong
}
// "%(mflib) "
// nothing to do (looks like the mflib spec has been officially removed from this version of GCC)
// "%{fsplit-stack: " "--wrap=pthread_create" "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsplit-stack"))
ADD_LINKER_ARG ("--wrap=pthread_create");
// "%{fprofile-arcs|fprofile-generate*|coverage:" "-lgcov" "} "
if (ARGV_CONTAINS_SINGLEARG ("-fprofile-arcs") || ARGV_CONTAINS_ARGWITHVAL ("-fprofile-generate") || ARGV_CONTAINS_SINGLEARG ("-coverage"))
ADD_LINKER_ARG ("-lgcov");
if (!ARGV_CONTAINS_SINGLEARG ("-nostdlib") && !ARGV_CONTAINS_SINGLEARG ("-r"))
{
if (!ARGV_CONTAINS_SINGLEARG ("-nodefaultlibs"))
{
#if IS_TARGET_INTEL
// "%{%:sanitize(address):"
// "%{static-libasan:" "-Bstatic" "} "
// "-lasan "
// "%{static-libasan:" "-Bdynamic" "} "
// "%{static-libasan|static:" "%:include(libsanitizer.spec)%(link_libasan)" "} "
// "%{static:" "%ecannot specify -static with -fsanitize=address" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=address"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-libasan")) ADD_LINKER_ARG ("-Bstatic");
ADD_LINKER_ARG ("-lasan");
if (ARGV_CONTAINS_SINGLEARG ("-static-libasan")) ADD_LINKER_ARG ("-Bdynamic");
if (ARGV_CONTAINS_SINGLEARG ("-static-libasan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to libasan on QNX
if (ARGV_CONTAINS_SINGLEARG ("-static"))
{
fprintf (stderr
, "%s: error: cannot specify -static with -fsanitize=address\n", argv
[0]);
}
}
// "%{%:sanitize(hwaddress):"
// "%{static-libhwasan:" "-Bstatic" "} "
// "-lhwasan "
// "%{static-libhwasan:" "-Bdynamic" "} "
// "%{static-libhwasan|static:" "%:include(libsanitizer.spec)%(link_libhwasan)" "} "
// "%{static:" "%ecannot specify -static with -fsanitize=hwaddress" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=hwaddress"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-libhwasan")) ADD_LINKER_ARG ("-Bstatic");
ADD_LINKER_ARG ("-lhwasan");
if (ARGV_CONTAINS_SINGLEARG ("-static-libhwasan")) ADD_LINKER_ARG ("-Bdynamic");
if (ARGV_CONTAINS_SINGLEARG ("-static-libhwasan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to libhwasan on QNX
if (ARGV_CONTAINS_SINGLEARG ("-static"))
{
fprintf (stderr
, "%s: error: cannot specify -static with -fsanitize=hwaddress\n", argv
[0]);
}
}
// "%{%:sanitize(thread):"
// "%{static-libtsan:" "-Bstatic" "} "
// "-ltsan "
// "%{static-libtsan:" "-Bdynamic" "} "
// "%{static-libtsan|static:" "%:include(libsanitizer.spec)%(link_libtsan)" "} "
// "%{static:" "%ecannot specify -static with -fsanitize=thread" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=thread"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-libtsan")) ADD_LINKER_ARG ("-Bstatic");
ADD_LINKER_ARG ("-ltsan");
if (ARGV_CONTAINS_SINGLEARG ("-static-libtsan")) ADD_LINKER_ARG ("-Bdynamic");
if (ARGV_CONTAINS_SINGLEARG ("-static-libtsan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to libtsan on QNX
if (ARGV_CONTAINS_SINGLEARG ("-static"))
{
fprintf (stderr
, "%s: error: cannot specify -static with -fsanitize=thread\n", argv
[0]);
}
}
// "%{%:sanitize(undefined):"
// "%{static-libubsan:" "-Bstatic" "} "
// "-lubsan "
// "%{static-libubsan:" "-Bdynamic" "} "
// "%{static-libubsan|static:" "%:include(libsanitizer.spec)%(link_libubsan)" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=undefined"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-libubsan")) ADD_LINKER_ARG ("-Bstatic");
ADD_LINKER_ARG ("-lubsan");
if (ARGV_CONTAINS_SINGLEARG ("-static-libubsan")) ADD_LINKER_ARG ("-Bdynamic");
if (ARGV_CONTAINS_SINGLEARG ("-static-libubsan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to libubsan on QNX
}
// "%{%:sanitize(leak):"
// "%{static-liblsan:" "-Bstatic" "} "
// "-llsan "
// "%{static-liblsan:" "-Bdynamic" "} "
// "%{static-liblsan|static:" "%:include(libsanitizer.spec)%(link_liblsan)" "}"
// "}"
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=leak"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-liblsan")) ADD_LINKER_ARG ("-Bstatic");
ADD_LINKER_ARG ("-llsan");
if (ARGV_CONTAINS_SINGLEARG ("-static-liblsan")) ADD_LINKER_ARG ("-Bdynamic");
if (ARGV_CONTAINS_SINGLEARG ("-static-liblsan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to liblsan on QNX
}
#else // !IS_TARGET_INTEL
// "%{%:sanitize(address): "
// "%{static-libasan|static:" "%:include(libsanitizer.spec)%(link_libasan)" "} "
// "%{static:" "%ecannot specify -static with -fsanitize=address" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=address"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-libasan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to libasan on QNX
if (ARGV_CONTAINS_SINGLEARG ("-static"))
{
fprintf (stderr
, "%s: error: cannot specify -static with -fsanitize=address\n", argv
[0]);
}
}
// "%{%:sanitize(hwaddress): "
// "%{static-libhwasan|static:" "%:include(libsanitizer.spec)%(link_libhwasan)" "} "
// "%{static:" "%ecannot specify -static with -fsanitize=hwaddress" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=hwaddress"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-libhwasan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to libhwasan on QNX
if (ARGV_CONTAINS_SINGLEARG ("-static"))
{
fprintf (stderr
, "%s: error: cannot specify -static with -fsanitize=hwaddress\n", argv
[0]);
}
}
// "%{%:sanitize(thread): "
// "%{static-libtsan|static:" "%:include(libsanitizer.spec)%(link_libtsan)" "} "
// "%{static:" "%ecannot specify -static with -fsanitize=thread" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=thread"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-libtsan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to libtsan on QNX
if (ARGV_CONTAINS_SINGLEARG ("-static"))
{
fprintf (stderr
, "%s: error: cannot specify -static with -fsanitize=thread\n", argv
[0]);
}
}
// "%{%:sanitize(undefined):"
// "%{static-libubsan:" "-Bstatic" "} "
// "-lubsan "
// "%{static-libubsan:" "-Bdynamic" "} "
// "%{static-libubsan|static:" "%:include(libsanitizer.spec)%(link_libubsan)" "}"
// "} "
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=undefined"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-libubsan")) ADD_LINKER_ARG ("-Bstatic");
ADD_LINKER_ARG ("-lubsan");
if (ARGV_CONTAINS_SINGLEARG ("-static-libubsan")) ADD_LINKER_ARG ("-Bdynamic");
if (ARGV_CONTAINS_SINGLEARG ("-static-libubsan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to libubsan on QNX
}
// "%{%:sanitize(leak): "
// "%{static-liblsan|static:" "%:include(libsanitizer.spec)%(link_liblsan)" "}"
// "}"
if (ARGV_CONTAINS_SINGLEARG ("-fsanitize=leak"))
{
if (ARGV_CONTAINS_SINGLEARG ("-static-liblsan") || ARGV_CONTAINS_SINGLEARG ("-static"))
ADD_LINKER_ARG ("-lm"); // only known dependency to liblsan on QNX
}
#endif // IS_TARGET_INTEL
// "%{fstack-protector|fstack-protector-all|fstack-protector-strong|fstack-protector-explicit:}"
// nothing to do
// "%(link_gcc_c_sequence)"
#if IS_TARGET_INTEL
// "%{static|static-libgcc:" "-lgcc -lgcc_eh" "}"
if (ARGV_CONTAINS_SINGLEARG ("-static") || ARGV_CONTAINS_SINGLEARG ("-static-libgcc"))
{
ADD_LINKER_ARG ("-lgcc");
ADD_LINKER_ARG ("-lgcc_eh");
}
// "%{!static:"
// "%{!static-libgcc:"
// "%{!shared:"
// "%{!shared-libgcc:" "-lgcc -lgcc_eh" "}"
// "%{shared-libgcc:" "-lgcc_s -lgcc" "}"
// "}"
// "%{shared:"
// "%{shared-libgcc:" "-lgcc_s" "}"
// "%{!shared-libgcc:" "-lgcc" "}"
// "}"
// "}"
// "}"
if (!ARGV_CONTAINS_SINGLEARG ("-static") && !ARGV_CONTAINS_SINGLEARG ("-static-libgcc"))
{
if (!ARGV_CONTAINS_SINGLEARG ("-shared"))
{
if (ARGV_CONTAINS_SINGLEARG ("-shared-libgcc"))
{
ADD_LINKER_ARG ("-lgcc_s");
ADD_LINKER_ARG ("-lgcc");
}
else
{
ADD_LINKER_ARG ("-lgcc");
ADD_LINKER_ARG ("-lgcc_eh");
}
}
else if (ARGV_CONTAINS_SINGLEARG ("-shared-libgcc"))
ADD_LINKER_ARG ("-lgcc_s");
else
ADD_LINKER_ARG ("-lgcc");
}
// "%{!nolibc:"
if (!ARGV_CONTAINS_SINGLEARG ("-nolibc"))
{
// "%{!symbolic:"
// " -lc"
// " %{shared:" "-lcS" "}"
// " %{!no-pie:" "-lcS" "}"
// " %{no-pie:" "-lc" "}"
// "} "
if (!ARGV_CONTAINS_SINGLEARG ("-symbolic"))
{
ADD_LINKER_ARG ("-lc");
if (ARGV_CONTAINS_SINGLEARG ("-shared") || !ARGV_CONTAINS_SINGLEARG ("-no-pie"))
ADD_LINKER_ARG ("-lcS");
}
// "%{static|static-libgcc:" "-lgcc -lgcc_eh" "}"
if (ARGV_CONTAINS_SINGLEARG ("-static") || ARGV_CONTAINS_SINGLEARG ("-static-libgcc"))
{
ADD_LINKER_ARG ("-lgcc");
ADD_LINKER_ARG ("-lgcc_eh");
}
// "%{!static:"
// "%{!static-libgcc:"
// "%{!shared:"
// "%{!shared-libgcc:" "-lgcc -lgcc_eh" "}"
// "%{shared-libgcc:" "-lgcc_s -lgcc" "}"
// "}"
// "%{shared:"
// "%{shared-libgcc:" "-lgcc_s" "}"
// "%{!shared-libgcc:" "-lgcc" "}"
// "}"
// "}"
// "
if (!ARGV_CONTAINS_SINGLEARG ("-static") && !ARGV_CONTAINS_SINGLEARG ("-static-libgcc"))
{
if (!ARGV_CONTAINS_SINGLEARG ("-shared"))
{
if (ARGV_CONTAINS_SINGLEARG ("-shared-libgcc"))
{
ADD_LINKER_ARG ("-lgcc_s");
ADD_LINKER_ARG ("-lgcc");
}
else
{
ADD_LINKER_ARG ("-lgcc");
ADD_LINKER_ARG ("-lgcc_eh");
}
}
else if (ARGV_CONTAINS_SINGLEARG ("-shared-libgcc"))
ADD_LINKER_ARG ("-lgcc_s");
else
ADD_LINKER_ARG ("-lgcc");
}
}
#else // !IS_TARGET_INTEL
// "%{static|static-pie:" "--start-group" "} "
if (ARGV_CONTAINS_SINGLEARG ("-static") || ARGV_CONTAINS_SINGLEARG ("-static-pie"))
ADD_LINKER_ARG ("--start-group");
// "%{static|static-libgcc|static-pie:" "-lgcc -lgcc_eh" "}"
if (ARGV_CONTAINS_SINGLEARG ("-static") || ARGV_CONTAINS_SINGLEARG ("-static-libgcc") || ARGV_CONTAINS_SINGLEARG ("-static-pie"))
{
ADD_LINKER_ARG ("-lgcc");
ADD_LINKER_ARG ("-lgcc_eh");
}
// "%{!static:"
// "%{!static-libgcc:"
// "%{!static-pie:"
// "%{!shared-libgcc:" "-lgcc --push-state --as-needed -lgcc_s --pop-state" "}"
// "%{shared-libgcc:" "-lgcc_s%{!shared: -lgcc}" "}"
// "}"
// "}"
// "}"
if (!ARGV_CONTAINS_SINGLEARG ("-static") && !ARGV_CONTAINS_SINGLEARG ("-static-libgcc") && !ARGV_CONTAINS_SINGLEARG ("-static-pie"))
{
if (!ARGV_CONTAINS_SINGLEARG ("-shared-libgcc"))
{
ADD_LINKER_ARG ("-lgcc");
ADD_LINKER_ARG ("--push-state");
ADD_LINKER_ARG ("--as-needed");
ADD_LINKER_ARG ("-lgcc_s");
ADD_LINKER_ARG ("--pop-state");
}
else
{
ADD_LINKER_ARG ("-lgcc_s");
if (!ARGV_CONTAINS_SINGLEARG ("-shared"))
ADD_LINKER_ARG ("-lgcc");
}
}
// "%{!nolibc:"
// "%{!symbolic:"
// " -lc"
// " %{shared:" "-lcS" "}"
// " %{!no-pie:" "-lcS" "}"
// " %{no-pie:" "-lc" "}"
// "} "
// "} "
if (!ARGV_CONTAINS_SINGLEARG ("-nolibc") && !ARGV_CONTAINS_SINGLEARG ("-symbolic"))
{
ADD_LINKER_ARG ("-lc");
if (ARGV_CONTAINS_SINGLEARG ("-shared") || !ARGV_CONTAINS_SINGLEARG ("-no-pie"))
ADD_LINKER_ARG ("-lcS");
}
// "%{static|static-pie:" "--end-group" "}"
if (ARGV_CONTAINS_SINGLEARG ("-static") || ARGV_CONTAINS_SINGLEARG ("-static-pie"))
ADD_LINKER_ARG ("--end-group");
// "%{!static:"
// "%{!static-pie:"
else
{
// "%{static|static-libgcc|static-pie:" "-lgcc -lgcc_eh" "}"
if (ARGV_CONTAINS_SINGLEARG ("-static-libgcc"))
{
ADD_LINKER_ARG ("-lgcc");
ADD_LINKER_ARG ("-lgcc_eh");
}
// "%{!static:"
// "%{!static-libgcc:"
// "%{!static-pie:"
// "%{!shared-libgcc:" "-lgcc --push-state --as-needed -lgcc_s --pop-state" "}"
// "%{shared-libgcc:" "-lgcc_s%{!shared: -lgcc}" "}"
// "}"
// "}"
// "}"
if (!ARGV_CONTAINS_SINGLEARG ("-static") && !ARGV_CONTAINS_SINGLEARG ("-static-libgcc") && !ARGV_CONTAINS_SINGLEARG ("-static-pie"))
{
if (!ARGV_CONTAINS_SINGLEARG ("-shared-libgcc"))
{
ADD_LINKER_ARG ("-lgcc");
ADD_LINKER_ARG ("--push-state");
ADD_LINKER_ARG ("--as-needed");
ADD_LINKER_ARG ("-lgcc_s");
ADD_LINKER_ARG ("--pop-state");
}
else
{
ADD_LINKER_ARG ("-lgcc_s");
if (!ARGV_CONTAINS_SINGLEARG ("-shared"))
ADD_LINKER_ARG ("-lgcc");
}
}
}
#endif // IS_TARGET_INTEL
was_cpluspluslib_included = false;
for (arg_index = 0; arg_index < larg_count; arg_index++)
{
ADD_LINKER_ARG (largs[arg_index]);
if (((arg
= strstr (largs
[arg_index
], "c++")) != NULL
) && (arg
[3] == 0))
was_cpluspluslib_included = true; // catches -lstdc++ and -lc++
}
if (!was_cpluspluslib_included)
{
if (strstr (argv
[0], "g++") != NULL
)
ADD_LINKER_ARG ("-lc++"); // if clang invoked us as g++ to link stuff it probably means we need a C++ library
else
{
requires_libcplusplus = false; // figure it out the hard way: if we see a C++ mangled symbol name in one of the object files, it means we need a C++ library
for (arg_index = 0; !requires_libcplusplus && (arg_index < oarg_count); arg_index++)
{
fp
= fopen (oargs
[arg_index
], "rb");
if (fp == NULL) continue; // unopenable object file, should error out perhaps?
while (!requires_libcplusplus
&& ((read_count
= fread (filebuf
, 1, sizeof (filebuf
), fp
)) > 0))
for (byte_index = 0; !requires_libcplusplus && (byte_index < read_count - 8); byte_index++)
if (memcmp (&filebuf
[byte_index
], ".text._Z", 8) == 0)
requires_libcplusplus = true;
}
if (requires_libcplusplus)
ADD_LINKER_ARG ("-lc++"); // we did it the hard way
}
}
}
// "%{!nostartfiles:" "%E" "}"
if (!ARGV_CONTAINS_SINGLEARG ("-nostartfiles"))
{
ADD_LINKER_ARG (resolve_envvars ("${QNX_HOST}/usr/bin/../lib/gcc/" TARGET_TRIPLE "/" BASEVER "/crtend.o"));
#if IS_TARGET_INTEL
if (ARGV_CONTAINS_SINGLEARG ("-m32"))
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86/lib/crtn.o"));
else
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/x86_64/lib/crtn.o"));
#else // !IS_TARGET_INTEL
if (ARGV_CONTAINS_SINGLEARG ("-EB"))
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/aarch64be/lib/crtn.o"));
else
ADD_LINKER_ARG (resolve_envvars ("${QNX_TARGET}/aarch64le/lib/crtn.o"));
#endif // IS_TARGET_INTEL
}
}
// "%{T*}"
for (arg_index = 1; arg_index < argc; arg_index++)
if ((strcmp (argv
[arg_index
], "-T") == 0) && (arg_index
+ 1 < argc
))
{
ADD_LINKER_ARG ("-T");
ADD_LINKER_ARG (argv[++arg_index]);
}
else if ((strncmp (argv
[arg_index
], "-T", 2) == 0) && ((argv
[arg_index
])[2] != 0))
ADD_LINKER_ARG (argv[arg_index]);
if (ARGV_CONTAINS_SINGLEARG
("-v") || (getenv ("DEBUG_LINKER_DRIVER") != NULL
))
{
fputs ("LINKER DRIVER: CONSTRUCTED COMMAND LINE BEGIN\n", stderr
);
for (arg_index = 0; arg_index < linker_argc; arg_index++)
fprintf (stderr
, "%s%s%s\n", (arg_index
> 0 ? "\t" : ""), linker_argv
[arg_index
], ((size_t) arg_index
+ 1 < linker_argc
? " \\" : ""));
fputs ("LINKER DRIVER: CONSTRUCTED COMMAND LINE END\n", stderr
);
}
execvpe (LINKER, linker_argv, envp);
return (1); // unreachable code
}