Binary patching Patches for
proprietary software are typically distributed as
executable files instead of
source code. When executed these files load a program into memory which manages the installation of the patch code into the target program(s) on disk. Patches for other software are typically distributed as data files containing the patch code. These are read by a patch
utility program which performs the installation. This utility modifies the target program's executable file—the program's
machine code—typically by overwriting its bytes with bytes representing the new patch code. If the new code will fit in the space (number of bytes) occupied by the old code, it may be put in place by overwriting directly over the old code. This is called an inline patch. If the new code is bigger than the old code, the patch utility will append load record(s) containing the new code to the object file of the target program being patched. When the patched program is run, execution is directed to the new code with branch instructions (jumps or calls) patched over the place in the old code where the new code is needed. On early 8-bit microcomputers, for example the Radio Shack
TRS-80, the operating system includes a PATCH/CMD utility which accepts patch data from a text file and applies the fixes to the target program's executable binary file(s). The patch code must have place(s) in memory to be executed at runtime. Inline patches are no difficulty, but when additional memory space is needed the programmer must improvise. Naturally if the patch programmer is the one who first created the code to be patched, this is easier. Savvy programmers plan in advance for this need by reserving memory for later expansion, left unused when producing their final iteration. Other programmers not involved with the original implementation, seeking to incorporate changes at a later time, must find or make space for any additional bytes needed. The most fortunate possible circumstance for this is when the routine to be patched is a distinct module. In this case the patch programmer need merely adjust the pointers or length indicators that signal to other system components the space occupied by the module; he is then free to populate this memory space with his expanded patch code. If the routine to be patched does not exist as a distinct memory module, the programmer must find ways to shrink the routine to make enough room for the expanded patch code. Typical tactics include shortening code by finding more efficient sequences of instructions (or by redesigning with more efficient algorithms), compacting message strings and other data areas, externalizing program functions to mass storage (such as disk overlays), or removal of program features deemed less important than the changes to be installed with the patch. Small in-memory machine code patches can be manually applied with the system debug utility, such as
CP/M's DDT or
MS-DOS's DEBUG debuggers. Programmers working in interpreted
BASIC often used the POKE command to alter the functionality of a system service routine or the interpreter itself.
Source code patching A patch for source code defines how to modify the text of code files. Such a patch is usually a text file that encodes the differences between two versions of a file. As this type of patch is often created via the
diff command, the patch file is also a diff file. Source code patching is common for an
open-source software project. Maintainers receive patches or people publish patches that fix problems and add functionality, like support for local languages outside the project's locale. In an example from the early development of the
Linux kernel (noted for publishing its complete source code),
Linus Torvalds, the original author, received hundreds of thousands of patches from many
programmers to apply against his original version. The
Apache HTTP Server originally evolved as a number of patches that
Brian Behlendorf collated to improve
NCSA HTTPd, hence a name that implies that it is a collection of patches (
"a patchy server"). The FAQ on the project's official site states that the name 'Apache' was chosen from respect for the Native American Indian tribe of
Apache. However, the 'a patchy server' explanation was initially given on the project's website. == See also ==