C编译: relocation truncated to fit R_X86_64_PC32 against symbol

标签 c compilation relocation

/* my program
   author/date:  me/now
*/

# include <stdio.h>

# define XX    1000
# define YY    20000       /* value of 1000 is ok */
# define ZZ    6000

/* global variable declaration */

int some_variable_this;
int some_variable_that;
double data[XX][YY][ZZ];

static void some_procedure_this ( void )
{
}

static void some_procedure_that ( void )
{
}

int main ( int argc, char *argv[] )
{
}

编写一个快速 C 程序来重新格式化一些数据。 通过 gcc myprogram.c 编译时,如果我使全局 data 数组太大,则会出现编译器错误:

relocation truncated to fit R_X86_64_PC32 against symbol 'some_variable_this'
relocation truncated to fit R_X86_64_PC32 against symbol 'some_variable_that'

我的目标是编写一个快速的 C 代码来重新格式化一些数据。

  • 这个R_X86_64_PC32是什么意思?
  • 是否有一个编译器标志可以用来解决这个问题?
  • 有没有更好的方法用 C 语言来编写此代码,同时仍然保持编写代码的速度和人类可读性的简单性?

如果重要的话,请在 Linux 中的 gcc 4.3.4 上使用。

最佳答案

  • What does this R_X86_64_PC32 mean?

它是 x86_64 的 ELF 中使用的 ELF 重定位类型。该特定类型表示引用数据的位置是根据与程序计数器相关的地址的 32 位偏移量计算的。我解释诊断结果表明所需的偏移量太大,无法适应提供的 32 位,这意味着编译器生成的代码实际上链接器无法正确链接。

  • Is there a compiler flag I can used to get around this?

也许吧。基本问题是您定义的对象比编译器作者(当时)想象的任何人都能够容纳的要大。您正在超越其设计限制。

可能有一些选项可以减轻这种影响。特别是,您可以尝试调整 -fPIC 和/或 -fPIE 选项,可能还有其他选项。

  • Is there a better way to code this, in C, while still maintaining quickness of writing the code and simplicity for human readability?

这本身并不是一个 C 问题,而是一个实现问题。然而,就语言标准而言,GCC 在这里并没有错或有缺陷——该标准并不强制实现接受技术上有效的每一个可能的程序。

话虽如此,您还可以尝试将 some_variable_thissome_variable_that 的声明移至 data 的声明之后>。可以想象,它也可能有助于声明这些变量静态,或将它们(或数据)移动到单独的共享库。

关于C编译: relocation truncated to fit R_X86_64_PC32 against symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57331990/

相关文章:

c++ - 什么是 boost 密集 C++/模板编译的良好 CPU/PC 设置?

c - 如何读取目标文件的重定位记录

web - 将网站迁移到新域,以及相关的谷歌索引问题

c - 为什么 main 函数总是加载在同一个地址,而变量大多数时候有不同的地址?

函数调用后无法定义变量

具有指针字段的结构的连续内存分配

visual-studio - DACPAC代表什么?

android - gradle中的 'compile …'是否需要安装设备中的Internet?

c - Makefile:没有链接问题的编译

c - 如何编译使用 getsubopt() 的代码?