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.










share|improve this question






















  • "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















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.










share|improve this question






















  • "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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • "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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo