[英]Calling NASM from a C program using system() produces different object code then calling using Bash
I've implemented a reasonably good optimizing compiler (for a toy language), and have come across a rather puzzling scenario. 我已经实现了一个相当好的优化编译器(针对玩具语言),并且遇到了一个令人费解的场景。 I can take an input file and produce assembly from it, so in that respect the "compiler" is finished. 我可以获取一个输入文件并从中生成程序集,因此在这方面,“编译器”已完成。 If I take that assembly file assemble it with NASM and link it to my runtime support library with G++ (the runtime needs libstdc++), I get a working executable, with no errors. 如果我使用该程序集文件与NASM进行组装,并使用G ++将其链接到我的运行时支持库(运行时需要libstdc ++),我将获得一个正常运行的可执行文件,并且没有任何错误。 However, I'd like to be able to compile to an executable in one command, so I added some calls to system
to my compiler, passing the EXACT SAME COMMANDS as I was using in bash. 但是,我希望能够用一个命令将其编译为可执行文件,因此我在编译器中添加了一些对system
调用,传递了我在bash中使用的EXACT SAME COMMANDS。 When I run the compiler, it seems to assembly properly, but the linking step (again, using g++) fails with an undefined reference to main
. 当我运行编译器时,似乎可以正确地进行组装,但是链接步骤(再次使用g ++)失败,并且undefined reference to main
。 Confused, I attempted to link manually (without reassembling, so I was using the object file produced by the NASM run with system
, and received the same error. If I reassemble using the new compiler's assembly output, I have no problems, which has led be to believe that NASM is the problem. Like I said, the commands are exactly the same (I literally copied and pasted them just to make sure after the first time). Is this environment variables or something? What's going on? 感到困惑,我尝试手动链接(不进行重新组装,因此我使用了NASM与system
运行的目标文件,并收到了相同的错误。如果我使用新编译器的汇编输出进行重新组装,则没有问题,这导致了可以相信NASM是问题。就像我说的,这些命令是完全相同的(我确实是在第一次复制后才将它们粘贴并粘贴的),这是环境变量还是什么原因?
EDIT: I manually assembled an object file, again using the same command as the one in the compiler, and I did a vim diff between the two. 编辑:我手动组装了一个目标文件,再次使用与编译器中相同的命令,并且在两者之间做了vim diff。 The compiler-generated one seems to only contain the ELF header. 编译器生成的似乎只包含ELF标头。
EDIT 2: A screenshot of the diff 编辑2:差异的屏幕截图
EDIT 3: I tried using system
to call a Perl script that in turn would call NASM and G++, still no luck. 编辑3:我尝试使用system
调用Perl脚本,而该脚本又将调用NASM和G ++,仍然没有运气。
Fixed it! 修复! It was the file not being flushed due to a race condition. 这是由于竞争状况导致文件未刷新。 Thanks for all the help, much appreciated. 感谢您的所有帮助,不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.