Created
September 21, 2016 12:55
-
-
Save raek/ed6a615fe9eefa0345037c3465a5e38c to your computer and use it in GitHub Desktop.
unused symbols and GCC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int used(void) | |
{ | |
printf("all used\n"); | |
return 0; | |
} | |
int unused(void) | |
{ | |
printf("all unused\n"); | |
return 9000; | |
} | |
int unrelated(void) | |
{ | |
return 42; | |
} | |
int main(int argc, char **argv) | |
{ | |
return used(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Compile | |
gcc -o all.o -c all.c | |
gcc -o main.o -c main.c | |
gcc -o helper.o -c helper.c | |
gcc -o unrelated.o -c unrelated.c | |
# Archive | |
rm -f helper.a # ar has no option to completely overwrite existing file | |
ar rcf helper.a helper.o | |
rm -f unrelated.a | |
ar rcf unrelated.a unrelated.o | |
# Link | |
gcc -o test1 all.o | |
gcc -o test2 main.o helper.o unrelated.o | |
gcc -o test3 main.o helper.a unrelated.a | |
# Tests | |
echo Same object file | |
nm test1 | grep "\b\(unused\|used\|unrelated\)\b" | |
echo Separate object file | |
nm test2 | grep "\b\(unused\|used\|unrelated\)\b" | |
echo Separate archived object file | |
nm test3 | grep "\b\(unused\|used\|unrelated\)\b" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int used(void) | |
{ | |
printf("used\n"); | |
return 0; | |
} | |
int unused(void) | |
{ | |
printf("unused\n"); | |
return 9000; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int used(void); | |
int main(int argc, char **argv) | |
{ | |
return used(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Same object file | |
0000000000400550 T unrelated | |
000000000040053b T unused | |
0000000000400526 T used | |
Separate object file | |
0000000000400566 T unrelated | |
0000000000400551 T unused | |
000000000040053c T used | |
Separate archived object file | |
0000000000400551 T unused | |
000000000040053c T used |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment