使用 mcmodel=medium 编译时 C++ 程序崩溃

C++ program crashes when compiled with mcmodel=medium

C++ 程序在使用 mcmodel=medium 编译时因分段错误而崩溃。我们在堆栈上使用了一些相当大的数组,我们需要启用中等 mcmodel。 我正在使用 g++ 5.4,当我对应用程序进行 strace 时,它​​会打印以下错误。请指教如何调试。

strace ./app 
execve("./app", ["./app"], [/* 65 vars */]) = -1 ENOMEM (Cannot allocate memory)
--- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
+++ killed by SIGSEGV +++
Segmentation fault (core dumped)

粘贴可执行文件的 readelf 输出 readelf -l 应用程序

Elf file type is EXEC (Executable file)
Entry point 0x6e4f60
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000074d0b8 0x000000000074d0b8  R E    200000
  LOAD           0x000000000074dc50 0x0000000000d4dc50 0x0000000000d4dc50
                 0x00000000000aafe0 0x00000041bf407a08  RW     200000
  DYNAMIC        0x000000000074dda8 0x0000000000d4dda8 0x0000000000d4dda8
                 0x0000000000000250 0x0000000000000250  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x00000000006b2c18 0x0000000000ab2c18 0x0000000000ab2c18
                 0x000000000000ef14 0x000000000000ef14  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     10
  GNU_RELRO      0x000000000074dc50 0x0000000000d4dc50 0x0000000000d4dc50
                 0x00000000000003b0 0x00000000000003b0  R      1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame .gcc_except_table 
   03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss .lbss 
   04     .dynamic 
   05     .note.ABI-tag .note.gnu.build-id 
   06     .eh_frame_hdr 
   07     
   08     .init_array .fini_array .jcr .dynamic .got 

内核版本: Linux rk-VirtualBox 4.4.0-64-lowlatency #85-Ubuntu SMP PREEMPT Mon Feb 20 12:39:25 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

您显示的 strace 输出表明 内核 拒绝启动您的程序(您的程序没有执行任何指令)。

内核简单地说:"this executable is built in such a way that I can't make it run"。

readelf -l ./app 的输出和确切的内核版本可以帮助进一步诊断。

更新:

第二个LOAD段:

LOAD           0x000000000074dc50 0x0000000000d4dc50 0x0000000000d4dc50
               0x00000000000aafe0 0x00000041bf407a08  RW     200000

要求内核分配 (mmap) 0x41bf407a08 字节。这几乎是 263GiB。要么你的机器没有那么多内存,要么你的 ulimit -vulimit -d 设置得太低,或者两者都有。