Guidance for cross-compiling complex C programs (like Doom) to run on custom MIPS virtual machines or emulators. This skill should be used when tasked with building software for non-standard MIPS targets that use custom syscall interfaces, require freestanding compilation (-nostdlib), or involve running binaries in JavaScript/custom VM environments.
This skill provides guidance for cross-compiling complex C codebases (such as Doom) to run on custom MIPS virtual machines or emulators. The primary challenge is bridging the gap between standard C library expectations and a custom runtime environment with its own syscall interface, memory layout, and instruction support.
Before writing any code, thoroughly analyze the target VM to understand its requirements:
Read the VM source code to extract the exact syscall interface:
Verify the target architecture details by examining the VM source:
From the VM source, extract:
Check for instruction limitations:
Before compiling, scan all source files to identify required headers:
# Find all included headers
grep -rh '#include' src/ | sort -u
Create stub headers for ALL identified system headers upfront rather than reactively fixing one error at a time. Common headers needed for complex C programs:
<stdio.h>, <stdlib.h>, <string.h>, <stdint.h><limits.h>, <stddef.h>, <stdarg.h><ctype.h>, <math.h>, <time.h><errno.h>, <assert.h>, <fcntl.h><unistd.h>, <sys/types.h>, <sys/stat.h>Create a comprehensive stdlib replacement with COMPLETE implementations:
Critical functions that must work correctly (not return 0 or stub):
printf, fprintf, sprintf, snprintf - Used for debug output and parsingsscanf, fscanf - Used for configuration parsingmalloc, free, realloc - Memory managementmemcpy, memset, memmove, strlen, strcmp, strcpy - String/memory operationsatoi, atol, strtol - Number parsingfopen, fread, fwrite, fclose, fseek, ftell - File I/OWarning signs of incomplete implementations:
Create a linker script that matches the VM's expectations:
.text, .data, .bss, .rodata placementAfter implementing syscalls, verify each one works:
// Test write syscall
const char *msg = "Syscall test\n";
write(1, msg, strlen(msg));
// Test read syscall
char buf[100];
int n = read(0, buf, sizeof(buf));
// Test file operations
int fd = open("/path/to/file", O_RDONLY);
// etc.
Confirm the entry point matches:
# Check the ELF entry point
mips-*-readelf -h output.elf | grep Entry
# Verify symbol is at expected address
mips-*-nm output.elf | grep __start
Generate and use map files to debug crashes:
# Generate map file during linking
-Wl,-Map=output.map
# When program terminates at specific PC, find the function:
grep "0x40b7b0" output.map
Test components in isolation before full integration:
Problem: Linking soft-float compiled code with hard-float libgcc produces warnings:
PyTorch深度学习模式与最佳实践,用于构建稳健、高效且可复现的训练流程、模型架构和数据加载。