Discussion:
Oddity with the gold linker
John Dallman
2018-04-24 14:49:15 UTC
Permalink
I'm trying to build some simple command-line tools to run in the adb shell.
I built a standalone toolchain from NDK 16b with this command:

ndk-bundle/build/tools/make_standalone_toolchain.py \
--arch arm64-v8a --api 21 --std libc++ \
--install-dir /local/kernel_lx86/toolchain


I have these directories on the front of my PATH, so that I can use the
clang from that toolchain and adb from the command line:

/local/kernel_lx86/toolchain/bin:/local/kernel_lx86/android-sdk/platform-tools


I encountered the problem where the BFD linker sets a flag that causes
warnings from the Android loader, thus:

WARNING: linker: /data/local/tmp/world.bfd: unsupported flags
DT_FLAGS_1=0x8000000


Searching for that found me the github discussion at
https://github.com/android-ndk/ndk/issues/602, and the simple answer seems
to be to use the -fuse-ld=gold option. So I did that, and found my
executable for a "hello world" program got about eleven times larger:

world.c is:

#include <stdio.h>

int main( int argc, char *argv[])
{
printf( "Hello, world\n");
return 0;
}


My commands are:

clang -fPIC -O -pie -fuse-ld=gold world.c -o world.gold
clang -fPIC -O -pie world.c -o world.bfd
ls -l world.c world.bfd world.gold


The sizes are:

-rwxrwxr-x 1 kerman ps 6000 Apr 24 14:47 world.bfd
-rw-rw-r-- 1 kerman ps 111 Apr 20 17:47 world.c
-rwxrwxr-x 1 kerman ps 68824 Apr 24 14:47 world.gold


Am I doing something wrong, or is this a gold bug?

Thanks,

John Dallman
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgnu%2BTWj9E0Ri2Q0f36TfgF5eKQy9LqOUmqznGCfCf_isQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
'Dan Albert' via android-ndk
2018-04-24 21:21:13 UTC
Permalink
Impressive.

If you're interested in keeping binary size down, you really ought to be
using the following flags:

CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections

And don't forget to run `strip --strip-unneeded` on the binary too. The
strip binaries are also in the toolchain directory.
Post by John Dallman
I'm trying to build some simple command-line tools to run in the adb
ndk-bundle/build/tools/make_standalone_toolchain.py \
--arch arm64-v8a --api 21 --std libc++ \
--install-dir /local/kernel_lx86/toolchain
I have these directories on the front of my PATH, so that I can use the
/local/kernel_lx86/toolchain/bin:/local/kernel_lx86/android-sdk/platform-tools
I encountered the problem where the BFD linker sets a flag that causes
WARNING: linker: /data/local/tmp/world.bfd: unsupported flags
DT_FLAGS_1=0x8000000
Searching for that found me the github discussion at
https://github.com/android-ndk/ndk/issues/602, and the simple answer
seems to be to use the -fuse-ld=gold option. So I did that, and found my
#include <stdio.h>
int main( int argc, char *argv[])
{
printf( "Hello, world\n");
return 0;
}
clang -fPIC -O -pie -fuse-ld=gold world.c -o world.gold
clang -fPIC -O -pie world.c -o world.bfd
ls -l world.c world.bfd world.gold
-rwxrwxr-x 1 kerman ps 6000 Apr 24 14:47 world.bfd
-rw-rw-r-- 1 kerman ps 111 Apr 20 17:47 world.c
-rwxrwxr-x 1 kerman ps 68824 Apr 24 14:47 world.gold
Am I doing something wrong, or is this a gold bug?
Thanks,
John Dallman
--
You received this message because you are subscribed to the Google Groups
"android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-ndk/CAH1xqgnu%2BTWj9E0Ri2Q0f36TfgF5eKQy9LqOUmqznGCfCf_isQ%40mail.gmail.com
<https://groups.google.com/d/msgid/android-ndk/CAH1xqgnu%2BTWj9E0Ri2Q0f36TfgF5eKQy9LqOUmqznGCfCf_isQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAFVaGhu%2BTPDNmVU4g-czZ7sT6z7O%2BmPE15riihPVCagEnT6bNg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
'Ryan Prichard' via android-ndk
2018-04-24 22:44:44 UTC
Permalink
It looks like gc-sections doesn't fix it:

