HOME

TheInfoList



OR:

In computer programming, a self-relocating program is a program that relocates its own address-dependent instructions and data when run, and is therefore capable of being loaded into memory at any address. In many cases, self-relocating code is also a form of self-modifying code.


Overview

Self-relocation is similar to the relocation process employed by the linker- 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 the operating system or shell 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 at load-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 during runtime.


Examples


Boot loaders

As an example, self-relocation is often employed in the early stages of bootstrapping operating systems on architectures like IBM PC compatibles, where lower-level chain boot loaders (like the
master boot record A master boot record (MBR) is a special type of boot sector at the very beginning of partitioned computer mass storage devices like fixed disks or removable drives intended for use with IBM PC-compatible systems and beyond. The concept of MBR ...
(MBR), volume boot record (VBR) and initial boot stages of operating systems such as DOS) move themselves out of place in order to load the next stage into memory.


CP/M extensions

Under
CP/M CP/M, originally standing for Control Program/Monitor and later Control Program for Microcomputers, is a mass-market operating system created in 1974 for Intel 8080/ 85-based microcomputers by Gary Kildall of Digital Research, Inc. Initial ...
, the debugger Dynamic Debugging Tool (DDT) dynamically relocated ''itself'' to the top of available memory through
page boundary relocation Relocation is the process of assigning load addresses for position-dependent code and data of a program and adjusting the code and data to reflect the assigned addresses. Prior to the advent of multiprocess systems, and still in many embedded ...
in order to maximize the Transient Program Area (TPA) for programs to run in. In 1988, the alternative command line processor ZCPR 3.4 for the
Z-System CP/M, originally standing for Control Program/Monitor and later Control Program for Microcomputers, is a mass-market operating system created in 1974 for Intel 8080/Intel 8085, 85-based microcomputers by Gary Kildall of Digital Research, Inc ...
introduced so called ''type-4'' programs which were self-relocatable through an embedded stub as well.


x86 DOS drivers

Under DOS, self-relocation is sometimes also used by more advanced drivers and RSXs/ TSRs loading themselves "high" into upper memory more effectively than possible for externally provided "high"-loaders (like LOADHIGH/ HILOAD, INSTALLHIGH/ HIINSTALL or DEVICEHIGH/ HIDEVICE etc. 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 a Program Segment Prefix (PSP) and an
environment segment An environment variable is a Name resolution (programming languages), dynamic-named Value (computer science), value that can affect the way running process (computing), processes will behave on a computer. They are part of the environment in wh ...
. 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 into conventional 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 resulting memory footprint and avoid
fragmentation Fragmentation or fragmented may refer to: Computers * Fragmentation (computing), a phenomenon of computer storage * File system fragmentation, the tendency of a file system to lay out the contents of files non-continuously * Fragmented distributi ...
. 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. Finally, it is technically impossible for an external loader to relocate drivers into expanded memory (EMS), the high memory area (HMA) or extended memory (via DPMS or CLOAKING), because these methods require small driver-specific
stub Stub or Stubb may refer to: Shortened objects and entities * Stub (stock), the portion of a corporation left over after most but not all of it has been bought out or spun out * Stub, a tree cut and allowed to regrow from the trunk; see Pollardi ...
s to remain in conventional or upper memory in order to coordinate the access to the relocation target area, and in the case of device drivers also because the driver's header must always remain in the first megabyte. In order to achieve this, the drivers must be specially designed to support self-relocation into these areas. 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 as fat binary. If the shared code is not designed to be
position-independent In computing, position-independent code (PIC) or position-independent executable (PIE) is a body of machine code that, being placed somewhere in the primary memory, executes properly regardless of its absolute address. PIC is commonly used f ...
, it requires some form of internal address fix-up similar to what would otherwise have been carried out by a relocating 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).


IBM DOS/360 and OS/360 programs

IBM
DOS/360 Disk Operating System/360, also DOS/360, or simply DOS, is the discontinued first member of a sequence of operating systems for IBM System/360, System/370 and later mainframes. It was announced by IBM on the last day of 1964, and it was first d ...
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. IBM OS/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 called one-time position-independent code).


Other examples

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 in
worm memory test Memory testers are specialized test equipment used to test and verify memory modules. Types Memory module testers can be broadly categorized into two types, hardware memory testers and software diagnostic programs that run in a PC environme ...
s. The Apple Worm is a dynamic self-relocator as well.


See also

* Dynamic dead code elimination * RPLOADER - a DR-DOS API to assist remote/network boot code in relocating itself while DOS boots * Garbage collection * Self-replication *
Self-reference Self-reference occurs in natural or formal languages when a sentence, idea or formula refers to itself. The reference may be expressed either directly—through some intermediate sentence or formula—or by means of some encoding. In philoso ...
* Quine (computing)


Notes


References


Further reading

*

https://archive.org/download/80-microcomputing-magazine-1983-10/80Microcomputing_1083_text.pdf] * * * {{cite web , title=PRL2COM , language=de , date=2017-04-24 , orig-date=2012-02-20, 2009, 2002, 1988-07-26, 1987-10-11 , author-first=Volker , author-last=Pohlers , work=Homecomputer DDR , url=http://hc-ddr.hucki.net/wiki/lib/exe/fetch.php/cpm/prl2com.zip , access-date=2020-02-21 , url-status=live , archive-url=https://web.archive.org/web/20200221030941/http://hc-ddr.hucki.net/wiki/lib/exe/fetch.php/cpm/prl2com.zip , archive-date=2020-02-21 Computer programming