5

这是我尝试在嵌入式 Linux 系统上运行的一些示例测试代码:

#include <iostream>

int main(int argc, char *argv[])
{
    char c = 'A';
    int i = 7;

    std::cout << "Hello World from C++" << std::endl;
    std::cout << "c=" << c << std::endl;
    std::cout << "i=" << i << std::endl;
}

嵌入式系统是 Microblaze,它是运行在 Xilinx FPGA 上的 32 位 RISC 软核处理器。请不要因此而推迟,因为您的许多标准 Linux 知识仍然适用。处理器配置为带有 MMU 的 LSB,而我正在使用的 Linux 构建(PetaLinux,由 Xilinx 提供)也期望相同。我正在使用提供的 GNU 编译器;GCC 似乎正式支持 Microblaze。

我遇到的问题是,当 stdlib 需要与整数交互时,它会出现段错误。这是输出:

Hello World from C++
c=A
Segmentation fault

请注意,字符处理得很好。此代码的 C 等效项也可以正常工作:

#include <stdio.h>

int main(int argc, char *argv[])
{
    char c = 'A';
    int i = 7;

    printf("Hello World from C\n");
    printf("c=%c\n", c);
    printf("i=%i\n", i);

    return 0;
}

...

Hello World from C
c=A
i=7

这使我怀疑共享库存在问题libstdc++.so.6.0.20。该库是由 Xilinx 提供的,所以它应该是正确的。该file库的输出是:

libstdc++.so.6.0.20: ELF 32-bit LSB shared object, Xilinx MicroBlaze 32-bit RISC, version 1 (SYSV), dynamically linked, not stripped

我的二进制文件的file输出是:

cpptest: ELF 32-bit LSB executable, Xilinx MicroBlaze 32-bit RISC, version 1 (SYSV), dynamically linked, interpreter /lib/ld.so.1, for GNU/Linux 2.6.32, stripped

我也尝试过使用-static标志静态链接我的二进制文件,但结果是一样的。

这是我正在使用的最相关的编译器和链接器设置,但我尝试更改这些设置但无济于事。

CC=microblazeel-xilinx-linux-gnu-gcc
CXX=microblazeel-xilinx-linux-gnu-g++

CFLAGS= -O2 -fmessage-length=0 -fno-common -fno-builtin -Wall -feliminate-unused-debug-types
CPPFLAGS?=
CXXFLAGS= -O2 -fmessage-length=0 -fno-common -fno-builtin -Wall -feliminate-unused-debug-types
LDFLAGS=-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed

To compile:
@$(CCACHE) $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(CFLAGS) $< -o "$@"

To link:
@$(CXX) $(RELOBJECTS) $(LDFLAGS) $(EXT_LIBS) -o $(RELBINARY)

请注意,它microblazeel指的是 microblaze 编译器的 little endian 版本。

我非常想对此进行调试或至少查看一个 coredump,但是当发生段错误时似乎没有产生 coredump,并且gdb在 Microblaze Linux 构建中没有可执行文件。也许我错过了什么?

感谢您花时间阅读本文。有什么想法吗?

4

1 回答 1

1

经过一番研究,我看到的vivado是硬件开发 IDE [因为他们提供试用期——所以这是硬件开发,他们总是想为此收费]。

如果您使用的是赛灵思的标准 SDK,则所有内容都应进行预配置。否则,硬件设计师会生成包含 Microblaze 的硬件设计。

由此,您可能必须使用 petalinux 生成兼容的新引导、内核等映像。

可能需要libstdc++从源代码进行重建,但我会作为最后的手段这样做。例如,在您开始gdb工作并获得测试结果之前,请不要打扰它。


以下是一些 petalinux PDF 文件:
http://www.xilinx.com/support/documentation/sw_manuals/petalinux2013_10/ug977-petalinux-getting-started.pdf
http://www.xilinx.com/support/documentation/sw_manuals/ petalinux2013_10/ug981-petalinux-application-development-debug.pdf


开发指南显示如何调用 gdb(例如):
在目标系统上:
gdbserver host:1534 /bin/myapp
在开发系统上:
petalinux-utils --gdb myapp后跟target remote 192.168.0.10:1534


我已经使用注释对您的 Makefile 进行了一些编辑。我已经注释掉了一些非必要的选项。请注意,我正在使用运算符逐渐+=建立CFLAGS/CXXFLAGS

这里的基本思想是进行与“标准”偏差最小的构建。仅添加经过验证的基本选项。构建和测试。将选项逐一添加[每次重新构建和测试],直到找到导致问题的选项。

然而,我确实强烈怀疑自己-fno-common会成为问题的根源。另外,在较小程度上,我有点怀疑-Wl,--as-needed

这些选项应该有效吗?当然,但是 xilinx/microblaze 不是没有 x86 ...

我添加了两个命令行 make 变量:
DEBUG-- generate for debug with gdb
VERBOSE-- show all about the build process

例如,尝试make <whatever> DEBUG=1 VERBOSE=1

CC = microblazeel-xilinx-linux-gnu-gcc
CXX = microblazeel-xilinx-linux-gnu-g++

CPPFLAGS ?=

CMFLAGS += -Wall -Werror
CMFLAGS += -fmessage-length=0

# compile for gdb session
# NOTES:
# (1) -gdwarf-2 may or may not be the the right option for microblaze
# (2) based on doc for -feliminate-unused-debug* petalinux/microblaze may want
#     stabs format
ifdef DEBUG
  CMFLAGS += -gdwarf-2
  CMFLAGS += -O0

# compile for normal build
#else
  CMFLAGS += -O2
  CMFLAGS += -feliminate-unused-debug-types
endif

# NOTE: I used to use "@" on commands, but now I leave it off -- debug or not
# sure it's "ugly" but you can get used to it pretty quickly--YMMV
ifndef VERBOSE
  Q :=
else
  ###Q := @
  Q :=
endif

# let compiler/linker tell you _everything_:
# (1) configure options when tool was built
# (2) library search paths
# (3) linker scripts being used
ifdef VERBOSE
  CMFLAGS += -v
  LDFLAGS += -Wl,--verbose=2
endif

CMFLAGS += -fno-builtin

# NOTE: I'd _really_ leave this off as it may confuse c++ std as "<<" calls
# _M_insert (which is in the library, which is almost certainly _not_ using
# -fno-common)
###CMFLAGS += -fno-common

# NOTE: I'm also suspicious of this a little bit because the c++ lib may have
# some "weak" symbols that the c library doesn't
###LDFLAGS += -Wl,--as-needed

# NOTE: this seems harmless enough, but you can comment it out to see if it
# helps
LDFLAGS += -Wl,--hash-style=gnu

# NOTE: an optimization only
ifndef DEBUG
  LDFLAGS += -Wl,-O1
endif

CFLAGS += $(CMFLAGS)
CXXFLAGS += $(CMFLAGS)

# NOTES:
# (1) leave this off for now -- doesn't save _that_ much and adds complexity
#     to the build
# (2) IMO, I _never_ use it and I erase/uninstall it on any system I
#     administrate (or just ensure the build doesn't use it by removing it
#     from $PATH)--YMMV
###XCCACHE = $(CCACHE)

# to compile
$(Q)$(XCCACHE) $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(CFLAGS) $< -o "$@"

# to link
$(Q)$(CXX) $(RELOBJECTS) $(LDFLAGS) $(EXT_LIBS) -o $(RELBINARY)
于 2015-11-18T03:53:20.173 回答