+ werror (E_FILE_OPEN_ERR, scratchFileName);
+ }
+ MSVC_style(options.vc_err_style);
+ if(options.use_stdout) dup2(STDOUT_FILENO, STDERR_FILENO);
+
+ return 0;
+}
+
+/*-----------------------------------------------------------------*/
+/* linkEdit : - calls the linkage editor with options */
+/*-----------------------------------------------------------------*/
+static void
+linkEdit (char **envp)
+{
+ FILE *lnkfile;
+ char *segName, *c;
+ int system_ret;
+ const char *s;
+
+
+ if(port->linker.needLinkerScript)
+ {
+ char out_fmt;
+
+ switch (options.out_fmt)
+ {
+ case 0:
+ out_fmt = 'i'; /* Intel hex */
+ break;
+ case 1:
+ out_fmt = 's'; /* Motorola S19 */
+ break;
+ case 2:
+ out_fmt = 't'; /* Elf */
+ break;
+ default:
+ out_fmt = 'i';
+ }
+
+ /* first we need to create the <filename>.lnk file */
+ SNPRINTF (scratchFileName, sizeof(scratchFileName),
+ "%s.lnk", dstFileName);
+ if (!(lnkfile = fopen (scratchFileName, "w")))
+ {
+ werror (E_FILE_OPEN_ERR, scratchFileName);
+ exit (1);
+ }
+
+ if (TARGET_IS_Z80 || TARGET_IS_GBZ80)
+ {
+ fprintf (lnkfile, "--\n-m\n-j\n-x\n-%c %s\n",
+ out_fmt, dstFileName);
+ }
+ else /*For all the other ports. Including pics???*/
+ {
+ fprintf (lnkfile, "-myux%c\n", out_fmt);
+ if(options.pack_iram)
+ fprintf (lnkfile, "-Y\n");
+ }
+
+ if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
+ {
+ /* if iram size specified */
+ if (options.iram_size)
+ fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
+
+ /* if stack size specified*/
+ if(options.stack_size)
+ fprintf (lnkfile, "-A 0x%02x\n", options.stack_size);
+
+ /* if xram size specified */
+ if (options.xram_size_set)
+ fprintf (lnkfile, "-v 0x%04x\n", options.xram_size);
+
+ /* if code size specified */
+ if (options.code_size)
+ fprintf (lnkfile, "-w 0x%04x\n", options.code_size);
+
+ if (options.debug)
+ fprintf (lnkfile, "-z\n");
+ }
+
+#define WRITE_SEG_LOC(N, L) \
+ segName = Safe_strdup(N); \
+ c = strtok(segName, " \t"); \
+ fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
+ if (segName) { Safe_free(segName); }
+
+ if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
+ {
+
+ /* code segment start */
+ WRITE_SEG_LOC (CODE_NAME, options.code_loc);
+
+ /* data segment start. If zero, the linker chooses
+ the best place for data */
+ if(options.data_loc)
+ {
+ WRITE_SEG_LOC (DATA_NAME, options.data_loc);
+ }
+
+ /* xdata start */
+ WRITE_SEG_LOC (XDATA_NAME, options.xdata_loc);
+
+ /* indirect data */
+ if (IDATA_NAME)
+ {
+ WRITE_SEG_LOC (IDATA_NAME, options.idata_loc);
+ }
+
+ /* bit segment start */
+ WRITE_SEG_LOC (BIT_NAME, 0);
+
+ /* stack start */
+ if ( (options.stack_loc) && (options.stack_loc<0x100) )
+ {
+ WRITE_SEG_LOC ("SSEG", options.stack_loc);
+ }
+ }
+ else /*For the z80, gbz80*/
+ {
+ WRITE_SEG_LOC ("_CODE", options.code_loc);
+ WRITE_SEG_LOC ("_DATA", options.data_loc);
+ }
+
+ /* If the port has any special linker area declarations, get 'em */
+ if (port->extraAreas.genExtraAreaLinkOptions)
+ {
+ port->extraAreas.genExtraAreaLinkOptions(lnkfile);
+ }
+
+ /* add the extra linker options */
+ fputStrSet(lnkfile, linkOptionsSet);
+
+ /* command line defined library paths if specified */
+ for (s = setFirstItem(libPathsSet); s != NULL; s = setNextItem(libPathsSet))
+ fprintf (lnkfile, "-k %s\n", s);
+
+ /* standard library path */
+ if (!options.nostdlib)
+ {
+ if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80 || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
+ {
+ switch (options.model)
+ {
+ case MODEL_SMALL:
+ c = "small";
+ break;
+ case MODEL_LARGE:
+ c = "large";
+ break;
+ case MODEL_FLAT24:
+ /* c = "flat24"; */
+ if (TARGET_IS_DS390)
+ {
+ c = "ds390";
+ }
+ else if (TARGET_IS_DS400)
+ {
+ c = "ds400";
+ }
+ else
+ {
+ fprintf(stderr,
+ "Add support for your FLAT24 target in %s @ line %d\n",
+ __FILE__, __LINE__);
+ exit(-1);
+ }
+ break;
+ case MODEL_PAGE0:
+ c = "xa51";
+ break;
+ default:
+ werror (W_UNKNOWN_MODEL, __FILE__, __LINE__);
+ c = "unknown";
+ break;
+ }
+ }
+ else /*for the z80, gbz80*/
+ {
+ if (TARGET_IS_HC08)
+ c = "hc08";
+ else if (TARGET_IS_Z80)
+ c = "z80";
+ else
+ c = "gbz80";
+ }
+ for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
+ mfprintf (lnkfile, getRuntimeVariables(), "-k %s{sep}%s\n", s, c);
+ }
+
+ /* command line defined library files if specified */
+ for (s = setFirstItem(libFilesSet); s != NULL; s = setNextItem(libFilesSet))
+ fprintf (lnkfile, "-l %s\n", s);