Compiling NDK program with .lds script

colordancer

I'm writing a NDK program and I want to compile it with my own lds script to organize the output's structure.

As a test program, it's the simplest hello word sample:

#include <stdio.h>
extern int main(int argc, char *argv[])
{
    printf("hello jni\r\n");
    return 0;
}

Part of Android.mk:

#LOCAL_LDFLAGS += -fuse-ld=gold
LOCAL_LDFLAGS += -fuse-ld=bfd
LOCAL_LDFLAGS += -T$(LOCAL_PATH)/test.lds

test.lds:

OUTPUT_FORMAT( "elf32-littlearm" , "elf32-littlearm" , "elf32-littlearm" ) 
OUTPUT_ARCH(arm)
SECTIONS
{
    .text : {
        *(.text)
    }
    .data : {*(.data)}
    .bss : {*(.bss)}
}

So, if I use bfd LOCAL_LDFLAGS += -fuse-ld=bfd to link, the problem is all the sections defined in lds are not linked into the output: There are 10 section headers, starting at offset 0x818c:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .interp           PROGBITS        00000000 008000 000013 00   A  0   0  1
  [ 2] .dynsym           DYNSYM          00000014 008014 000000 10   A  3   1  4
  [ 3] .dynstr           STRTAB          00000014 008014 000027 00   A  0   0  1
  [ 4] .hash             HASH            0000003c 00803c 00000c 04   A  2   0  4
  [ 5] .got.plt          PROGBITS        00000048 008048 00000c 04  WA  0   0  4
  [ 6] .dynamic          DYNAMIC         00000054 008054 000090 08  WA  3   0  4
  [ 7] .comment          PROGBITS        00000000 0080e4 000025 01  MS  0   0  1
  [ 8] .ARM.attributes   ARM_ATTRIBUTES  00000000 008109 00002d 00      0   0  1
  [ 9] .shstrtab         STRTAB          00000000 008136 000054 00      0   0  1

Then I use ld.gold LOCAL_LDFLAGS += -fuse-ld=gold to link, more problems came out. The first problem is a linking error:

[armeabi] Compile++ thumb: testlds <= main.cpp
[armeabi] Executable     : testlds
/media/android/android-ndk-r9b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.gold: error: multiple SHT_ARM_EXIDX sections .ARM.exidx.text.startup.main and .ARM.exidx in a non-relocatable link
/media/android/android-ndk-r9b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.gold: warning: unwinding may not work because EXIDX input section 6 of /media/android/android-ndk-r9b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/libgcc.a(unwind-arm.o) is not in EXIDX output section
/media/android/android-ndk-r9b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.gold: warning: unwinding may not work because EXIDX input section 6 of /media/android/android-ndk-r9b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/libgcc.a(pr-support.o) is not in EXIDX output section
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/testlds] Error 1

I searched with the keyword multiple SHT_ARM_EXIDX sections and find some comments in gold's source:

-      Output_section* os = layout->find_output_section(".ARM.exidx");
-      if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
+      Arm_output_section<big_endian>* exidx_output_section = NULL;
+      for (Layout::Section_list::const_iterator p =
+        layout->section_list().begin();
+      p != layout->section_list().end();
+      ++p)
+   if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
+     {
+       if (exidx_output_section == NULL)
+         exidx_output_section =
+       Arm_output_section<big_endian>::as_arm_output_section(*p);
+       else
+         // We cannot handle this now.
+         gold_error(_("multiple SHT_ARM_EXIDX sections %s and %s in a "
+              "non-relocatable link"),
+             exidx_output_section->name(),
+             (*p)->name());
+     }

Then I modified my lds a bit:

SECTIONS
{
    .text : {
        *(.text)
        *(.ARM.exidx)
    }
    .data : {*(.data)}
    .bss : {*(.bss)}
}

This time, compiling sucessed. But my program could not be executed. I open the program with IDA and I found that the entry function is not pointed to the main function correctly.

I'm totally lost now. How could I compile my NDK program with a certain lds?

colordancer

I found AOSP's linker script in NDK and I used it to compile with my source. It's OK then. So I believe it's the problem with my LDS.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related