2009. 1. 21. 10:00

이 에러는 얼마전 내가 겪었던 나름대로 당혹스러웠던 에러였다.

다른데서 잘 동작하는 게 갑자기 링크에러를 내다니...

결국은 내가 무지한 탓이었지만...


직관적으로 ARM 사이트(http://www.arm.com)에서 찾아보면 다음과 같이 나온다.
http://www.arm.com/support/faqdev/1240.html


그냥 봐선 저 내용 잘 이해가 안될 것이다.
저 내용만 보고 이해를 하려면, ARM link시에 사용하는 몇몇 옵션들에 대해서 이미 잘 알고 있다는 전제하에서 가능하다.

그럼 차근차근 저 내용을 풀어 보도록 하겠다.


  What does "Error: L6248E: cannot have address type relocation" mean?
  실제 에러는 이렇게 나오지는 않고, 이 의미를 포함해서 나온다.
  처음에 제일 당황스러운 부분이다. 왜 주소를 가질수 없다는 걸까? 
  분명히 코딩 상의 문제는 아니다.



 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);
}

 그냥 보면 이 코드는 전혀 문제가 없어 보인다. 정말 단순하기 짝이 없지만 크게 문제될 부분으로 보이지는 않는다.
실제로 gcc로 컴파일 해보자. 아무 에러 안나고 잘 실행될 것이다.
중요한 것은 링크 옵션에 있다.


when compiled and linked with:
armcc -c -apcs /ropi pi.c
armlink -ropi pi.othe 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.

자 드디어 보여지는 링크 옵션. 강조해 놓은 저부분에 아무런 문제도 없어 보이는 코드에서 링크에러를 보여주는 원인 제공자이다. 
 눈에 띄는 옵션 들이 있다. -apcs /ropi
 Error 부분을 보면 낯선 구문이 보인다. PI region 'ER_RO' 이걸 이해하는데 키워드중 하나가 바로  PI region이다.

* PI region
  - Position Independent Region 의 약자이다.
     ARM Manual에는 간략하게 이런 설명이 있다.
     One or more of the position-independent regions is automatically moved to a different address.
     이 이야기는 load 시, 즉 프로그램이 실행되어 data 영역이 ram 에 로드될때 PI region에 속한 data 영역은 absolute하지 않고 relative 하다, 다른 말로 different address를 가진다는 이야기이다.

* Error 부문을 해석해 보면
  ABSOLUTE region ER_RW 에 있는 pi.o(.data)(-- 이것은 data영역 즉 변수에 대한 영역이다.--) 는 PI region ER_RO에 속한 .constdata 에 대한 address를 가질수 없다라는 뜻으로 해석된다. 
  좀더 쉽게 접근하자면,  absolute region에서 relative region의 address를 참조할 수 없다라고 이해하면 된다.
  (어떻게 보면 정말 당연한 이야기이기도 하다.)
  Position Independent region이라는 것이 absolute address를 가지는 것이 아니라 pc-relative address를 가진 다는 의미이다.

 * 그렇다면 갑자기 PI region이라는 것은 어디서 튀어나온 것일까?
   그렇다, 이것은 옵션에 지정되어 있다. 
      /ropi, -ropi

  이 두 옵션에 대해 ARM manual에서는 다음과 같이 설명하고 있다.
* -ropi
---------------------------------------------------------------------------------------------------------
This option generates (read-only) position-independent code. /pic is an alias for this option. If this option is selected the compiler:
 - addresses read-only code and data pc-relative
 - sets the Position Independent (PI) attribute on read-only output sections.
Note
The ARM tools cannot determine if the final output image will be Read-Only Position Independent (ROPI) until the linker finishes processing input sections. This means that the linker might emit ROPI error messages, even though you have selected this option.
---------------------------------------------------------------------------------------------------------

* 다시 Error 밑 쪽의 설명을 참조하여 풀어보면
  compiler는 global pointer 변수 str를 만들고, 코드에 이에 대한 초기값이 지정되어 있으므로 이 초기값으로 str 변수 값을 초기화한다. 이 때 "test" 이 값은 const data 형태로 생성하고, 이는 ro 영역, read only 영역에 속한다. 
그런데 컴파일과 링크 옵션에서 지정했다시피 ro 영역은 PI region으로 지정하였다. 이 이야기는 "test"에 대한 address가 load시마다 달라질 수 있다는 의미이다. str이라는 변수는 compile 시 초기값이 결정되어야만 하고, 이 초기값으로 사용되는 "test"에 대한 address는 load시에 결정된다. 결국 compiler는 global pointer 변수 str의 초기값으로 load시 마다 달라질 수 있는 address를 넣을 수 없기 때문에 이런 에러를 보여주는 것이다.

 

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.

 
 ARM 사이트에서 제시하는 이런 에러에 대한 해결 방안이다.
 결국 코드를 바꿔야 한다. 이런식의 코드를 Position Independent code라고 부른다.

 이 예제의 내용은 비교적 이해하기 어렵지 않다.
 1) 같은 const 영역 ro 영역에 두는 것이다. 물론 같은 ro 영역이니 상관없다.
 2) global로 쓰지 않고 local variable로 사용하는 것이다.

 마지막의 두 array에 대한 예제는 크기를 지정하지 않은 경우와 지정한 경우에 대해 차이가 있고, 이 차이에 의해 이 에러를 일으킬 수 있다는 점을 설명하고 있다.
  좀 더 다르게 이런식의 접근도 가능하다.
   char *list[]   ==> 배열 포인터, 즉 global pointer variable이다. 결론적으로 처음 설명한 것과 같은 논리이다.
   char list[3][5] ==> 사이즈와 값이 결정된 array이다. 
  이둘의 차이는 하나는 포인터를 가지는 변수고, 하나는 값을 가지는 변수라는 점이다.

 이 에러의 초점은 주소 참조 이다. 그러니 address를 참조하지 않는다면 아예 해당되지 않는 이야기인 것이다.


참조
http://www.arm.com