Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to understand what it means by patching binary files in an example from the manpage of xxd:

Patch the date in the file xxd.1
% echo "0000037: 3574 68" | xxd -r - xxd.1
% xxd -s 0x36 -l 13 -c 13 xxd.1
0000036: 3235 7468 204d 6179 2031 3939 36  25th May 1996
  1. Does it mean editing binary files, or is it like in Wikipedia

    A patch is a piece of software designed to fix problems1 with, or update a computer program or its supporting data.

  2. How do xxd and patch differ, and are they similar?
  3. What does the example try to do?
share|improve this question

3 Answers 3

up vote 7 down vote accepted

To patch a file means to modify it, with the connotation that the modification is generally small. The usage comes from the general English usage where a patch is a small modification (to a piece of cloth, for example). When it comes to files, a patch is not always a repair.

A patch is a series of instructions that describe how to modify a file or a set of files. In the unix world, a patch is usually the output of the diff command, describing changes in a text file. A patch in this sense describes the modifications in terms of adding, removing or modifying lines in the files. The patch utility applies these instructions to modify a file or set of files. It tries to be smart about applying multiple patches to the same file, as the first patch could cause changes that prevent the second one from being applies because the file is no longer in the expected state. Because diff and patch strongly base their operation on lines, they are not well-suited to binary files.

xxd is a generic utility for working with binary files. One of its capabilities is to make modifications in a file. For example, the command xxd -r - xxd.1 means to apply the modifications described on standard input. The example patch (i.e. the modification instructions) 0000037: 3574 68 mean: starting at offset 0x37 (that's 55 in decimal), replace the next three bytes by 0x35, 0x74 and 0x68 (i.e. the three characters 5th).

share|improve this answer

On microcontrollers, you sometimes have programs, that alter the program memory, meaning the program itself. You could say the program "mutates" when it runs.

This is just one explanation for altering binary applications.

In fact, you use binary patching quite often in microcontroller programs. Since your program is machine code, and more often than not, you're programming in assembler, the assembled machine code is quite a direct representation of what you've been programming. So, once your code is assembled you simply provide patches to some or all of the microcontroller program memory.

share|improve this answer

When you apply a patch to a file, you are overwriting part of it with updated data (a patch can also tack additional data on to the end). You can patch any type of file, whether it's text, binary, or whatever, because all files are streams of bytes when it comes down to it.

The parts of it that are supposed to be overwritten are specified in the patch file.

If a software fix involves updating a file, a patch can modify the file to be the same as a full version of an updated file. Since patches aren't the full file, but just the differences, patches will be smaller, consume less disk space, and consume less bandwidth when downloaded.

Direct editing of a binary file in a hex editor (such as hexer) is sometimes called "manual" patching.

I'm not familiar with xxd, but it looks like from preliminary searching that xxd can generate a dump or hex listing of binary data, and can also convert such a listing back into straight binary. So you can dump the hex with xxd, edit it with vim or another text editor, and write changes back with an xxd -r. You can also pipe it a string using echo in xxd "hexdump format." (Other options let you perform changes to the binary file. Looks like xxd has a lot of options that let you specify where to start and stop overwriting, or patching.)

The first command in the example is basically telling xxd to change the bytes in file xxd.1 starting at offset 0x0000037 to 0x35 0x74 0x68 (ASCII for "5th"). xxd can read it's own "hexdump" format and that is what the "echo" command is providing to xxd.

The second command in the example is dumping a count (-c) of 13 bytes in the file starting at that same offset.

The patch command does a similar thing, but it wants files in a "diff" format. You can make a diff file by saving the output of the diff command, which will compare two files and express the differences in said diff format. From reading the man page it looks like patch is meant more for changing text files than straight binary.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.