Patching ELF with minimum number of change in bytes
up vote
1
down vote
favorite
Problem Definition : I need to patch (let's say) xxx.elf for my remote target. My aim is to keep most of elf file unchanged except my patch will be placed at the end of elf file. It means I have minimum number of bytes for sending to my remote target. It means less communication stuff, and etc.
Note: Shared library is not used.
1) When I have changed source code directly and rebuilt it (xxx_patch.elf). Taking the difference of two files (xx.elf vs. xxx_patch.elf), it seems that there is a huge shift at the beginning of file. And it does not work for me.
2) As a result of my search, I have read a few elf patching techniques: replacing a section, adding a section, etc.
Then, I created a patch.c and patch.h file. I have copied a function that I want to make changes to patch files. I also changed linker file to create a new section (.patchText) and put patch.o to this section. I built the source code again and compare both original and patched files. It seems OK for me cause I can see my changes at the end of file. First address shift in bytes occurs as I expected. But the problem here is I need to modify call instruction manually to call new function. It requires calculations about PC relative addressing and etc. It is complicated.
3) __attribute_((weak)) option : it works if I put my original output files and patch output in the same section. Else, it does not work for me. Because I can not use same section for patch to minimize byte size.
4) -Wl,-wrap,symboname option : It has a similar working scheme with 3)
So, is there any idea about that? Which will be better? Or is there any other way to achieve this?
Thank you.
c patch elf
add a comment |
up vote
1
down vote
favorite
Problem Definition : I need to patch (let's say) xxx.elf for my remote target. My aim is to keep most of elf file unchanged except my patch will be placed at the end of elf file. It means I have minimum number of bytes for sending to my remote target. It means less communication stuff, and etc.
Note: Shared library is not used.
1) When I have changed source code directly and rebuilt it (xxx_patch.elf). Taking the difference of two files (xx.elf vs. xxx_patch.elf), it seems that there is a huge shift at the beginning of file. And it does not work for me.
2) As a result of my search, I have read a few elf patching techniques: replacing a section, adding a section, etc.
Then, I created a patch.c and patch.h file. I have copied a function that I want to make changes to patch files. I also changed linker file to create a new section (.patchText) and put patch.o to this section. I built the source code again and compare both original and patched files. It seems OK for me cause I can see my changes at the end of file. First address shift in bytes occurs as I expected. But the problem here is I need to modify call instruction manually to call new function. It requires calculations about PC relative addressing and etc. It is complicated.
3) __attribute_((weak)) option : it works if I put my original output files and patch output in the same section. Else, it does not work for me. Because I can not use same section for patch to minimize byte size.
4) -Wl,-wrap,symboname option : It has a similar working scheme with 3)
So, is there any idea about that? Which will be better? Or is there any other way to achieve this?
Thank you.
c patch elf
"is there any other way to achieve this" -- probably. But you didn't tell us what you are actually trying to achieve. See xyproblem.info
– Employed Russian
Nov 22 at 17:15
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Problem Definition : I need to patch (let's say) xxx.elf for my remote target. My aim is to keep most of elf file unchanged except my patch will be placed at the end of elf file. It means I have minimum number of bytes for sending to my remote target. It means less communication stuff, and etc.
Note: Shared library is not used.
1) When I have changed source code directly and rebuilt it (xxx_patch.elf). Taking the difference of two files (xx.elf vs. xxx_patch.elf), it seems that there is a huge shift at the beginning of file. And it does not work for me.
2) As a result of my search, I have read a few elf patching techniques: replacing a section, adding a section, etc.
Then, I created a patch.c and patch.h file. I have copied a function that I want to make changes to patch files. I also changed linker file to create a new section (.patchText) and put patch.o to this section. I built the source code again and compare both original and patched files. It seems OK for me cause I can see my changes at the end of file. First address shift in bytes occurs as I expected. But the problem here is I need to modify call instruction manually to call new function. It requires calculations about PC relative addressing and etc. It is complicated.
3) __attribute_((weak)) option : it works if I put my original output files and patch output in the same section. Else, it does not work for me. Because I can not use same section for patch to minimize byte size.
4) -Wl,-wrap,symboname option : It has a similar working scheme with 3)
So, is there any idea about that? Which will be better? Or is there any other way to achieve this?
Thank you.
c patch elf
Problem Definition : I need to patch (let's say) xxx.elf for my remote target. My aim is to keep most of elf file unchanged except my patch will be placed at the end of elf file. It means I have minimum number of bytes for sending to my remote target. It means less communication stuff, and etc.
Note: Shared library is not used.
1) When I have changed source code directly and rebuilt it (xxx_patch.elf). Taking the difference of two files (xx.elf vs. xxx_patch.elf), it seems that there is a huge shift at the beginning of file. And it does not work for me.
2) As a result of my search, I have read a few elf patching techniques: replacing a section, adding a section, etc.
Then, I created a patch.c and patch.h file. I have copied a function that I want to make changes to patch files. I also changed linker file to create a new section (.patchText) and put patch.o to this section. I built the source code again and compare both original and patched files. It seems OK for me cause I can see my changes at the end of file. First address shift in bytes occurs as I expected. But the problem here is I need to modify call instruction manually to call new function. It requires calculations about PC relative addressing and etc. It is complicated.
3) __attribute_((weak)) option : it works if I put my original output files and patch output in the same section. Else, it does not work for me. Because I can not use same section for patch to minimize byte size.
4) -Wl,-wrap,symboname option : It has a similar working scheme with 3)
So, is there any idea about that? Which will be better? Or is there any other way to achieve this?
Thank you.
c patch elf
c patch elf
asked Nov 22 at 12:36
vilidigonzales
62
62
"is there any other way to achieve this" -- probably. But you didn't tell us what you are actually trying to achieve. See xyproblem.info
– Employed Russian
Nov 22 at 17:15
add a comment |
"is there any other way to achieve this" -- probably. But you didn't tell us what you are actually trying to achieve. See xyproblem.info
– Employed Russian
Nov 22 at 17:15
"is there any other way to achieve this" -- probably. But you didn't tell us what you are actually trying to achieve. See xyproblem.info
– Employed Russian
Nov 22 at 17:15
"is there any other way to achieve this" -- probably. But you didn't tell us what you are actually trying to achieve. See xyproblem.info
– Employed Russian
Nov 22 at 17:15
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431198%2fpatching-elf-with-minimum-number-of-change-in-bytes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"is there any other way to achieve this" -- probably. But you didn't tell us what you are actually trying to achieve. See xyproblem.info
– Employed Russian
Nov 22 at 17:15