What does "Error: L6248E: cannot have address type relocation" mean?

本文介绍了解决ARM编译器在构建位置独立代码时出现的链接错误的方法。通过两个示例展示了如何避免使用全局指针,以确保代码能够正确链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Applies to: ARM Developer Suite (ADS)RealView Developer Kit (RVDK) for OKIRealView Developer Kit for XScale (RVXDK)RealView Development Suite (RVDS)

Answer

This linker error can occur when trying to build "Position Independent" code. Consider a small example like:

#include <stdio.h>
char *str = "test";
int main(void)
{
    printf ("%s",str);
}

when compiled and linked with:

armcc -c -apcs /ropi pi.c
armlink -ropi pi.o

the linker will report a message of the form:

Error: L6248E: pi.o(.data) in ABSOLUTE region 'ER_RW' cannot have address/offset type
relocation to .constdata in PI region 'ER_RO'.

For the code above, the compiler generates a global pointer "str" to the char string "test". The global pointer "str" will need to be initialized to the address of the char string "test" in the .constdata section. However, absolute addresses cannot be used in a PI system, so the link step fails, because of the ABS32 relocations to (position independent) .constdata.

To resolve this, you must re-write the code to avoid the explicit pointer. Two possible ways are shown below:

1) Use a global array instead of a global pointer:

#include <stdio.h>
const char str[] = "test";
int main(void)
{
    printf ("%s",str);
}

2) Use a local pointer instead of a global pointer:

#include <stdio.h>
int main(void)
{
    char *str = "test";
    printf ("%s",str);
}

Please note that if you are using a list with multiple elements, such as:

char * list[] = {"zero", "one", "two"};

You will get a separate link error for each element in the array. In this case, the recommended solution is:

char list[3][5] = {"zero", "one", "two"};

with the print instruction being (for example):

printf("%s", list[1]);

Note that you will need to declare a two dimensional array for the list, with the first dimension as the number of elements in the array, and the second dimension as the maximum size for an element in the array.

转载于:https://www.cnblogs.com/WIT-Evan/archive/2012/01/04/7291508.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值