<div dir="ltr"><br><div class="gmail_extra"><span style="font-size:11pt;font-family:"Calibri","sans-serif";color:rgb(31,73,125)">>
And be warned that the PC doesn't point at the next instruction when you
use it like this - I believe you don't need to modify it at all if you
swap the pop and the .long.</span><br>Bernie, is it related to ARM pipeline? I'm interesting in this, is there any other additional information?<br><br><div class="gmail_quote">On Fri, Mar 8, 2013 at 4:59 AM, Tim Northover <span dir="ltr"><<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Ashi,<br>
<div class="im"><br>
> ld: illegal text-relocation to _data_table in table.o from foo in<br>
> use_table.o for architecture armv7<br>
<br>
</div>It looks like you're using iOS. I'm not familiar with the exact<br>
workings of that platform, but I think a similar message would occur<br>
in ELF-land.<br>
<br>
If iOS *is* comparable, your issue is that symbols in dynamically<br>
loaded objects can't (usually) be referenced directly because the code<br>
section (and any embedded litpools) need to be identical no matter<br>
where everything is loaded in memory. This is most of the point of<br>
.dylib files: the instructions only have to exist once in memory and<br>
can be shared. If the bytes would be different for each user that's<br>
not possible -- the linker spots places where that happens and gives<br>
an error.<br>
<br>
My advice would be to compile a .c file (with "-S" to view the<br>
assembly) which makes accesses like you're trying to do and copy its<br>
code. There's no shame in it: I had a good idea of what ELF did but<br>
still got the syntactic details wrong when I tried to write similar<br>
code off the top of my head.<br>
<br>
In this case, for example, I'd compile the C code (with "-fPIC" in<br>
principle, though it seems to make no difference here. Actually, I'm<br>
rather disturbed that you're trying to compile the C part of a .dylib<br>
without "-fPIC". In the Linux world that's a no-no. Could be valid for<br>
iOS, but I'd suggest you make sure if you don't know):<br>
<br>
extern int data_table[];<br>
<br>
int *wheres_data_table(void) {<br>
return &data_table[0];<br>
}<br>
<br>
(N.b. "armv7" uses a movw/movt pair which is perfectly valid. If you<br>
want to see how to do it using litpools, you need to compile for<br>
"armv6" which doesn't have those instructions).<br>
<br>
Cheers.<br>
<span class=""><font color="#888888"><br>
Tim.<br>
</font></span></blockquote></div>Thanks for all your great reply! Finally, I got it work by several ways, compiler's assembly output would help a lot(thanks Tim) and a linker option:-Wl,-read_only_relocs,suppress would also help.<br>
And here is a similar problem under powerPC :<a href="http://lists.apple.com/archives/unix-porting/2008/Jan/msg00027.html">http://lists.apple.com/archives/unix-porting/2008/Jan/msg00027.html</a><br><br>here is my summary:<br>
*** problem ***<br>LDR Rx, =Label is not supported under Clang<br><br>*** solution ***<br>replace LDR pseudo-instruction by manually loading Label. 2 methods are used, they are shown in use_table_m1.s and use_table_m2.s respectively. There are 7 targets in my Makefile, include several different option test(details are all in Makefile).<br>
Test environment is Xcode4.6. The test_d_m1_1 can't be generated due to illegal text-relocation error and others can be generated, they are 3 dynamic targets and 3 static targets, the test_s_m1_2 have a warning due to -mdynamic-no-pic option. however I haven't test them on iOS device, I'm not sure whether the "-Wl,-read_only_relocs,suppress" option would really work.<br>
<br><br><br>/* ==begin table.c== */<br>int data_table[] = {0xff, 0xff};<br>/* ==end table.c== */<br><br>/* ==begin use_table_m1.s==<br> * use table by LDR <br> */<br> .text<br> .syntax unified<br> .align 4<br> .global foo<br>
.thumb <br> .thumb_func<br><br>foo:<br> LDR r0,[PC,#4]<br> BX lr<br> .long _data_table <br>/* ==end use_table_m1.s== */<br><br>/* ==begin use_table_m2.s==<br> * use table by code from compiler's assembly out put <br>
*/<br> .text<br> .syntax unified<br> .align 4<br> .global foo<br> .thumb <br> .thumb_func<br><br>foo:<br> /* these lines are from compiler's assembly output($(CC) -S):<br> * extern int data_table[];<br>
* int *wheres_data_table(void) {<br>
* return &data_table[0];<br> * }<br> */<br> movw r1, :lower16:(L_data_table$non_lazy_ptr-(LPC0_0+4))<br> movt r1, :upper16:(L_data_table$non_lazy_ptr-(LPC0_0+4))<br>LPC0_0: <br> add r1, pc<br>
ldr r1, [r1]<br> bx lr<br><br> .section __DATA,__nl_symbol_ptr,non_lazy_symbol_pointers<br> .align 2<br>L_data_table$non_lazy_ptr:<br> .indirect_symbol _data_table<br> .long 0<br>
<br> .subsections_via_symbols<br>/* ==end use_table_m2.s== */<br><br>/* ==begin test.c ==*/<br>extern int data_table[];<br>int main(void)<br>{<br> int a = data_table[0];<br> return 0;<br>}<br>/* ==end test.c ==*/<br>
<br><br>/* ==Makefile== */<br><br>CC = /Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang<br>AR = /Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar<br>CFLAG = -fPIC -Wall -arch armv7 -mcpu=cortex-a9 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk <br>
<br>all:dynamic static<br><br>dynamic:test_d_m1_1 test_d_m1_2 test_d_m2_1 test_d_m2_2<br><br>static:test_s_m1_1 test_s_m1_2 test_s_m2_1<br><br>test_d_m1_1:test.c libtest_m1_1.dylib<br> $(CC) $(CFLAG) test.c ./libtest_m1_1.dylib -o $@<br>
<br>test_d_m1_2:test.c libtest_m1_2.dylib<br> $(CC) $(CFLAG) test.c ./libtest_m1_2.dylib -o $@<br><br>test_d_m2_1:test.c libtest_m2_2.dylib<br> $(CC) $(CFLAG) test.c ./libtest_m2_2.dylib -o $@<br><br>test_d_m2_2:test.c libtest_m2_2.dylib<br>
$(CC) $(CFLAG) test.c ./libtest_m2_2.dylib -o $@<br><br>test_s_m1_1:test.c libtest_m1_1.a<br> $(CC) $(CFLAG) $^ -o $@<br><br>test_s_m1_2:test.c libtest_m1_1.a<br> $(CC) $(CFLAG) -mdynamic-no-pic $^ -o $@<br><br>
test_s_m2_1:test.o libtest_m2_1.a<br> $(CC) $(CFLAG) $^ -o $@<br><br>libtest_m1_1.dylib:table.o use_table_m1.o<br> $(CC) -dynamiclib $(CFLAG) $^ -o $@<br><br>libtest_m1_2.dylib:table.o use_table_m1.o<br> $(CC) -dynamiclib -Wl,-read_only_relocs,suppress $(CFLAG) $^ -o $@<br>
<br>libtest_m2_1.dylib:table.o use_table_m2.o<br> $(CC) -dynamiclib $(CFLAG) $^ -o $@<br><br>libtest_m2_2.dylib:table.o use_table_m2.o<br> $(CC) -dynamiclib -Wl,-read_only_relocs,suppress $(CFLAG) $^ -o $@<br><br>libtest_m1_1.a:table.o use_table_m1.o<br>
$(AR) rcs $@ $^ <br><br>libtest_m2_1.a:table.o use_table_m2.o<br> $(AR) rcs $@ $^ <br><br>table.o:table.c<br> $(CC) -c $(CFLAG) $^ -o $@<br><br>test.o:test.c<br> $(CC) -c $(CFLAG) $^ -o $@<br><br>use_table_m1.o:use_table_m1.s<br>
$(CC) -c -integrated-as $(CFLAG) $^ -o $@<br><br>use_table_m2.o:use_table_m2.s<br> $(CC) -c -integrated-as $(CFLAG) $^ -o $@<br><br>clean:<br> rm -f *.o libtest* test_d_m* *~ test_s_m*<br><br>/* == end Makefile == */<br>
<br>(all of these files are also available at <a href="http://dl.dropbox.com/u/13855163/ill_text_relocation_test.tar.gz">http://dl.dropbox.com/u/13855163/ill_text_relocation_test.tar.gz</a>)<br><br>Best Regards,<br>Ashi<br>
<br></div></div>