The shoutbox is currently out of service. Join us on Discord instead.
You can help CodeWalrus stay online by donating here.

Know what C functions aren't called from a certain function?

Started by Hayleia, June 22, 2016, 04:17:20 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Hayleia

No idea if that's the right place to post, especially since I'm mostly asking for 3DS dev but since it might apply to C in general, I posted here...

So, is there a way to detect what functions are never called from a certain starting function, recursively?
Example, if you didn't get what I said.

int a() {
    return 1;
}
int b() {
    return 2;
}
int c() {
    return a()+1;
}
int main() {
  return c()+2;
}

Well, then.
- what functions aren't called from a? b, c, main.
- what functions aren't called from b? a, c, main.
- what functions aren't called from c? b, main (a is called directly from c).
- what functions aren't called from main? b (c is called directly from main and a is called indirectly from c).

So is there something that can parse my files and tell me this?
Note, this has to be a tool that happens before execution since, as I said, this is going to be executed from the 3DS, so I can't really run the compiled executable through some IDE tool that sends stuff to GDB or whatever, that won't work.

Ivoah

Quote from: Hayleia on June 22, 2016, 04:17:20 pm
No idea if that's the right place to post, especially since I'm mostly asking for 3DS dev but since it might apply to C in general, I posted here...

So, is there a way to detect what functions are never called from a certain starting function, recursively?
Example, if you didn't get what I said.

int a() {
    return 1;
}
int b() {
    return 2;
}
int c() {
    return a()+1;
}
int main() {
  return c()+2;
}

Well, then.
- what functions aren't called from a? b, c, main.
- what functions aren't called from b? a, c, main.
- what functions aren't called from c? b, main (a is called directly from c).
- what functions aren't called from main? b (c is called directly from main and a is called indirectly from c).

So is there something that can parse my files and tell me this?
Note, this has to be a tool that happens before execution since, as I said, this is going to be executed from the 3DS, so I can't really run the compiled executable through some IDE tool that sends stuff to GDB or whatever, that won't work.

Sounds like you want a static analyzer, here's one for C: http://clang-analyzer.llvm.org/

Hayleia

Thanks :)
In the meantime, I also discovered cflow. Not sure which one suits my needs best, I'll see.

edit Seems like cflow -r *.c | grep -x -E "[^ ].*|[ ]*main.*" gives an output that fulfills my need correctly (if I didn't fail, it should list all the functions in the .c files, either with nothing under them or with mentions of "main" which mean they are called by main).

Juju

Depending of the compiler, I think they do a good job at removing unused and unreachable code when you ask for it through compiler options (like -O in GCC). But better audit the code yourself and comment out/remove the unused code, I guess.
Read Zarmina!
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader :walrii: --Snektron



if you wanna throw money at me and/or CodeWalrus monthly it's here

Streetwalrus

Quote from: Juju on June 23, 2016, 05:52:38 am
Depending of the compiler, I think they do a good job at removing unused and unreachable code when you ask for it through compiler options (like -O in GCC). But better audit the code yourself and comment out/remove the unused code, I guess.

Actually unless you enable LTO, you need to add specific linker flags for that to happen. Otherwise it can't  know what to remove since code in other objects may call it.

Hayleia

This probably works when compiling a regular PC program but adding -flto to my cflags didn't remove anything to my compiled armloaderhax payload for 3DS :P
Removing unused functions by hand did though. Went down from 90KB to 12 :)

Powered by EzPortal