Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android NDK产生不合理的大二进制文件,如何优化。那么大小呢?_Android_Android Ndk - Fatal编程技术网

Android NDK产生不合理的大二进制文件,如何优化。那么大小呢?

Android NDK产生不合理的大二进制文件,如何优化。那么大小呢?,android,android-ndk,Android,Android Ndk,我注意到Android NDK(r6b在我的例子中)产生了不合理的大结果.so文件。例如,在我的例子中,我有150到200行C++代码(6个本地方法和3个C++最简单类),这个本地代码产生 60kb(!)。因此启用异常和RTTI或12KB。因此,禁用异常和RTTI。只需检查一下,我已经编译了NDK包中包含的hello jni示例,并获得了10kb。因此,对于这个示例中的单行本机方法 在我看来,对于移动平台来说,这是不合理的开销(在我的桌面上,可比大小代码产生的开销要少10-15倍,所以) 我应该

我注意到Android NDK(r6b在我的例子中)产生了不合理的大结果.so文件。例如,在我的例子中,我有150到200行C++代码(6个本地方法和3个C++最简单类),这个本地代码产生<> 60kb(!)<强>。因此启用异常和RTTI或12KB。因此,禁用异常和RTTI。只需检查一下,我已经编译了NDK包中包含的hello jni示例,并获得了10kb。因此,对于这个示例中的单行本机方法

在我看来,对于移动平台来说,这是不合理的开销(在我的桌面上,可比大小代码产生的开销要少10-15倍,所以)

  • 我应该知道减少二进制文件大小的诀窍吗
  • 为什么只有C语言的代码会有这样的开销
  • <>为什么有额外的开销,C++代码有异常和RTTI?< /LI> 更新#1:NDK中hello jni示例的readelf输出

    您可以看到与<强>乔尔F 的区别,尤其是关于栈展开(C++异常)的< < /P> 更新#2 问题在于NDKR6B中包含的工具链,特别是关于链接器的问题。感谢@Joel F提供有关先前NDK版本的胶水。我在NDKR6B旁边安装了NDKR5C,并比较了结果。编译器通过两个工具链生成相同的对象文件,但链接后的结果不同。

    edit3 我能够用NDKR6B复制10KB hello jni二进制文件。我发现了一个可怕的黑客。基本上,将这一行放在您的一个文件中:

    char __aeabi_unwind_cpp_pr0[0];
    
    但是您的代码将缺少异常处理(我认为这是
    -fno异常的要点)
    …)

    无论如何,这将libhello-jni.so的字节数降到2228字节。仍然比r5c大,但比10KB小很多

    编辑2 是的,在你的读写输出中,你似乎有很多C++相关的开销。也许他们在r5c和r6b之间改变了什么?在R5C的hello JN.C.

    中没有C++代码。 我有160行C代码,NDKR5C将其缩减为3.8KB。请尝试以下操作,以了解是什么导致了该尺寸:

    /path/to/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/<platform>/bin/arm-linux-androideabi-readelf -a libmylib.so
    
    构建输出:(显然对行长度或其他方面有限制)

    来自
    readelf-a libhello jni.so的输出:

    ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: ARM Version: 0x1 Entry point address: 0x2dc Start of program headers: 52 (bytes into file) Start of section headers: 1108 (bytes into file) Flags: 0x5000002, has entry point, Version5 EABI Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 5 Size of section headers: 40 (bytes) Number of section headers: 12 Section header string table index: 11 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 000000d4 0000d4 00004c 04 A 2 0 4 [ 2] .dynsym DYNSYM 00000120 000120 0000e0 10 A 3 2 4 [ 3] .dynstr STRTAB 00000200 000200 0000db 00 A 0 0 1 [ 4] .text PROGBITS 000002dc 0002dc 00002c 00 AX 0 0 4 [ 5] .rodata PROGBITS 00000308 000308 000014 00 A 0 0 4 [ 6] .ARM.exidx ARM_EXIDX 0000031c 00031c 000008 00 AL 4 0 4 [ 7] .dynamic DYNAMIC 00001324 000324 000088 08 WA 3 0 4 [ 8] .got PROGBITS 000013ac 0003ac 00000c 04 WA 0 0 4 [ 9] .comment PROGBITS 00000000 0003b8 000012 00 0 0 1 [10] .ARM.attributes ARM_ATTRIBUTES 00000000 0003ca 000029 00 0 0 1 [11] .shstrtab STRTAB 00000000 0003f3 000061 00 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) There are no section groups in this file. Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align EXIDX 0x00031c 0x0000031c 0x0000031c 0x00008 0x00008 R 0x4 LOAD 0x000000 0x00000000 0x00000000 0x00324 0x00324 R E 0x1000 LOAD 0x000324 0x00001324 0x00001324 0x00094 0x00094 RW 0x1000 DYNAMIC 0x000324 0x00001324 0x00001324 0x00088 0x00088 RW 0x4 GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4 Section to Segment mapping: Segment Sections... 00 .ARM.exidx 01 .hash .dynsym .dynstr .text .rodata .ARM.exidx 02 .dynamic .got 03 .dynamic 04 Dynamic section at offset 0x324 contains 12 entries: Tag Type Name/Value 0x00000001 (NEEDED) Shared library: [libc.so] 0x00000001 (NEEDED) Shared library: [libstdc++.so] 0x00000001 (NEEDED) Shared library: [libm.so] 0x00000001 (NEEDED) Shared library: [libdl.so] 0x0000000e (SONAME) Library soname: [libhello-jni.so] 0x00000010 (SYMBOLIC) 0x0 0x00000004 (HASH) 0xd4 0x00000005 (STRTAB) 0x200 0x00000006 (SYMTAB) 0x120 0x0000000a (STRSZ) 219 (bytes) 0x0000000b (SYMENT) 16 (bytes) 0x00000000 (NULL) 0x0 There are no relocations in this file. There are no unwind sections in this file. Symbol table '.dynsym' contains 14 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 000002dc 0 SECTION LOCAL DEFAULT 4 2: 00000324 0 NOTYPE GLOBAL DEFAULT ABS __exidx_end 3: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_unwind_cpp_pr0 4: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS _bss_end__ 5: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS __bss_start__ 6: 0000031c 0 NOTYPE GLOBAL DEFAULT ABS __exidx_start 7: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS __bss_end__ 8: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS __bss_start 9: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS __end__ 10: 000002dd 44 FUNC GLOBAL DEFAULT 4 Java_com_example_hellojni 11: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS _edata 12: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS _end 13: 000013b8 0 NOTYPE GLOBAL DEFAULT 8 __data_start Histogram for bucket list length (total of 3 buckets): Length Number % of total Coverage 0 0 ( 0.0%) 1 0 ( 0.0%) 0.0% 2 1 ( 33.3%) 16.7% 3 0 ( 0.0%) 16.7% 4 1 ( 33.3%) 50.0% 5 0 ( 0.0%) 50.0% 6 1 ( 33.3%) 100.0% No version information found in this file. Attribute Section: aeabi File Attributes Tag_CPU_name: "5TE" Tag_CPU_arch: v5TE Tag_THUMB_ISA_use: Thumb-1 Tag_ABI_PCS_wchar_t: 4 Tag_ABI_FP_denormal: Needed Tag_ABI_FP_exceptions: Needed Tag_ABI_FP_number_model: IEEE 754 Tag_ABI_align8_needed: Yes Tag_ABI_align8_preserved: Yes, except leaf SP Tag_ABI_enum_size: int Tag_ABI_optimization_goals: Aggressive Debug ELF标头: 魔术:7f 45 4c 46 01 01 00 00 类别:ELF32 数据:2的补码,小尾端 版本:1(当前版本) OS/ABI:UNIX-SystemV ABI版本:0 类型:DYN(共享对象文件) 机器:手臂 版本:0x1 入口点地址:0x2dc 程序头的开始:52(文件中的字节) 节头开始:1108(字节到文件) 标志:0x5000002,具有入口点,版本5 EABI 此标头的大小:52(字节) 程序头的大小:32(字节) 程序头数:5 节标题的大小:40(字节) 节头数:12 节头字符串表索引:11 章节标题: [Nr]名称类型地址尺寸ES Flg Lk Inf Al [0]空0000000000000000000 [1].哈希000000 D4 0000d4 00004c 04 A 2 0 4 [2].dynsym dynsym 00000120 000120 0000e0 10 A 3 2 4 [3].dynstr STRTAB 000002000002000000DB 00 A 0 0 1 [4].文本程序000002dc 0002dc 00002c 00 AX 0 0 4 [5]。rodata PROGBITS 00000308 000308 000014 00 A 0 0 4 [6].ARM.exidx ARM_exidx 00000 31C 00031c 000008 00 AL 4 0 4 [7].动态00001324 000088 WA 3 0 4 [8]。获取程序000013C 00003AC 00000c 04 WA 0 4 [9]。注释程序00000000 0003b8 00001200 01 [10] .ARM.attributes ARM_attributes 00000000 0003ca 000029 00 0 1 [11] .shstrtab STRTAB 00000000 0003f3 000061 00 0 1 标志的关键点: W(写入)、A(分配)、X(执行)、M(合并)、S(字符串) I(信息),L(链接顺序),G(组),x(未知) O(需要额外的操作系统处理)O(特定于操作系统),p(特定于处理器) 此文件中没有节组。 程序标题: 键入Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align EXIDX 0x00031c 0x000031C 0x0000031c 0x00008 0x00008 R 0x4 加载0x0000000x00000000 0x00000000 0x00324 0x00324 R E 0x1000 加载0x000324 0x00001324 0x00001324 0x00094 0x00094 RW 0x1000 动态0x000324 0x00001324 0x00001324 0x00088 0x00088 RW 0x4 GNU_堆栈0x0000000x00000000 0x00000000 0x00000 0x00000 RW 0x4 节到段映射: 分段段。。。 00.ARM.exidx 01.hash.dynsym.dynstr.text.rodata.ARM.exidx 02.动态,明白了吗 03.动态 04 偏移量0x324处的动态部分包含12个条目: 标记类型名称/值 0x00000001(需要)共享库:[libc.so] 0x00000001(需要)共享库:[libstdc++.so] 0x00000001(需要)共享库:[libm.so] 0x00000001(需要)共享库:[libdl.so] 0x0000000e(SONAME)库SONAME:[libhello jni.so] 0x00000010(符号)0x0 0x00000004(哈希)0xd4 0x00000005(STRTAB)0x200 0x00000006(SYMTAB)0x120 0x0000000a(STRSZ)
    ndk-build V=1
    
    ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: ARM Version: 0x1 Entry point address: 0x2dc Start of program headers: 52 (bytes into file) Start of section headers: 1108 (bytes into file) Flags: 0x5000002, has entry point, Version5 EABI Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 5 Size of section headers: 40 (bytes) Number of section headers: 12 Section header string table index: 11 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 000000d4 0000d4 00004c 04 A 2 0 4 [ 2] .dynsym DYNSYM 00000120 000120 0000e0 10 A 3 2 4 [ 3] .dynstr STRTAB 00000200 000200 0000db 00 A 0 0 1 [ 4] .text PROGBITS 000002dc 0002dc 00002c 00 AX 0 0 4 [ 5] .rodata PROGBITS 00000308 000308 000014 00 A 0 0 4 [ 6] .ARM.exidx ARM_EXIDX 0000031c 00031c 000008 00 AL 4 0 4 [ 7] .dynamic DYNAMIC 00001324 000324 000088 08 WA 3 0 4 [ 8] .got PROGBITS 000013ac 0003ac 00000c 04 WA 0 0 4 [ 9] .comment PROGBITS 00000000 0003b8 000012 00 0 0 1 [10] .ARM.attributes ARM_ATTRIBUTES 00000000 0003ca 000029 00 0 0 1 [11] .shstrtab STRTAB 00000000 0003f3 000061 00 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) There are no section groups in this file. Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align EXIDX 0x00031c 0x0000031c 0x0000031c 0x00008 0x00008 R 0x4 LOAD 0x000000 0x00000000 0x00000000 0x00324 0x00324 R E 0x1000 LOAD 0x000324 0x00001324 0x00001324 0x00094 0x00094 RW 0x1000 DYNAMIC 0x000324 0x00001324 0x00001324 0x00088 0x00088 RW 0x4 GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4 Section to Segment mapping: Segment Sections... 00 .ARM.exidx 01 .hash .dynsym .dynstr .text .rodata .ARM.exidx 02 .dynamic .got 03 .dynamic 04 Dynamic section at offset 0x324 contains 12 entries: Tag Type Name/Value 0x00000001 (NEEDED) Shared library: [libc.so] 0x00000001 (NEEDED) Shared library: [libstdc++.so] 0x00000001 (NEEDED) Shared library: [libm.so] 0x00000001 (NEEDED) Shared library: [libdl.so] 0x0000000e (SONAME) Library soname: [libhello-jni.so] 0x00000010 (SYMBOLIC) 0x0 0x00000004 (HASH) 0xd4 0x00000005 (STRTAB) 0x200 0x00000006 (SYMTAB) 0x120 0x0000000a (STRSZ) 219 (bytes) 0x0000000b (SYMENT) 16 (bytes) 0x00000000 (NULL) 0x0 There are no relocations in this file. There are no unwind sections in this file. Symbol table '.dynsym' contains 14 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 000002dc 0 SECTION LOCAL DEFAULT 4 2: 00000324 0 NOTYPE GLOBAL DEFAULT ABS __exidx_end 3: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_unwind_cpp_pr0 4: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS _bss_end__ 5: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS __bss_start__ 6: 0000031c 0 NOTYPE GLOBAL DEFAULT ABS __exidx_start 7: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS __bss_end__ 8: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS __bss_start 9: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS __end__ 10: 000002dd 44 FUNC GLOBAL DEFAULT 4 Java_com_example_hellojni 11: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS _edata 12: 000013b8 0 NOTYPE GLOBAL DEFAULT ABS _end 13: 000013b8 0 NOTYPE GLOBAL DEFAULT 8 __data_start Histogram for bucket list length (total of 3 buckets): Length Number % of total Coverage 0 0 ( 0.0%) 1 0 ( 0.0%) 0.0% 2 1 ( 33.3%) 16.7% 3 0 ( 0.0%) 16.7% 4 1 ( 33.3%) 50.0% 5 0 ( 0.0%) 50.0% 6 1 ( 33.3%) 100.0% No version information found in this file. Attribute Section: aeabi File Attributes Tag_CPU_name: "5TE" Tag_CPU_arch: v5TE Tag_THUMB_ISA_use: Thumb-1 Tag_ABI_PCS_wchar_t: 4 Tag_ABI_FP_denormal: Needed Tag_ABI_FP_exceptions: Needed Tag_ABI_FP_number_model: IEEE 754 Tag_ABI_align8_needed: Yes Tag_ABI_align8_preserved: Yes, except leaf SP Tag_ABI_enum_size: int Tag_ABI_optimization_goals: Aggressive Debug