SPO600 2025 Winter Project - Stage 1: Trouble Shooting & Reflection (part2)
Introduction
In Part 1, I explained in detail how to create a basic GCC pass, covering the steps to define, register, and integrate a custom pass into the GCC compilation pipeline.
In this post, I will focus on the troubleshooting process—the challenges I encountered while implementing my custom pass, the debugging steps I took, and how I resolved each issue. I will also include some reflections on the process, highlighting key lessons learned and things I would do differently if I were to start over.
Troubleshooting GCC Pass Implementation Issues
1️⃣ undefined reference to make_tree_skim(gcc::context*) Error
Issue:
While building GCC, the following undefined reference error occurred:
/usr/bin/ld: passes.o: in function gcc::pass_manager::pass_manager(gcc::context*): undefined reference to make_tree_skim(gcc::context*)
Root Cause:
• The make_tree_skim() function was defined, but the linker failed to reference it properly.
• The tree-skim.o file was added to Makefile.in, but GCC did not include it in the build.
• libbackend.a did not properly link tree-skim.o, causing the symbol to be missing.
Solution:
1. Check if tree-skim.o is correctly built
• Run nm libbackend.a | grep make_tree_skim to verify if the symbol exists.
• If it shows U (undefined), the symbol is not linked properly.
2. Ensure tree-skim.o is added to Makefile.in
• Open Makefile.in (nano Makefile.in) and check if tree-skim.o is listed under OBJS =.
• If not present, add it and re-run make.
3. Verify passes.def registration
• Ensure NEXT_PASS(pass_skim); is placed correctly in passes.def.
4. Declare make_pass_skim() in tree-pass.h
• Add this line to tree-pass.h: extern gimple_opt_pass *make_pass_skim(gcc::context *ctxt);
5. Regenerate Makefile and rebuild
rm -rf ~/gcc-build-001/Makefile
../git/gcc/configure --prefix=$HOME/gcc-test-001
time make -j20 |& tee rebuild.log
• Removing Makefile ensures the changes are correctly applied.
2️⃣ Build Failure Due to execute() Function Bug
Issue:
The execute() function inside the pass caused the GCC build to fail.
Root Cause:
• The gate() function always returned true, triggering execute(), which had an unexpected bug.
• A NULL reference inside execute() caused xgcc (the bootstrap compiler) to fail.
Solution:
1. Temporary Fix:
• Modify the gate() function to return false to prevent execute() from running:
bool gate(function *fun) final override {
return false; // Temporarily disable execution
}
• This allows the build to succeed, but the issue in execute() still needs fixing.
2. Add NULL Checks Inside Loops
• Ensure basic_block or gimple_stmt_iterator is not NULL before accessing:
FOR_EACH_BB_FN(bb, fun) {
if (!bb) continue; // Skip null basic blocks
}
3. Check for dump_file Before Writing Logs
• Prevent crashes due to invalid file references:
if (dump_file) {
fprintf(dump_file, "=== Function: %s ===\n", function_name(fun));
}
3️⃣ Incorrect Environment Variables Causing cp: permission denied Error
Issue:
• After modifying environment variables, unexpected errors occurred while running make.
• The cp command failed with a Permission denied error when installing the compiled GCC binary:
cp: cannot create regular file ‘/usr/local/bin/gcc’: Permission denied
Root Cause:
• Misconfigured $PATH, $LD_LIBRARY_PATH, or $GCC_EXEC_PREFIX caused GCC to use incorrect paths.
• configure was executed with the wrong environment settings, leading to a broken build.
• cp failed due to attempting to copy files to a system-protected directory without proper permissions.
Solution:
1. Reset Environment Variables
• Check current values with:
echo $PATH
echo $LD_LIBRARY_PATH
env | grep GCC
• If incorrect values are found, reset them:
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
export LD_LIBRARY_PATH=/usr/lib:/usr/local/lib
unset GCC_EXEC_PREFIX
2. Reinitialize GCC Setup
rm -rf ~/gcc-build-001
mkdir ~/gcc-build-001
cd ~/gcc-build-001
../git/gcc/configure --prefix=$HOME/gcc-test-001
🔹 Conclusion: Most Issues Originated from the tree-skim.cc Code
The majority of issues were not due to misconfigurations but rather due to errors in the custom pass file (tree-skim.cc).
Initially, I assumed the issue was with the build environment or configure, but in reality, the root cause was incorrect logic inside the new pass code.
Even if the build environment is correctly set up, errors in the pass code itself can cause failures during both compilation and execution.
Reflection
Through this project, I gained hands-on experience with GCC’s internal structure and learned how to create and integrate a custom pass into the compilation pipeline. While I successfully implemented a basic pass, the process was far from smooth.
One of the biggest challenges was dealing with persistent build failures. Each time I encountered an issue, I often ended up deleting and recreating files, resetting configurations, and redoing steps from scratch, which sometimes made things worse. In hindsight, many of the problems were not due to the build environment but rather mistakes in my own pass code. I initially assumed something was wrong with my setup, but I later realized that debugging my pass logic first would have saved a lot of time.
Another key takeaway was the importance of version control. Since I wasn’t using Git properly at the start, I lost track of what I had changed, making it difficult to revert to a working state. As I kept modifying files without proper tracking, my setup became increasingly unstable. Eventually, I started using Git for version control, which allowed me to experiment more confidently and quickly restore previous versions when things went wrong.
Overall, this project reinforced the idea that understanding the system is just as important as writing code. Instead of assuming an issue is caused by the environment, I now know to carefully analyze my own code first. And most importantly, version control is essential for any development process, especially when working with a complex system like GCC.
Reference
Most of the ideas came from Professor Tyler’s Wiki and feedback. Thank you! 😊
Comments
Post a Comment