$ cat main.c
int main(void) {}
$ /ssd2/stand-arm64-21-libc++-r16b/bin/clang main.c -Os -pie
-ffunction-sections -fdata-sections -Wl,--gc-sections -fuse-ld=bfd &&
/ssd2/stand-arm64-21-libc++-r16b/bin/aarch64-linux-android-strip
--strip-unneeded a.out && ls -l a.out
-rwxr-x--- 1 rprichard primarygroup 3792 Apr 24 15:29 a.out
$ /ssd2/stand-arm64-21-libc++-r16b/bin/clang main.c -Os -pie
-ffunction-sections -fdata-sections -Wl,--gc-sections -fuse-ld=gold &&
/ssd2/stand-arm64-21-libc++-r16b/bin/aarch64-linux-android-strip
--strip-unneeded a.out && ls -l a.out
-rwxr-x--- 1 rprichard primarygroup 67320 Apr 24 15:29 a.out

For gold, I don't see anything large in the section table or in the program
headers. Here are the headers:

Program Headers:
Type Offset VirtAddr PhysAddr FileSiz
MemSiz Flg Align
PHDR 0x000040 0x0000000000000040 0x0000000000000040 0x0001c0
0x0001c0 R 0x8
INTERP 0x000200 0x0000000000000200 0x0000000000000200 0x000015
0x000015 R 0x1
[Requesting program interpreter: /system/bin/linker64]
LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000530
0x000530 R E 0x10000
LOAD 0x00fda0 0x000000000001fda0 0x000000000001fda0 0x000278
0x000278 RW 0x10000
DYNAMIC 0x00fdd0 0x000000000001fdd0 0x000000000001fdd0 0x0001f0
0x0001f0 RW 0x8
NOTE 0x000218 0x0000000000000218 0x0000000000000218 0x000098
0x000098 R 0x4
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000
0x000000 RW 0x10
GNU_RELRO 0x00fda0 0x000000000001fda0 0x000000000001fda0 0x000260
0x000260 RW 0x8

There seems to be a gap between the end of the R+E LOAD header ending at
0x530 and the start of the R+W load header at 0xfda0. I see that the
GNU_RELRO header ends at exactly 0x10000, so maybe that has something to do
with it? (0x00fda0 + 0x000260 == 0x10000)

I think the increase is mostly padding -- compressed image sizes aren't
that different:

$ /ssd2/stand-arm64-21-libc++-r16b/bin/clang main.c -Os -pie
-ffunction-sections -fdata-sections -Wl,--gc-sections -fuse-ld=bfd &&
/ssd2/stand-arm64-21-libc++-r16b/bin/aarch64-linux-android-strip
--strip-unneeded a.out && gzip -ck a.out | wc -c
1212
$ /ssd2/stand-arm64-21-libc++-r16b/bin/clang main.c -Os -pie
-ffunction-sections -fdata-sections -Wl,--gc-sections -fuse-ld=gold &&
/ssd2/stand-arm64-21-libc++-r16b/bin/aarch64-linux-android-strip
--strip-unneeded a.out && gzip -ck a.out | wc -c
1379

Aside: I tested the LLD linker on the same empty program and saw a 200KB
binary, also mostly padding. Each load segment's offset was on an aligned
64KB boundary:

Program Headers:
Type Offset VirtAddr PhysAddr FileSiz
MemSiz Flg Align
PHDR 0x000040 0x0000000000000040 0x0000000000000040 0x0001f8
0x0001f8 R 0x8
INTERP 0x000238 0x0000000000000238 0x0000000000000238 0x000015
0x000015 R 0x1
[Requesting program interpreter: /system/bin/linker64]
LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x0004e0
0x0004e0 R 0x10000
LOAD 0x010000 0x0000000000010000 0x0000000000010000 0x0000c0
0x0000c0 R E 0x10000
LOAD 0x020000 0x0000000000020000 0x0000000000020000 0x010200
0x010200 RW 0x10000
DYNAMIC 0x030030 0x0000000000030030 0x0000000000030030 0x0001b0
0x0001b0 RW 0x8
GNU_RELRO 0x030000 0x0000000000030000 0x0000000000030000 0x000200
0x001000 R 0x1
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000
0x000000 RW 0
NOTE 0x000448 0x0000000000000448 0x0000000000000448 0x000098
0x000098 R 0x4

-Ryan


On Tue, Apr 24, 2018 at 2:21 PM 'Dan Albert' via android-ndk <
Post by 'Dan Albert' via android-ndk
Impressive.
If you're interested in keeping binary size down, you really ought to be
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections
And don't forget to run `strip --strip-unneeded` on the binary too. The
strip binaries are also in the toolchain directory.
Post by John Dallman
I'm trying to build some simple command-line tools to run in the adb
ndk-bundle/build/tools/make_standalone_toolchain.py \
--arch arm64-v8a --api 21 --std libc++ \
--install-dir /local/kernel_lx86/toolchain
I have these directories on the front of my PATH, so that I can use the
/local/kernel_lx86/toolchain/bin:/local/kernel_lx86/android-sdk/platform-tools
I encountered the problem where the BFD linker sets a flag that causes
WARNING: linker: /data/local/tmp/world.bfd: unsupported flags
DT_FLAGS_1=0x8000000
Searching for that found me the github discussion at
https://github.com/android-ndk/ndk/issues/602, and the simple answer
seems to be to use the -fuse-ld=gold option. So I did that, and found my
#include <stdio.h>
int main( int argc, char *argv[])
{
printf( "Hello, world\n");
return 0;
}
clang -fPIC -O -pie -fuse-ld=gold world.c -o world.gold
clang -fPIC -O -pie world.c -o world.bfd
ls -l world.c world.bfd world.gold
-rwxrwxr-x 1 kerman ps 6000 Apr 24 14:47 world.bfd
-rw-rw-r-- 1 kerman ps 111 Apr 20 17:47 world.c
-rwxrwxr-x 1 kerman ps 68824 Apr 24 14:47 world.gold
Am I doing something wrong, or is this a gold bug?
Thanks,
John Dallman
--
You received this message because you are subscribed to the Google Groups
"android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-ndk/CAH1xqgnu%2BTWj9E0Ri2Q0f36TfgF5eKQy9LqOUmqznGCfCf_isQ%40mail.gmail.com
<https://groups.google.com/d/msgid/android-ndk/CAH1xqgnu%2BTWj9E0Ri2Q0f36TfgF5eKQy9LqOUmqznGCfCf_isQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-ndk/CAFVaGhu%2BTPDNmVU4g-czZ7sT6z7O%2BmPE15riihPVCagEnT6bNg%40mail.gmail.com
<https://groups.google.com/d/msgid/android-ndk/CAFVaGhu%2BTPDNmVU4g-czZ7sT6z7O%2BmPE15riihPVCagEnT6bNg%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CALgsJznr%2BZcj623pBr1O6KSmiiZA0npzdFGcKkJ87_urwXq59g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
John Dallman
2018-04-25 12:09:04 UTC
Permalink
if you're interested in keeping binary size down, you really ought to be
using the following flags:

Thanks for those.
​
I think the increase is mostly padding -- compressed image sizes aren't
that different:

OK, I'll push on and see if the growth is a constant size of padding. if it
is, I can live with it.

Thanks,

John
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgmP%3DcxqiTZU0fUq5f0CA6_pVnUkO6NORj%3DLxnZmi56Xhw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...