【问题标题】:Confused about gcc -static option or its behavior in virtual machine对 gcc -static 选项或其在虚拟机中的行为感到困惑
【发布时间】:2017-02-23 06:14:15
【问题描述】:

我写了一个幼稚的c程序try.c

#include <stdlib.h>
int main() {return 0;}

然后我尝试使用下面的shell脚本编译并运行它

CFLAGS='-Wpedantic -Wall -Wextra -Werror -std=c89'
gcc -o try  ${CFLAGS}  try.c -static
valgrind ./try -v --track-origins=yes

那么输出就相当混乱了:

==16641== Memcheck, a memory error detector
==16641== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==16641== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==16641== Command: ./try -v --track-origins=yes
==16641== 
==16641== Conditional jump or move depends on uninitialised value(s)
==16641==    at 0x419349: _int_free (in  /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x41D296: free (in    /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x46CCAE: fillin_rpath (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x46D57A: _dl_init_paths (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x44282B: _dl_non_dynamic_init (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x443557: __libc_init_first (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641==    by 0x400B77: (below main) (in /home/su/ca/hw/1_try/try_static/trytrytry/try)

但是,如果我删除“-static”选项,一切都会顺利。 我在 ubuntu 16.04(虚拟机)和 ubuntu 14.04(虚拟机)上试过 可能跟虚拟机有关?

【问题讨论】:

  • 它与VM无关,但取决于您的libc和GCC。
  • 如果您将main() 更改为main(void),有什么变化吗?这两个在语义上是不同的,我只是在猜测,但这可能有助于建立答案......
  • 它不会改变任何东西。

标签: c gcc static-libraries valgrind static-linking


【解决方案1】:

此错误是 Valgrind 可能默认为 suppresses 的调用堆栈的一部分。抑制特定于调用所属的共享库(即 libc)——因此将程序构建为静态可执行文件会阻止 Valgrind 识别它应该忽略此错误。

无论如何,这个错误是 libc 初始化代码内部的,所以你应该忽略它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 2012-11-19
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2014-07-06
    • 1970-01-01
    相关资源
    最近更新 更多