I wonder whether it would be possible to use ELFIO to write a program that would strip unused functions from an ELF binary.
I have noticed `ld' is not terribly smart with unused (not called by any other function or referrenced in any other way) functions. I'm pretty ignorant regarding the ELF format, ELFIO and the linking process so I don't know whether this would be simple/possible to implement. Any comments?
On the another hand, if there already is a tool available that does this I would like to know about it.
Regards,
Carlos
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I believe that so long as the executable still contains symbol and relocation information this should be possible.
You would have to ensure that you do not violate the alignment constraints and possibly rewrite the GOT, so it might be easiest if you turn the executable back into an object file (minus the functions you want to remove) and let the linker take care of everything else.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm primarily concerned with the case where I do have the object files in the first place. What would then be the necessary magic incantations to let ld `take care of everything else' (strip the unused functions)?
Regards,
Carlos
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry, I meant strip the functions yourself from the object files, and then link the app using ld.
I don't really know anything about ld, particually not optimisations like that. However I would be very suprised if it had that functionality built in because it mainly deals with sections, not procedures.
It would be a matter of scanning all of the object files to determine which procedures are used, and then modifying the section contents to remove the body of procedures, and processing the relocations and symbol tables to remove associated entries.
I'd be interested in having the util once it is done, so if you'd like to talk it over, my icq is 462051 :)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think that this might not be as simple as it sounds. I know that with the old .obj file used in MSDOS the compiler produced all code (functions) as a block, potentially using relative call instructions between functions which resided within the same object file (and therefore requiring no fixup at link time).
I would think - although I am not 100% sure - that ELF is much the same.
What this means is you can't really just remove a function, because this changes the relative address between two other functions, and there may potentially be a relative call or jump instruction between those two functions which doesn't even have a fixup entry in the object file.
At least this probably wouldn't be too hard to verify - perhaps just write a simple program with two functions - main() and x(), where "x" is called by "main" - and see if there is a fixup entry for the call residing within the body of the main function.
The "traditional" way to make sure that unused functions are not linked in is to write each function in a seperate source file, then link the resulting objects from each source into a static library, and llink the main program against the library. This pulls in only those object files which contain functions which are called somewhere else in the program.
best regards,
Davin.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Wasn't aware of the traditional method I must admit, but I would prefer something that operates on the object files, or the linked binary, as it would be much less hassle to maintain.
I do not know whether gcc generates relative control flow instructions or not, but in all the object files that I have messed around with there have been relocation entries for the functions. It may be that this is not the case all the time, once again I do not know ;)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I wouldn't mind taking the time to write the util as long as it would be straighforward, as I don't really have the time to build up a lot of expertise from scratch. It doesn't surprise me it might not be that simple since I have tried quite hard to find a tool that would do it and couldn't.
I'm aware of the `traditional' way. It's a royal PITA since you have to maintain lots and lots of files (it even discourages factorization).
I guess it should be in theory possible to write code that would break the .cc files into individual pieces but it would be quite hard to make it work correctly regarding the preprocessor, static functions and variables, etc.
Any other ideas?
Best regards,
Carlos
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I wonder whether it would be possible to use ELFIO to write a program that would strip unused functions from an ELF binary.
I have noticed `ld' is not terribly smart with unused (not called by any other function or referrenced in any other way) functions. I'm pretty ignorant regarding the ELF format, ELFIO and the linking process so I don't know whether this would be simple/possible to implement. Any comments?
On the another hand, if there already is a tool available that does this I would like to know about it.
Regards,
Carlos
I believe that so long as the executable still contains symbol and relocation information this should be possible.
You would have to ensure that you do not violate the alignment constraints and possibly rewrite the GOT, so it might be easiest if you turn the executable back into an object file (minus the functions you want to remove) and let the linker take care of everything else.
Hi George,
Thanks for the reply.
I'm primarily concerned with the case where I do have the object files in the first place. What would then be the necessary magic incantations to let ld `take care of everything else' (strip the unused functions)?
Regards,
Carlos
Sorry, I meant strip the functions yourself from the object files, and then link the app using ld.
I don't really know anything about ld, particually not optimisations like that. However I would be very suprised if it had that functionality built in because it mainly deals with sections, not procedures.
It would be a matter of scanning all of the object files to determine which procedures are used, and then modifying the section contents to remove the body of procedures, and processing the relocations and symbol tables to remove associated entries.
I'd be interested in having the util once it is done, so if you'd like to talk it over, my icq is 462051 :)
I think that this might not be as simple as it sounds. I know that with the old .obj file used in MSDOS the compiler produced all code (functions) as a block, potentially using relative call instructions between functions which resided within the same object file (and therefore requiring no fixup at link time).
I would think - although I am not 100% sure - that ELF is much the same.
What this means is you can't really just remove a function, because this changes the relative address between two other functions, and there may potentially be a relative call or jump instruction between those two functions which doesn't even have a fixup entry in the object file.
At least this probably wouldn't be too hard to verify - perhaps just write a simple program with two functions - main() and x(), where "x" is called by "main" - and see if there is a fixup entry for the call residing within the body of the main function.
The "traditional" way to make sure that unused functions are not linked in is to write each function in a seperate source file, then link the resulting objects from each source into a static library, and llink the main program against the library. This pulls in only those object files which contain functions which are called somewhere else in the program.
best regards,
Davin.
Wasn't aware of the traditional method I must admit, but I would prefer something that operates on the object files, or the linked binary, as it would be much less hassle to maintain.
I do not know whether gcc generates relative control flow instructions or not, but in all the object files that I have messed around with there have been relocation entries for the functions. It may be that this is not the case all the time, once again I do not know ;)
Davin, George,
Thanks for your comments
I wouldn't mind taking the time to write the util as long as it would be straighforward, as I don't really have the time to build up a lot of expertise from scratch. It doesn't surprise me it might not be that simple since I have tried quite hard to find a tool that would do it and couldn't.
I'm aware of the `traditional' way. It's a royal PITA since you have to maintain lots and lots of files (it even discourages factorization).
I guess it should be in theory possible to write code that would break the .cc files into individual pieces but it would be quite hard to make it work correctly regarding the preprocessor, static functions and variables, etc.
Any other ideas?
Best regards,
Carlos