Linking fails : relocation truncated to fit: R_X86_程序占用内存大于2GB所导致的问题

GCC 编译错误 relocation truncated to fit: R_X86_64_32S against `.bss' 问题如下图所示:(.text+0x53a): relocation truncated to fit: R_X86_64_32S against `.bss'     以前在linux中编译程序,从来没有遇到过这种问题,今天第一次遇到。查了一些资料发现时内存的限制,自己还有些疑问,以前编译内存大小是4G的程序都没出现过这种错误。       看到一些博文中说是内存超过2G会出现问题。需 阅读详情
  
1
 

Problem(Abstract)

This technote explains how to resolve a Purify engine: Error: The linker returned error code: 1 error, that can occur when using IBM Rational PurifyPlus.

Symptom

The following error when linking an application with PurifyPlus:


  • relocation truncated to fit: R_X86_64_PC32 _P_W_8B_C
    Purify engine: Error: The linker returned error code: 1


Cause

By default, gcc generates code for the small code model. This means that the program and its symbols must be linked in the lower 2 GB of address space. You receive this error because your application is exceeding the 2 GB limit when linked with PurifyPlus.

Resolving the problem

To resolve this problem, you can do one of the following:

  1. Use the medium code model. This is done by specifying the gcc option -mcmodel=medium. In the medium code model, the program is linked in the lower 2 GB of address space, but symbols can be located anywhere in the address space (i.e. they can be located beyond 2 GB). The medium code model cannot be used in conjunction with the -fPIC compiler option.
  2. Dynamically link with the library referenced in the "relocation truncated to fit ..." message, rather than statically linking with that library.


http://www-01.ibm.com/support/docview.wss?uid=swg21413829
 
