4 Replies Latest reply on Sep 6, 2012 10:54 AM by ricardo_anguiano

    Having problems with Sourcery linker loading Android libraries

    roger_r_cruz

      I have been using Sourcery Code Bench version arm-2011.09-70-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 to compile an application for Android.  This toolchain worked flawlessly for me in doing the compilation and execution as long as I specified that the application use the Sourcery linker.  I copied the runtime C libraries and linker to the Android emulator prior to the application's execution.  As I said this has worked well.

       

      I then changed my application link against another library (call it libA) which uses OpenGL.  I have the source code to libA and it requires being compiled with Android's NDK.  The compilation of libA goes well as expected and I copied to the Android emulator where it can successfully be found by the Sourcery linker as it attempts to load (and resolve dependencies) of my application.

       

      The problem I am seeing with the Sourcery linker is when it attempts to resolve the dependencies of libA which are depedencies against NDK libraries.  The linker attempts to load libc and it "hangs" (ie, the application does not show any more output).  Under the debugger and with LD_DEBUG_OUTPUT enabled, I can see the linker is calling libc.so's init routine but it never returns.  I see similar problems with other libraries (libdl.so but not libGLESv1_CM.so or libcutils.so, weird)

       

       

      /data/bin/myapp -> /data/lib/libA.so -> /system/lib/libGLESv1_CM.so -> /system/lib/libcutils.so -> /system/lib/libc.so


             375: binding file  /data/bine/myapp [0] to  /data/lib/libA.so [0]: normal symbol `<some symbol being resolved correctly'

             375:

             375: calling init: /system/lib/libc.so

             375:

       

       

      Backtrace where my app does a dlopen of the shared library (libA) built with NDK

      (gdb) where

      #0  0xfffffffe in ?? ()

      #1  0x4000fc7c in call_init () from /data/lib/ld-linux.so.3

      #2  0x4000fe5c in _dl_init_internal () from /data/lib/ld-linux.so.3

      #3  0x400147e8 in dl_open_worker () from /data/lib/ld-linux.so.3

      #4  0x4000faf4 in _dl_catch_error () from /data/lib/ld-linux.so.3

      #5  0x40014048 in _dl_open () from /data/lib/ld-linux.so.3

      #6  0x42487bc4 in dlopen_doit () from /data/lib/libdl.so.2

      #7  0x4000faf4 in _dl_catch_error () from /data/lib/ld-linux.so.3 #8  0x424882b8 in _dlerror_run () from /data/lib/libdl.so.2

      #9  0x42487c8c in dlopen@@GLIBC_2.4 () from /data/lib/libdl.so.2

       

      So my questions,

      1) Has anyone have a clue on what is going on or how to debug it? 
      2) Has anyone had any success in getting a Sourcery-compiled application load a library compiled with NDK?  If so, how?


      Thanks in advance
      Roger R. Cruz
        • 1. Re: Having problems with Sourcery linker loading Android libraries
          roger_r_cruz

          One more piece of data that may be useful.  I have dumped the ELF headers from the libc in Android and it appears to lack the init/fini sections that are present in Sourcery's libc.  I don't know if Sourcery's linker expects a shared library to have specific sections and that is why it "hangs" as shown before.  Another piece of info is that I attempted to run my Sourcery-compiled application without specifying the Sourcery linker (btw, this is how I do that -Wl,--dynamic-linker /data/lib/ld-linux.so.3 where ld-linux.so.3 was copied from the Sourcery toolchain directory to the Android emulator) and so my application uses /system/bin/linker (the default Android linker).  When I do this, the Android linker fails to load my application because it doesn't recognize certain relocation type of the Sourcery-generated ELF headers

           

          820 unknown reloc type 19 @ 0x40190138 (1187))))CANNOT LINK EXECUTABLE

           

          Android's libc headers.  Note lack of .init and .fini sections.

           

          Section Headers:

            [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al

            [ 0]                   NULL            00000000 000000 000000 00      0   0  0

            [ 1] .hash             HASH            000000f4 0000f4 002288 04   A  2   0  4

            [ 2] .dynsym           DYNSYM          0000237c 00237c 004990 10   A  3   0  4

            [ 3] .dynstr           STRTAB          00006d0c 006d0c 003717 00   A  0   0  1

            [ 4] .rel.plt          REL             0000a424 00a424 000030 08   A  2   2  4

            [ 5] .rel.dyn          REL             0000a454 00a454 000008 08   A  2   2  4

            [ 6] .plt              PROGBITS        0000aecc 00aecc 00005c 00  AX  0   0  4

            [ 7] .text             PROGBITS        0000af30 00af30 02c5dc 00  AX  0   0 16

            [ 8] .rodata           PROGBITS        00037510 037510 0066ab 00   A  0   0  8

            [ 9] .ARM.extab        PROGBITS        0003dbbc 03dbbc 000288 00   A  0   0  4

            [10] .ARM.exidx        ARM_EXIDX       0003de44 03de44 001908 08   A  7   0  4

            [11] .init_array       INIT_ARRAY      00040000 040000 000010 00  WA  0   0  4

            [12] .fini_array       FINI_ARRAY      00040010 040010 00000c 00  WA  0   0  1

            [13] .data.rel.ro      PROGBITS        00040020 040020 001348 00  WA  0   0 32

            [14] .dynamic          DYNAMIC         00041368 041368 0000b8 08  WA  3   0  4

            [15] .got              PROGBITS        00041420 041420 000108 00  WA  0   0  4

            [16] .data             PROGBITS        00041540 041540 001214 00  WA  0   0 32

            [17] .bss              NOBITS          00042760 042760 00b35c 00  WA  0   0 16

            [18] .ident            PROGBITS        00000000 042760 0001ee 00      0   0  1

            [19] .note.gnu.gold-ve NOTE            00000000 042950 000018 00      0   0  4

            [20] .ARM.attributes   ARM_ATTRIBUTES  00000000 042968 000027 00      0   0  1

            [21] .shstrtab         STRTAB          00000000 04298f 0000c3 00      0   0  1

          • 2. Re: Having problems with Sourcery linker loading Android libraries
            ricardo_anguiano

            Hello Roger,

             

            The short answer is that using Sourcery CodeBench with an unmodified Android OS installation is not supported.  More details here:

             

            https://sourcery.mentor.com/GNUToolchain/kbentry113

             

            While it won't solve your problem, I wanted to point out that the latest release of Sourcery CodeBench Lite Edition for ARM GNU/Linux is 2012.03-57.

             

            Thanks,

            Ricardo Anguiano

            Mentor Graphics/CodeSourcery

            Embedded Software Division

            +1-510-354-6774

            • 3. Re: Having problems with Sourcery linker loading Android libraries
              roger_r_cruz

              Hi Ricardo,

               

              I have done more debugging and I can see why the Sourcery linker crashes when it loads libc or libdl.so from Android.  It is parsing the DT_INIT_ARRAY from those libraries and it does not check that the address it is jumping to is valid.  Android puts in -1 or 0 to indicate invalid entries in the array but Sourcery's linker does not check for it.  I believe I can easily change the sourcery linker to deal with that if I only knew how to build it.  I downloaded the CodeBench source code but I am having a hard time building the linker.  Is there a way to quickly build the linker with the CodeBench tools themselves?

               

              Roger

              • 4. Re: Having problems with Sourcery linker loading Android libraries
                ricardo_anguiano

                Hello Roger,

                 

                Sorry, while we provide the source code to our tools, we do not support building our tools from source code.

                 

                Sincerely,

                Ricardo Anguiano

                Mentor Graphics/CodeSourcery

                Embedded Software Division

                +1-510-354-6774