This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem in generating position independent code with out TEXTREL

Note: This was originally posted on 12th August 2010 at http://forums.arm.com

Hi Experts,

Issue:Not able to remove TEXTREL from our static library generated with --apcs /fpic option using armcc(RVCT 4.0 build 771  tool chain)

Description:
    We compiled our codebase(some modules with handwritten assembly) using armcc(RVCT 4.0 build 771  tool chain) to get static libraries, and then these are used by gstreamer module which further compiles using the above generated libraries with wrappers using GCC tool chain and creates shared objects(.so).

Even though we use equivalent --apcs /fpic compile time option during static library creation to remove position independent code, the TEXTREL are not going from library.
When we disable the (handwritten assembly) in the above mentioned codebase, TEXTRELs go away.
As there are lot of handwritten ARM assembly functions,its difficult to enable one by one and compile and check. :unsure:

So,Experts please throw some light on this issue.


Thanks In Advance,
satish
  • Note: This was originally posted on 13th August 2010 at http://forums.arm.com

    I'm not sure I know exactly what you mean by TEXTREL.  I'm guessing it's a non-PC-relative relocation from a .text (or RO) section.  (Such a relocation would have to be performed at load time (by a dynamic linker) for a shared object.)

    Unlike the compiler, the assembler has little or no choice about the relocations it produces.  If you have something like
    ...
    IMPORT foo
    ...
    LDR r0,=foo
    or
    DCD foo
    ...

    then the assembler has to produce a non-PC-relative relocation.

    When you tell armasm '--apcs /fpic' you are promising that it is appropriate to mark the RO sections object code as position-independent.  This is different from building C or C++ code where '--apcs /fpic' is telling the compiler to make sure the RO sections object code are position-independent.

    The assembler can't add the code needed to use a PC-relative relocation, but the compiler can.

    I don't think there's any solution besides finding out what asm constructs are producing the unwanted relocations and changing them.  (Using asm macros might make it easier to maintain.)
  • Note: This was originally posted on 16th August 2010 at http://forums.arm.com

    I'm not sure I know exactly what you mean by TEXTREL.  I'm guessing it's a non-PC-relative relocation from a .text (or RO) section.  (Such a relocation would have to be performed at load time (by a dynamic linker) for a shared object.)

    Unlike the compiler, the assembler has little or no choice about the relocations it produces.  If you have something like
    ...
    IMPORT foo
    ...
    LDR r0,=foo
    or
    DCD foo
    ...

    then the assembler has to produce a non-PC-relative relocation.

    When you tell armasm '--apcs /fpic' you are promising that it is appropriate to mark the RO sections object code as position-independent.  This is different from building C or C++ code where '--apcs /fpic' is telling the compiler to make sure the RO sections object code are position-independent.

    The assembler can't add the code needed to use a PC-relative relocation, but the compiler can.

    I don't think there's any solution besides finding out what asm constructs are producing the unwanted relocations and changing them.  (Using asm macros might make it easier to maintain.)



    Hi Scott,

       Thank you for the reply.I had removed all the IMPORTS from my assembly but still TEXTREL is coming in library.
    I am building handwritten assembly with armcc only(not sure whether it uses armasm internally).
    My assembly functions are in .c files in following fashion:

    CODE:
    __asm void func1()
    {

    }

    So can you tell me what else may be reason, for generation of TEXTREL in library.

    Thanks In Advance,
    satish
  • Note: This was originally posted on 16th August 2010 at http://forums.arm.com

    Thank you for the reply.I had removed all the IMPORTS from my assembly but still TEXTREL is coming in library.
    I am building handwritten assembly with armcc only(not sure whether it uses armasm internally).
    My assembly functions are in .c files in following fashion:

    CODE:
    __asm void func1()
    {

    }

    So can you tell me what else may be reason, for generation of TEXTREL in library.


    You're using what's known as "embedded assembly".  What you write inside the embedded asm functions has pretty much the same rules as a .s file, but armcc will generate some EXPORT/PROC/ENDP directives for you and some IMPORT directives if you're using __cpp(...).

    It's not using the IMPORT directive per se that is generating the relocation; it's what you do with the imported symbol (DCD or LDR rx,= in my example above).  If know the symbols being used by the unwanted relocations and can post the embedded asm lines that mention them; we might be able to suggest what you could do instead.
  • Note: This was originally posted on 17th August 2010 at http://forums.arm.com

    You're using what's known as "embedded assembly".  What you write inside the embedded asm functions has pretty much the same rules as a .s file, but armcc will generate some EXPORT/PROC/ENDP directives for you and some IMPORT directives if you're using __cpp(...).

    It's not using the IMPORT directive per se that is generating the relocation; it's what you do with the imported symbol (DCD or LDR rx,= in my example above).  If know the symbols being used by the unwanted relocations and can post the embedded asm lines that mention them; we might be able to suggest what you could do instead.


    I am also facing similar relocation issue. i am facing issue in normal c code where assembly has been disabled. Code is compiled using RVCT4.0 build 771. I tried with all possible options like --apcs /fpic,  --apcs=/ropi --lower_ropi etc but it has no effect.
    Is there any utility or method using which we can identify, the parts of code responsible for these?
  • Note: This was originally posted on 6th January 2011 at http://forums.arm.com

    Sorry, I didn't realize you were using --no_hide_all.  If you're accessing non-hidden symbols then things are more complicated -- more complicated than I understand.  You have to use the GOT (Global Object Table).

    The 'RELOC 96, table_1' is "manually" generating an R_ARM_GOT_PREL relocation at the previous line.  See http://infocenter.ar...b/CIADEEEC.html for a description of the RELOC directive.

    Just adding the RELOC 96 to my embedded asm example is not going to work -- notice how the compiler is using the 'LDR rx, [pc, rx]' (and a different offset in the DCD) instead of 'ADD rx, pc, rx'.

    Details of R_ARM_GOT_PREL are in the BPABI document http://infocenter.ar...037b/index.html.

    Good luck.
  • Note: This was originally posted on 3rd January 2011 at http://forums.arm.com

    Hi Experts,

    I was able te resolve the TEXTREL issues(by avoiding functions calls using LDR and DCD/DCW...for data) in some code bases with the solutions provided by Scott & others............
    Now i am working on some other code base where i am facing TEXTREL issues due to access of tables.
    Please go through the below mentioned sample program which replicates my issues.........

    sample program:

    short int table[30] = {1,2,3,4,5,.......,30};

    void foo {
    __asm {

    LDR r4,=table
    ..............
    ...............
    ..............

    }

    (with --apcs /fpic option) - When i use C code of the above corrsponding assembly then compiler is taking care of TEXTREL issue(able to remove TEXTREL by using RELOC),but when i access the table using embedded assembly, assembler is not able to generate the TEXTREL free code.Please throw some light on this issue..........

    Thanks In Advance,
    satish
  • Note: This was originally posted on 3rd January 2011 at http://forums.arm.com

    Hi Experts,

            I was able te resolve the TEXTREL issues(by avoiding functions calls using LDR and DCD/DCW...for data) in some code bases with the solutions provided by Scott & others............
    Now i am working on some other code base where i am facing TEXTREL issues due to access of tables.
    Please go through the below mentioned sample program which replicates my issues.........

    sample program:

    short int table[30] = {1,2,3,4,5,.......,30};

    void foo {
    __asm {

           LDR r4,=table
          ..............
           ...............
          ..............

    }

    (with --apcs /fpic option) - When i use C code of the above corrsponding assembly then compiler is taking care of TEXTREL issue(able to remove TEXTREL by using RELOC),but when i access the table using embedded assembly, assembler is not able to generate the TEXTREL free code.Please throw some light on this issue..........

    Thanks In Advance,
    satish
  • Note: This was originally posted on 4th January 2011 at http://forums.arm.com

    You show inline asm:

    void foo {
    __asm {
       ...
    }


    but I think you must actually be using embedded asm.


    __asm void foo() {
      ...
    }



    The embedded assembler (or normal assembler) can't add the code needed to use a PC-relative relocation, you have to write it yourself.  For example, building with 'armcc -c --apcs /fpic':

    extern int table[] = { 1, 2, 3 };
    __asm void *foo() {
      import table /* should not really be needed, but is */
      ldr r0, lbl
      add r0, r0, pc
      bx lr
    lbl
      dcd table - .
    }


    If you're really using inline asm, please post a small example that compiles and and the compile options you are using.
  • Note: This was originally posted on 4th January 2011 at http://forums.arm.com

    Hi Scott,

              You are right....its embedded assembly.....Its not inline assembly.
    What is the solution for such scenarios?
    Is manual addition of code for PC-relative relocation is the only solution?
    If so,is this solution recomended?
    If so, can you please specify me the process to write this piece of PC-relative relocation code with an example.

    Thanks In Advance,
    satish

    [font="Arial"][size="2"][/size][/font]
  • Note: This was originally posted on 5th January 2011 at http://forums.arm.com

    The most maintainable solution would probably be to use C instead of embedded assembly.  If you're stuck using embedded assembly then manually changing/adding code is the only way to make it position independent.  The changes needed depends on what the non-PI code is doing, but usually involve adding the PC to some offset.  Here's a slightly improved/expanded example:

    In C:

    extern int table[] = { 1, 2, 3};
    int foo_c(int i) { return table[i]; }


    Here's a PI version in embedded asm (for ARM state):


    __asm int foo_asm() {
      ldr r1, dlbl
    ulbl
      add r1, r1, pc
      ldr r0,[r1,r0,lsl #2]
      bx lr
      align
    dlbl
      dcd __cpp(table) - ulbl - 8
    }


    'ulbl' is the labelling the position "using" ADD instruction.  'dlbl' is labelling the DCD. I think in Thumb state 'ulbl - 8' needs to be changed to 'ulbl -4'.

    The non-PI asm version would be:


    __asm int foo_asm_non_PI() {
      ldr r1, dlbl
      ldr r0,[r1,r0,lsl #2]
      bx lr
      align
    dlbl
      dcd __cpp(table)
    }


    Which is pretty much the same as:


    __asm int foo_asm_non_PI2() {
       ldr r1, =__cpp(table)
       ldr r0,[r1,r0,lsl #2]
       bx lr
    }
  • Note: This was originally posted on 6th January 2011 at http://forums.arm.com

    Hi Scott,



    I need to use embedded assembly only. So, I had tested the sample code given by you and here are my observations:

    The below mentioned PI version code is also generating TEXTREL's.....



    __asm int foo_asm() {
      ldr r1, dlbl
    ulbl
      add r1, r1, pc
      ldr r0,[r1,r0,lsl #2]
      bx lr
      align
    dlbl
      dcd __cpp(table) - ulbl - 8
    }




    To analyze the behavior of arm compiler and embedded assembler, I had generated the test.tmp.s file from test.c file. I am unable to upload the files.There is error in uploading.So,i am copying the file contents:



    test.c file content:



    extern short int table_1[33];


    int foo(int i)
    {
        return table_1[i];
    }

    __asm int foo_asm(int i)
    {
    LDR r1, dlbl
    ulbl
    ADD r1, r1, pc
    LDR r0, [r1,r0,lsl #2]
    bx lr
    align
    dlbl
       DCD __cpp(table_1) - ulbl - 8
        RELOC 96, table_1  //if this is used then only TEXTREL is not generated
    }


    test.tmp.s  file content - this is the .s file which is genrated by arm compiler for above code.

    ; generated by ARM NEON C/C++ Compiler, RVCT4.0 [Build 771]
    ; commandline armcc [--arm -c -S --gnu --no_hide_all --library_interface=aeabi_glibc -oobj/test.tmp.s --cpu=Cortex-A8 --fpu=VFPv3 --apcs=/interwork//fpic --no_unaligned_access -O3 -Otime --vectorize --enum_is_int --wchar32 obj/test.tmp.c]
            ARM
            REQUIRE8
            PRESERVE8

            AREA ||.text||, CODE, READONLY, ALIGN=2

    ||foo|| PROC
            LDR      r1,|L0.20|
            LDR      r1,[pc,r1]
            ADD      r0,r1,r0,LSL #1
            LDRSH    r0,[r0,#0]
            BX       lr
            ENDP

    |L0.20|
            DCD      0x00000008                    ;Line 18
            RELOC 96, table_1                     ;Line 19

            AREA ||.arm_vfe_header||, DATA, READONLY, NOALLOC, ALIGN=2

            DCD      0x00000000

    ;*** Start embedded assembler ***

       #line 1 "obj/test.tmp.c"
      AREA ||.emb_text||, CODE, READONLY
        ARM
      EXPORT |foo_asm| [DYNAMIC]
      IMPORT |table_1| [DYNAMIC]
    #line 10
    |foo_asm| PROC
    #line 11

    LDR r1, dlbl
    ulbl
    ADD r1, r1, pc
    LDR r0, [r1,r0,lsl #2]
    bx lr
    align
    dlbl
    DCD  |table_1| - ulbl - 8
                                                              ;Line 44
    ENDP

    ;*** End   embedded assembler ***

            EXPORT ||foo|| [CODE,DYNAMIC]

            IMPORT ||Lib$$Request$$armlib|| [CODE,WEAK]
            IMPORT table_1 [DATA,DYNAMIC]

            ATTR FILESCOPE
            ATTR SETVALUE Tag_ABI_PCS_wchar_t,4
            ATTR SETVALUE Tag_ABI_enum_size,2
            ATTR SETVALUE Tag_ABI_optimization_goals,2
            ATTR SETSTRING Tag_conformance,"2.06"
            ATTR SETVALUE AV,18,1

            ASSERT {ENDIAN} = "little"
            ASSERT {INTER} = {TRUE}
            ASSERT {ROPI} = {TRUE}
            ASSERT {RWPI} = {FALSE}
            ASSERT {IEEE_FULL} = {FALSE}
            ASSERT {IEEE_PART} = {FALSE}
            ASSERT {IEEE_JAVA} = {FALSE}
            END



    As per my analysis,

    For c-code - " armcc compiler is able to produce TEXTREL free code for table access by using RELOC"(see Line No:18,19 in above test.tmp.s file content)

    For embedded assembly "" "assembler is not able to do the same......which results in TEXTREL's in code"...(see Line:44 "" where embedded assembler is not able to use RELOC)



    So, I had manually added the RELOC 96, table_1 in embedded assembly, then TEXTREL's are not generated.

    Is this the proper/recommended solution? Can I use the same for all tables?

    Can you please explain what is the significance of 96 in "RELOC 96, table_1"?





    Thanks In Advance,

    satish
  • Note: This was originally posted on 17th August 2010 at http://forums.arm.com

    I am also facing similar relocation issue. i am facing issue in normal c code where assembly has been disabled. Code is compiled using RVCT4.0 build 771. I tried with all possible options like --apcs /fpic,  --apcs=/ropi --lower_ropi etc but it has no effect.
    Is there any utility or method using which we can identify, the parts of code responsible for these?

    I'm not sure I understand the problem correctly.  Can you explain what symptoms and/or error messages you are seeing (with examples if possible)?

    Understanding relocation problems usually involves dumping the images and/or objects with 'fromelf -yr ...' or 'readelf -Dr ...'.  It can be a bit of a chore to go from a dynamic relocation in an image back to the object file it came from -- in the worst case you'll need the link map, too.
  • Note: This was originally posted on 17th August 2010 at http://forums.arm.com

    I'm not sure I understand the problem correctly.  Can you explain what symptoms and/or error messages you are seeing (with examples if possible)?

    Understanding relocation problems usually involves dumping the images and/or objects with 'fromelf -yr ...' or 'readelf -Dr ...'.  It can be a bit of a chore to go from a dynamic relocation in an image back to the object file it came from -- in the worst case you'll need the link map, too.


    I tried executing readelf -d ... ,
    This clearly showed there is a TEXTREL
    ...
    0x00000016 (TEXTREL)      0x0
    ...
    how can we link this to a paricular part of code?
  • Note: This was originally posted on 18th August 2010 at http://forums.arm.com

    I tried executing readelf -d ... ,
    This clearly showed there is a TEXTREL
    ...
    0x00000016 (TEXTREL)      0x0
    ...
    how can we link this to a paricular part of code?


    One thing to try, if you're using armlink, is adding '--diag_warning=6408,6409' to enable warnings when a non-PI object file is used when linking with --fpic.  Unfortunately it will warn even if there are no offending relocations.  [I've requested an enhancement to warn when DT_TEXTREL is set and give the object file info.]

    [I don't have a precise example for this next bit.]
    To find the offending relocation by hand, dump the code and dynamic relocations of the image:
    fromelf -cyr foo.so > foo.so.dump
    or
    objdump -dR foo.so > foo.so.dump
    Now find the dynamic relocations whose offsets are in read-only segments.  Use the offset to find the code being relocated.  Look above that location for the nearest symbol.  Hopefully this will be a unique function name which will lead you to the source.  Otherwise you might need to create a link map and use it to find which object file was placed at the offset.

    If the reason for the relocation is not clear from the source, post the relevant parts of the image dump (relocation info and code around the offset) and source.
  • Note: This was originally posted on 1st September 2010 at http://forums.arm.com

    I'm not sure I know exactly what you mean by TEXTREL.  I'm guessing it's a non-PC-relative relocation from a .text (or RO) section.  (Such a relocation would have to be performed at load time (by a dynamic linker) for a shared object.)

    Unlike the compiler, the assembler has little or no choice about the relocations it produces.  If you have something like
    ...
    IMPORT foo
    ...
    LDR r0,=foo
    or
    DCD foo
    ...

    then the assembler has to produce a non-PC-relative relocation.

    When you tell armasm '--apcs /fpic' you are promising that it is appropriate to mark the RO sections object code as position-independent.  This is different from building C or C++ code where '--apcs /fpic' is telling the compiler to make sure the RO sections object code are position-independent.

    The assembler can't add the code needed to use a PC-relative relocation, but the compiler can.

    I don't think there's any solution besides finding out what asm constructs are producing the unwanted relocations and changing them.  (Using asm macros might make it easier to maintain.)


    If we are specifying "--apcs /fpic" for compiling c/c++ code that has ARM inline assembly, Can we use r9 register as normal register?  Section 6.3 of APCS, says for rwpi , r9 cannot be used as normal register.  Can you clarify compiler behaviour for  --apcs /fpic and rwpi ?