2 
程序占用内存大于2GB所导致的问题( http://hi.baidu.com/ialen/blog/item/437d4f7fd3a7ec2f0cd7dacb.html)
2011-05-15 20:18

最近遇到了一个问题,他的程序在分配小数组的时候编译、运行都安然无恙,而一旦数组足够大之后编译就会出现含有
relocation truncated to fit
这种字样的错误信息。最后W和S同学一起调研发现是程序占用内存大于2GB所导致的问题。现在的程序的内存都大于2GB了,不得不感叹下新生代的力量。发现了问题要解决却不容易。不过在S同学的辛苦调研和亲身调试下,问题最终得以解决。偶顺手把解决方法拿了过来,万一哪天我的程序也这么变态了…
主要的参考文献是这两篇:

http://software.intel.com/en-us/articles/avoiding-relocation-errors-when-building-applications-with-large-global-or-static-data-on-intel64/
http://www.ualberta.ca/CNS/RESEARCH/LinuxClusters/64-bit.html

为防止以后网址失效,将第二篇的主要内容copy在这里:
The Portland Group, Intel, and GNU compilers installed on the AICT Linux Cluster are configured to produce 64-bit executables by default. Additional compiler flags such as the GNU -m64 are unnecessary for this purpose.
New or existing programs that use less than 2GB of memory will probably not require any modifications to their source code or to the way they are compiled. A large-memory program, on the other hand, in addition to the data size issues mentioned above, may need additional compiler flags, depending on the compiler used and how data is implemented.
Typically, large data objects are declared as either static global arrays or dynamically allocated memory. These are associated with the data and heapmemory segments, respectively. Large heap (and stack) segments do not need any special attention. However, if the size of the data segment (sum of all static global arrays) exceeds 2GB, use the appropriate flags from the following two tables.
C/C++ compilers flag
Portland pgcc -mcmodel=medium
pgCC -mcmodel=medium
Intel icc –
GNU gcc –
g++ –
Table 1 C/C++ compiler flags for accommodating large data segments.

Fortran compilers flag
Portland pgf77 -mcmodel=medium
pg90/pg95 -mcmodel=medium [-i8]¹
Intel ifort -mcmodel=medium -i-dynamic² [-i8]¹
GNU g77 -mcmodel=medium
¹ Makes intrinsic array enquiry functions return INTEGER*8 values.
² Add $INTEL_LIB_PATH to LD_LIBRARY_PATH.
Table 2 Fortran compiler flags for accommodating large data segments.
The -mcmodel flag selects the memory model or memory access modes for a program. Three memory models are supported by the compilers: small(text+data<2GB), medium (text<2GB, data>2GB), and large (text>2GB, data>2GB). Memory segments less than 2GB can be accessed with direct references, while larger segments require indirect or relative references.
Because its addressing modes are more efficient, the small model is faster than the medium model. Therefore, unless your program needs support for large data, it is best to use the small model. Text segments larger than 2GB are rare, so the large model can be ignored, and in any case, it is available only with the Intel compiler.
The default memory model for the Portland compilers is small. On the other hand, the default for both the Intel and GNU C/C++ compilers is medium, and for the Fortran compilers it is small.

If you build your program by compiling and linking in separate stages, be sure to include the -mcmodel flag (if used) on the linker command line as well.
When using -mcmodel=medium with the Intel Fortran compiler, the additional -i-dynamic flag is required to link the appropriate libraries. Because these libraries are dynamically linked, your executable must be made aware of their location. One way to do this is to amend the environment variableLD_LIBRARY_PATH with the value of INTEL_LIB_PATH as follows (bash syntax shown).
export LD_LIBRARY_PATH=$INTEL_LIB_PATH:$LD_LIBRARY_PATH
This command will also be necessary in batch job scripts. Another way is to embed $INTEL_LIB_PATH directly into the executable file using a compiler (linker) flag. For example,
ifort -mcmodel=medium -i-dynamic -i8 source.f90  -Wl,-rpath,$INTEL_LIB_PATH
The -i8 flag should be used for Fortran 90/95 code that calls any instrinsic array enquiry functions, such as SIZE. Without it, the return values of these functions are truncated to 32 bits. This is true for both Portland and Intel compilers.
对于W同学的f77程序,我们的最终解决方法是在编译和链接时都加上如下Flags:
-mcmodel=medium -i-dynamic
最后还有个补充问题。如果要用mkl库,我们发现用如下的链接方式

FLIBS +=-lmkl_lapack -lmkl_em64t -lmkl_solver -lguide -lpthread
时会出错。需要改成
FLIBS += -lmkl_intel_lp64 -lmkl_core -lmkl_sequential
同时在LD_LIBRARY_PATH环境变量中要加上intel的mkl下的lib/em64t目录所在的路径,比如
export

bl跳转指令使用错误导致编译出现错误--relocation truncated to fit: R_ARM_PC24 init_irq bl 跳转指令只能在+/- 32MB内跳转: 阅读详情

相关推荐

make: *** [.tmp_vmlinux1] 错误 1

init/built-in.o: In function `loglevel': calibrate.c:(.init.text+0x120): relocation truncated to fit: R_ARM_CALL against symbol `get_option' defined in .text section in lib/lib.a(cmdline.o) init/bui

习惯就好zz的专栏 1728

Relocation truncated to fit解决办法

这种情况一般出现在汇编代码中。有些跳转指令的跳转范围很小。而当要跳转到的地址超出了范围,那么就会报错! 对于x86: 最终的原因是使用参数-mcmodel=kernel,只能使用最高2G最低2G的内存空间,如果想使用更多的大内存空间,应该使用-mcmodel=large。 对于ppc: mcmodel对于ppc并不适用,我们在网上搜索找到了-mlongcall参数,加入到base_c...

weixin_44138807的博客 2万+

IGV实战指南:从数据导入到Peak可视化的全流程解析

本文提供了一份IGV实战指南,详细解析了从数据导入到Peak可视化的全流程。内容包括软件安装、参考基因组加载、测序数据处理、信号调整以及如何精准定位和可视化Peak区域,并分享了导出出版级矢量图的高级技巧,帮助用户高效完成基因组数据可视化分析。

weixin_29212867的博客 147

解决relocation truncated to fit 问题

解决relocation truncated to fit 问题

Contas的博客 3776

PCA9698嵌入式C++驱动库:高性能I²C GPIO扩展方案

GPIO扩展是嵌入式系统中解决MCU引脚资源不足的关键技术,其核心原理在于通过串行总线(如I²C)桥接主控与外设寄存器,实现逻辑引脚到物理IO的映射与状态同步。技术价值体现在低开销、高确定性与多设备可扩展性,广泛应用于工业控制、传感器节点和LED矩阵驱动等实时场景。本文聚焦PCA9698——一款支持24通道双向IO、中断输出与可编程驱动模式的I²C GPIO扩展芯片,结合其C++驱动库的影子寄存器机制、端口级原子操作及非阻塞同步设计,提供面向资源受限平台(如ATmega2560)的工程就绪方案。

weixin_35756637的博客 626

相机图像质量研究(22)常见问题总结:CMOS期间对成像的影响--光学串扰

光学串扰是一个比较难以理解的光学现象,实际影响往往不大。一、光学串扰产生的原因通常是入射角度太大,光线在不同通道之间产生了混叠二、光学串扰产生的现象光学串扰容易导致迷宫纹。总结本节讲了光学串扰产生的原因,现象。

xiaoshixiu的专栏 1572

解决GCC连接器(lld)出现问题 relocation truncated to fit (重定向截断)

但是缺点肯定是有的,那就是效率肯定没有RVA相对地址(短地址)跳转速度块,因为绝对地址都是需要先放在寄存器之中的,在按照寄存器的值跳转,不能直接JMP到绝对地址上面的,相当于每次都要多走一些流程,并且在CPU之中相对寻址本来就比随机的绝对寻址要快的。这条会有限制,C++ 17 直接没法用了,而且 C++ 11 的一些模板展开特性也会受到限制,基本不是很推荐通过这条指令来解决,但如果真的必须要,无可避免、退无可退的情况下,您或许可以考虑设置它到连接器上面试试看,的确是有效的。3、配置连接器选项,增加选项。

liulilittle的博客 6180

Qt跨平台CAN通信实战:Vector、PCAN与周立功设备统一接口设计

本文针对Qt开发中常见的多品牌CAN设备兼容难题,提出了一套统一的跨平台CAN通信接口设计方案。通过抽象层封装,实现了对Vector、PCAN和周立功设备的统一操作,上层业务代码无需修改即可在不同硬件间切换,有效解决了驱动API差异、环境配置复杂和维护成本高等痛点,显著提升了代码复用率和开发效率。

weixin_29161785的博客 329

ARM 常见问题总结

1、relocation truncated to fit: R_ARM_THM_CALL against symbol 'xxx' 可能的原因有几种: 1、第一种就是如下文所说,超出b/bl 跳转范围,这个可以看编译出来的code 地址来确认。 2、第二种可能是code 所在的section 属性有问题 例如下面这段汇编: test 在 .startup section中,Test2 在 .text section中 test 会调用Test2, 编译过程中会报错:relocation truncate

申小白 6335

【学习笔记】人群归因分数 PAF 以及combined PAF

人群归因风险比例(Population Attributable Fraction,PAF)是一个流行病学指标,它估计了在总体中,如果能消除一个特定的危险因素,能预防多少百分比的疾病或健康结果。简单来说,PAF回答的是:“如果没有这个风险因素,总体中会有多少病例是可以避免的?

weixin_53499537的博客 4898

科研——ICONIP论文修改和提交

ICONIP说明文档

Blackoutdragon的博客 6646

relocation truncated to fit: R_X86_64_PC32错误排除

relocation truncated to fit: R_X86_64_PC32:当程序开辟内存空间大于2G时,编译时会出现 relocation truncated to fit: R_X86_64_PC32的错误信息,排除办法如下:在编译选项中加入 -mcmodel=medium如果是MPI程序,则安装MPICH时需添加c和fortran的相关编译选项如下:./config

xlsp的专栏 1万+

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

relocation truncated to fit: R_MIPS_CALL16 问题!

我在broadcom7309上编译链接库生成程序时,出现以下链接问题。链接库时,出现以下很多类似的错误:relocation truncated to fit: R_MIPS_CALL16 against IsHealthy()relocation truncated to fit: R_MIPS_GOT16 against `g_dwCommsClockrelocation truncat

大大狗之家 1万+

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

relocation truncated to fit: R_X86_64_PC32

/home_soft/soft/x86_64/compiler/intel/11.0/081/lib/intel64/libifcore.a(for_diags_intel.o): In function `for__message_catalog_close': for_dia

We need more…… 7917

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Link error: relocation truncated to fit: R_MIPS_GPREL16 time

<br />Link error: relocation truncated to fit: R_MIPS_GPREL16 time<br />    具体解释见http://sourceware.org/ml/ecos-discuss/2000-04/msg00214.html<br />简单的说,是因为你把全局变量放到了编译后的某一节中,而这个节不是通用的 .data  .bss,而是特殊的节。导致最后链接的时候,编译器不知到选取哪个节中的变量。<br /> <br />例如:<br /> #defin

lijiecong的专栏 7228

运行C时报错:relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain‘ collect2: error

写C时,遇到报错 [Running] cd "d:\考研\408\LeranC\Code\GramForC\" && gcc 01data_types.c -o 01data_types && "d:\考研\408\LeranC\Code\GramForC\"01data_types /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-c

qq_45921491的博客 3032

有关Fortran编译的一个错误

从来没有用过fortran, 今天有师弟让我帮忙跑一个fortran的程序, 我一编译就报如下错误 /tmp/ccy4yDLX.o: In function `output_':Hjdk.for:(.text+0xc3): relocation truncated to fit: R_X86_64_32S against symbol `sol_' defined in COMMON ...

weixin_30273175的博客 1637

E003-编译问题(01)relocation truncated to fit: R_BA_10_PCREL against undefined symbol `embl_strea..`

Linux GCC 编译问题

weixin_43190801的博客 994

遇到过的错误

#include<iostream> #include<algorithm> #include <cstring> #include<cstdio> using namespace std; const int N=50100; int s[N][N]; int main() { int n,x,y,d,i=0,j=0,r; scanf("%d%d",&n,&r); while(n--) { ...

与其说是回归 不如说是与过去自己的诀别 1107

arm-linux-gcc 硬浮点,使用arm-linux-gcc-3.4.1软浮点报错:

使用arm-linux-gcc-3.4.1软浮点报错:lib_arm/libarm.a(_udivsi3.o)(.text+0x8c):/home/qsg/u-boot-1.2.0/lib_arm/_udivsi3.S:67: relocation truncated to fit:R_ARM_PLT32 __div0lib_arm/libarm.a(_umodsi3.o)(.text+0xa8)...

weixin_39986741的博客 487

编译u-boot:_udivsi3.S:67: relocation truncated to fit: R_ARM_PLT32 __div0

用交叉编译工具arm-linuc-gcc 3.4.1编译U-boot1.2.0时出现了这样的错误:       lib_arm/_udivsi3.S:67: relocation truncated to fit: R_ARM_PLT32 __div0       lib_arm/_umodsi3.S:79: relocation truncated to fit: R_ARM_PLT32 _

草原骑士的专栏 2426

ARM学习笔记010之Gcc编译新问题

11.27 之前的除法除不出来还没有解决,现在又有以下问题(在不添加raise函数的情况下): 网上说的原因是: I'm getting the "relocation truncated to fit: R_ARM_PC24 against symbol" error message when linking, what does this mean? You w

汉尼拔勇闯天涯 1262

linux下编译程序使其处理超过2G的大文件

为使程序可以处理大文件的能力,在文件中可以添加: #define -D_LARGEFILE_SOURCE #define -D_LARGEFILE64_SOURCE #define -D_FILE_OFFSET_BITS=64   在gcc编译时,加入的大文件(大于2G

fanfan_821的专栏 1120
下一篇: ssh配置文件详解
crazyhacking
博客等级 码龄15年 299粉丝 148原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值