The below mentioned in the GCC Page for the function sections and data sections options:
-ffunction-sections -fdata-sections
When using those compiler options, you can add the linker option -Wl,--gc-sections that will remove all unused code.
-Wl,--gc-sections