0

This might be the root cause of the problem in my previous question: Remove exception/unwind functions from Android NDK shared objects. I have distilled it into a minimal working example.

It happens in both C and C++ regardless of C/C++ version. (I thought C wasn't supposed to have exceptions?)

Image of results

13472-byte shared object with exception-handling functions:

int buf[1];

__attribute__((optimize("-Os"))) void bar() {
    buf[0] = 0;
}

__attribute__((visibility("default"))) void foo() {
    bar();
}

5180-byte shared object without exception handling:

int buf[1];

void bar() {
    buf[0] = 0;
}

__attribute__((visibility("default"))) void foo() {
    bar();
}

Compiler flags:

LOCAL_C_INCLUDES := 

LOCAL_CFLAGS := $(LOCAL_C_INCLUDES:%=-I%) -O3 -flto -Wall -D__ANDROID__ -fvisibility=hidden -s
LOCAL_CPPFLAGS := $(LOCAL_C_INCLUDES:%=-I%) -O3 -flto -Wall -D__ANDROID__ -fvisibility=hidden -s
LOCAL_CFLAGS += -pipe
LOCAL_CPPFLAGS += -pipe
LOCAL_CFLAGS += -fno-exceptions -fno-rtti # IGNORED
LOCAL_CPPFLAGS += -fno-exceptions -fno-rtti # IGNORED

__attribute__((optimize("-Os"))) isn't useless, but how come it generates this exception-handling file size bloat which I definitely don't need?

How to fix this?

Edit: To show that it isn't just getting optimized away with -O3, this is also 5180 bytes without exception handling:

int buf[100];

void bar(int i, int j) {
    buf[i] = j;
}

__attribute__((visibility("default"))) void foo(int i, int j) {
    bar(i, j);
}

__attribute__((visibility("default"))) int getFoo(int i) {
    return buf[i];
}

__attribute__((visibility("default"))) int* getBuf() {
    return buf;
}
Community
  • 1
  • 1
fgsfdsfgts
  • 87
  • 2
  • 8
  • gcc has an option to propagate exceptions through C-linkage stack frames, which is now enabled by default, I believe. – Sam Varshavchik Mar 02 '16 at 03:21
  • the `-fno-rtti` parameter is only for C++. by default the `-fno-exceptions` is the normal setting for C programs. Suggest making a decision on which language then use the parameters appropriate for that language. – user3629249 Mar 02 '16 at 16:28
  • @user3629249 Thanks for the tip, but it doesn't resolve the meat of the problem. The reason I tried switching to C in the first place was just to try to avoid getting the exception-handling code in the shared object. gcc (/g++) seems to ignore the `-fno-exceptions` (/`-fno-rtti`) and generates exception-handling code just because I dereference a pointer, but only if I add `__attribute__((optimize("-Os")))` to the function. As far as I can tell I'm not supposed to get this increase in file size. – fgsfdsfgts Mar 02 '16 at 17:21

0 Answers0