Incomputer programming, aself-relocating program is a program thatrelocates its own address-dependent instructions and data when run, and is therefore capable of being loaded into memory at any address.[1][2] In many cases, self-relocating code is also a form ofself-modifying code.
Self-relocation is similar to therelocation process employed by thelinker-loader when a program is copied from external storage into main memory; the difference is that it is the loaded program itself rather than the loader in theoperating system orshell that performs the relocation.
One form of self-relocation occurs when a program copies the code of its instructions from one sequence of locations to another sequence of locations within the main memory of a single computer, and then transfers processor control from the instructions found at the source locations of memory to the instructions found at the destination locations of memory. As such, the data operated upon by the algorithm of the program is the sequence of bytes which define the program.
Static self-relocation typically happens atload-time (after the operating system has loaded the software and passed control to it, but still before its initialization has finished), sometimes also when changing the program's configuration at a later stage duringruntime.[3][4]
As an example, self-relocation is often employed in the early stages of bootstrapping operating systems on architectures likeIBM PC compatibles, where lower-level chainboot loaders (like themaster boot record (MBR),volume boot record (VBR) and initial boot stages of operating systems such asDOS) move themselves out of place in order to load the next stage into memory.
UnderCP/M, the debuggerDynamic Debugging Tool (DDT)dynamically relocateditself to the top of available memory throughpage boundary relocation in order to maximize theTransient Program Area (TPA) for programs to run in.[5][6]
In 1988, the alternative command line processorZCPR 3.4 for theZ-System introduced so calledtype-4 programs which were self-relocatable through an embedded stub as well.[7][8][9][10][11]
UnderDOS, self-relocation is sometimes also used by more advanceddrivers andresident system extensions (RSXs) orterminate-and-stay-resident programs (TSRs) loading themselves "high" intoupper memory more effectively than possible for externally provided "high"-loaders (likeLOADHIGH/HILOAD,INSTALLHIGH/HIINSTALL orDEVICEHIGH/HIDEVICE etc.[12] since DOS 5) in order to maximize the memory available for applications. This is down to the fact that the operating system has no knowledge of the inner workings of a driver to be loaded and thus has to load it into a free memory area large enough to hold the whole driver as a block including its initialization code, even if that would be freed after the initialization. For TSRs, the operating system also has to allocate aProgram Segment Prefix (PSP) and anenvironment segment.[13] This might cause the driver not to be loaded into the most suitable free memory area or even prevent it from being loaded high at all. In contrast to this, a self-relocating driver can be loaded anywhere (including intoconventional memory) and then relocate only its (typically much smaller) resident portion into a suitable free memory area in upper memory. In addition, advanced self-relocating TSRs (even if already loaded into upper memory by the operating system) can relocate over most of their own PSP segment and command line buffer and free their environment segment in order to further reduce the resultingmemory footprint and avoidfragmentation.[14] Some self-relocating TSRs can also dynamically change their "nature" and morph into device drivers even if originally loaded as TSRs, thereby typically also freeing some memory.[4] Finally, it is technically impossible for an external loader to relocate drivers intoexpanded memory (EMS), thehigh memory area (HMA) orextended memory (viaDPMS orCLOAKING), because these methods require small driver-specificstubs to remain in conventional or upper memory in order to coordinate the access to the relocation target area,[15][nb 1][nb 2] and in the case of device drivers also because the driver's header must always remain in the first megabyte.[15][13] In order to achieve this, the drivers must be specially designed to support self-relocation into these areas.[15]
Some advanced DOS drivers also contain both a device driver (which would be loaded at offset +0000h by the operating system) and TSR (loaded at offset +0100h) sharing a common code portion internally asfat binary.[13] If the shared code is not designed to beposition-independent, it requires some form of internal address fix-up similar to what would otherwise have been carried out by arelocating loader already; this is similar to the fix-up stage of self-relocation but with the code already being loaded at the target location by the operating system's loader (instead of done by the driver itself).
IBMDOS/360 did not have the ability to relocate programs during loading. Sometimes multiple versions of a program were maintained, each built for a different load address (partition). A special class of programs, called self-relocating programs, were coded to relocate themselves after loading.[16] IBMOS/360 relocated executable programs when they were loaded into memory. Only one copy of the program was required, but once loaded the program could not be moved (so calledone-time position-independent code).
As an extreme example of (many-time) self-relocation, also called dynamic self-relocation, it is possible to construct a computer program so that it does not stay at a fixed address in memory, even as it executes, as for example used inworm memory tests.[17][18][19][20] TheApple Worm is a dynamic self-relocator as well.[21]
[…] Laws: […] "dynamic relocation" of the OS. Can you tell us what that is and why it was important? […]Eubanks: […] whatGary did […] was […] mind boggling. […] I remember the day at theschool he came bouncing into the lab and he said, I have figured out how torelocate. He took advantage of the fact that the only byte was always going to be thehigh order byte. And so he created abitmap. […] it didn't matter how much memory the computer had, the operating system could always be moved into the high memory. Therefore, you could commercialize this […] on machines of different amounts of memory. […] you couldn't be selling a 64KCP/M and a 47K CP/M. It'd just be ridiculous to have a hard compile in the addresses. So Gary figured this out one night, probably in the middle of the night thinking about some coding thing, and this really made CP/M possible to commercialize. I really think that without that relocation it would have been a very tough problem. To get people to buy it, it'd seem complicated to them, and if you added more memory you'd have to go get a different operating system. […]Intel […] had thebytes reversed, right, for the memory addresses. But they were always in the same place, so you could relocate it on a256 byte boundary, to be precise. You could therefore always relocate it with just a bitmap of where those […] Laws: Certainly the most eloquent explanation I've ever had of dynamic relocation […][5][6] (33 pages)
[…] Add a SYS device driver header to the driver, so that CTMOUSE could beboth in one, a normalTSR and a device driver - similar to our FreeKEYB advanced keyboard driver. […] This is not really needed underDR DOS becauseINSTALL= is supported since DR DOS 3.41+ and DR DOS preserves the order of[D]CONFIG.SYS directives […] but it would […] improve the […] flexibility onMS-DOS/PC DOS systems, which […] always executeDEVICE= directives prior to any INSTALL= statements, regardless of their order in the file. […] software may require the mouse driver to be present as a device driver, as mouse drivers have always been device drivers back in the old times. These mouse drivers have had specific device driver names depending on which protocol they used ("PC$MOUSE" forMouse Systems Mode for example), and some software may search for these drivers in order to find out the correct type of mouse to be used. […] Another advantage would be that device drivers usually consume less memory (noenvironment, noPSP) […] It's basically a tricky file header, a different code to parse the command line, a different entry point and exit line, and some segment magics to overcome the ORG 0 / ORG 100h difference. Self-loadhighing a device driver is a bit more tricky as you have to leave the driver header where it is and only relocate the remainder of the driver […]
[…] At least theMS-DOS 6.0+GRAFTABL relocates itself over parts of itsPSP segment (offset +60h and upwards) to minimize its resident size. […](NB. A post-DR-DOS 7.03 GRAFTABL 2.00+ supports dynamic self-relocation as well.)
[…] DOSRELO provides a method of makingDOS problem programs self-relocating. DOSRELO accomplishes the self-relocation capability for all programs, regardless of the language, by adding entry point logic to theobject code of the program before theLinkage Editor catalogs it on theCore Image Library. […]
[…] Besides fetching an instruction, theZ80 uses half of the cycle torefresh thedynamic RAM. […] since the Z80 must spend half of eachinstruction fetch cycle performing other chores, it doesn't have as much time to fetch aninstruction byte as it does a data byte. If one of theRAM chips at the memory location being accessed is a little slow, the Z80 may get the wrong bit pattern when it fetches an instruction, but get the right one when it reads data. […] the built-in memory test won't catch this type of problem […] it's strictly a data read/write test. During the test, all instruction fetches are from theROM, not from RAM […] result[ing] in theH89 passing the memory test but still operating erratically on some programs. […] This is a program that tests memory by relocating itself through RAM. As it does so, the CPU prints the current address of the program on theCRT and then fetches the instruction at that address. If the RAM ICs are okay at that address, the CPU relocates the test program to the next memory location, prints the new address, and repeats the procedure. But, if one of the RAM ICs is slow enough to return an incorrect bit pattern, the CPU will misinterpret the instruction and behave unpredictably. However, it's likely that the display will lock up showing the address of faulty IC. This narrows the problem down eight ICs, which is an improvement over having to check as much as 32. […] The […] program will perform aworm test by pushing an RST 7 (RESTART 7) instruction from the low end of memory on up to the last working address. The rest of the program remains stationary and handles the display of the current location of the RST 7 command and itsrelocation. Incidentally, the program is called a worm test because, as the RST 7 instruction moves up through memory, it leaves behind aslime trail ofNOPs (NO OPERATION). […]