Content deleted Content added
Matthiaspaul (talk | contribs) →Further reading: improved refs |
review: research and clarify Windows and ALSR with ref |
||
(46 intermediate revisions by 12 users not shown) | |||
Line 1:
{{Short description|Assigning or adjusting addresses at runtime}}
{{Distinguish|Renumbering}}
{{More footnotes needed|date=July 2024}}
{{Use dmy dates|date=May 2019|cs1-dates=y}}
{{Use list-defined references|date=December 2021}}
{{Use American English|date=January 2019}}
A [[Linker (computing)|linker]] usually performs relocation in conjunction with '''symbol resolution''', the process of searching files and libraries to replace symbolic references or names of [[Library (computer science)|libraries]] with actual usable addresses in [[primary storage|memory]] before running a program.▼
In [[software development]], '''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.<ref name="Intel_iRMX"/><ref name="Levine_1999_CH1_CH3"/>
Relocation is typically done by the linker at [[link time]], but it can also be done at [[load time]] by a relocating [[loader (computing)|loader]], or at [[Run time (program lifecycle phase)|run time]] by the running program [[self-relocation|itself]]. Some architectures avoid relocation entirely by deferring [[address assignment]] to run time; this is known as [[zero address arithmetic]].{{Which|date=March 2017}}▼
▲A [[
▲Relocation is typically done by the linker at [[link time]], but it can also be done at [[load time]] by a relocating [[loader (computing)|loader]], or at [[
== Segmentation ==
[[Object file]]s are typically segmented into various
== Relocation table ==
The relocation table is a list of
In some architectures, a fixup that crosses certain boundaries (such as a segment boundary) or that is not aligned on a word boundary is illegal and flagged as an error by the linker.<ref name="Borland_2007"/>
=== DOS and 16-bit Windows ===
Far [[pointer (computer programming)|pointer]]s ([[32-bit]] pointers with [[x86 memory segmentation|segment]]:offset, used to address 20-bit 640
Instead, segments are relative values in the DOS EXE file. These segments need to be corrected, when the executable has been loaded into memory. The EXE [[loader (computing)|loader]] uses a relocation table to find the segments
===
With 32-bit Windows operating systems, it is not mandatory to provide relocation tables for EXE files, since they are the first image loaded into the virtual address space and thus will be loaded at their preferred base address.
For both
Windows executables can be marked as ASLR-compatible. The ability exits in Windows 8 and newer to enable ASLR even for applications not marked as compatible.<ref>{{cite web |url=https://cloud.google.com/blog/topics/threat-intelligence/six-facts-about-address-space-layout-randomization-on-windows |title=Six Facts about Address Space Layout Randomization on Windows |date=March 17, 2020 |access-date=July 24, 2020}}</ref> To run successfully in this envoronment the relocation sections cannot be omitted by the compiler.<!--[[User:Kvng/RTH]]-->
=== Unix-like systems ===
Line 37 ⟶ 40:
* merging all segments of common type into a single segment of that type
* assigning unique run time addresses to each section and each symbol, giving all code (functions) and data (global variables) unique run time addresses {{clarify|date=July 2021}}
* referring to the '''relocation table''' to modify{{why|date=November 2016}}
==Example==
The following example uses [[Donald Knuth]]'s [[MIX (abstract machine)|MIX]] architecture and MIXAL assembly language. The principles are the same for any architecture, though the details will change.
[[File:relocation example.tif|frameless|right|334px]]
* (A) Program ''SUBR'' is compiled to produce object file (B), shown as both machine code and assembler. The compiler may start the compiled code at an arbitrary ___location, often ___location 1 as shown. Location 13 contains the machine code for the jump instruction to statement ''ST'' in ___location 5.
* (C) If ''SUBR'' is later linked with other code it may be stored at a ___location other than 1. In this example the linker places it at ___location 120. The address in the jump instruction, which is now at ___location 133, must be '''relocated''' to point to the new ___location of the code for statement ''ST'', now 125. [1 61 shown in the instruction is the MIX machine code representation of 125].
* (D) When the program is loaded into memory to run it may be loaded at some ___location other than the one assigned by the linker. This example shows ''SUBR'' now at ___location 300. The address in the jump instruction, now at 313, needs to be relocated again so that it points to the updated ___location of ''ST'', 305. [4 49 is the MIX machine representation of 305].
==Alternatives==
Some architectures avoid relocation entirely by deferring address assignment to run time; as, for example, in stack machines with [[zero address arithmetic]] or in some segmented architectures where every compilation unit is loaded into a separate segment.
== See also ==
Line 53 ⟶ 59:
* [[Static library]]
* [[Self-relocation]]
* [[Rebasing]]
* [[Garbage collection (computing)|Garbage collection]]
* [[Pointer swizzling]], a lazy form of pointer modification
* [[Relocatable Object Module Format]]
== References ==
{{Reflist|refs=
<ref name="Intel_iRMX">{{cite book |title=iRMX 86 Application Loader Reference Manual |publisher=[[Intel]] |url=http://bitsavers.informatik.uni-stuttgart.de/pdf/intel/iRMX/iRMX_86_Rev_6_Mar_1984/146196_Burst/iRMX_86_Application_Loader_Reference_Manual.pdf |access-date=2020-01-11 |url-status=live |archive-url=https://web.archive.org/web/20200111215002/http://bitsavers.informatik.uni-stuttgart.de/pdf/intel/iRMX/iRMX_86_Rev_6_Mar_1984/146196_Burst/iRMX_86_Application_Loader_Reference_Manual.pdf |archive-date=2020-01-11 |chapter=Types of Object Code |pages=((1{{hyphen}}
<ref name="Levine_1999_CH1_CH3">{{cite book |author-last=Levine |author-first=John R. |author-link=John R. Levine |title=Linkers and Loaders |date=2000 |orig-
<ref name="Borland_2007">{{cite web |author=Borland |author-link=Borland |title=Borland article #15961: Coping with 'Fixup Overflow' messages. |orig-
<ref name="ELF">{{cite web |title=Executable and Linkable Format (ELF) |series=Tool Interface Standards (TIS) Portable Formats Specification, Version 1.1 |website=skyfree.org |url=http://www.skyfree.org/linux/references/ELF_Format.pdf |access-date=2018-10-01 |url-status=live |archive-url=https://web.archive.org/web/20191224125108/http://www.skyfree.org/linux/references/ELF_Format.pdf |archive-date=2019-12-24}}</ref>
}}
== Further reading ==
* {{
* {{cite magazine |title=A Proposed Microprocessor Software Standard |author-first1=Peter G. |author-last1=Formaniak |author-first2=David |author-last2=Leitch |series=Technical Forum |magazine=[[BYTE]] - the small systems journal |publisher=[[Byte Publications, Inc.]] |publication-place=Peterborough, New Hampshire, USA |volume=2 |number=7 |date=July 1977 |id=ark:/13960/t32245485 |pages=34, 62–63 |url=https://archive.org/details/byte-magazine-1977-07/page/n35/mode/1up |access-date=2021-12-06}} (3 pages) (NB. Describes a [[Mostek hex format|relocatable hex format]] by [[Mostek]].)
* {{cite journal |author-first=Gary Arlen |author-last=Kildall |author-link=Gary Arlen Kildall |title=A simple technique for static relocation of absolute machine code |journal=[[Dr. Dobb's Journal of Computer Calisthenics & Orthodontia]] |publisher=[[People's Computer Company]] |id=#22 |volume=3 |number=2 |date=February 1978 |pages=10–13<!-- in the issue --> (66–69<!-- in the volume -->) |isbn=0-8104-5490-4<!-- of the volume --> |url=https://groups.google.com/d/msg/comp.os.cpm/TLHgIi16yTo/gupNB1ai8UQJ |access-date=2017-08-19 |url-status=live |archive-url=https://archive.today/20170909091943/https://groups.google.com/forum/%23!msg/comp.os.cpm/TLHgIi16yTo/gupNB1ai8UQJ#!topic/comp.os.cpm/TLHgIi16yTo<!-- https://archive.today/20170909091943/https://groups.google.com/forum/%23!msg/comp.os.cpm/TLHgIi16yTo/gupNB1ai8UQJ --> |archive-date=2017-09-09}} [https://archive.org/stream/dr_dobbs_journal_vol_03/dr_dobbs_journal_vol_03_djvu.txt] [https://web.archive.org/web/20170819141800/http://www.retrotechnology.com/dri/d_dri_refs.html] [https://web.archive.org/web/20170819173516/http://archive.computerhistory.org/resources/access/text/2016/12/102762506-05-01-acc.pdf] (This "resize" method, named ''page boundary relocation'', could be applied statically to a [[CP/M-80]] disk image using {{ill|MOVCPM|pl|MOVCPM (CP/M)}} in order to maximize the [[Transient Program Area|TPA]] for programs to run. It was also utilized dynamically by the CP/M debugger [[Dynamic Debugging Tool]] (DDT) to [[self-relocation|relocate itself]] into higher memory. The same approach was independently developed by [[Bruce Van Natta (IMSAI)|Bruce Van Natta]] of [[IMS Associates]] to produce relocatable [[PL/M]] code. As ''paragraph boundary relocation'', [[Self-relocation#Ref-Paul-2002-Drivers|another variant]] of this method was later utilized by dynamically [[high memory area|HMA]] self-relocating [[Terminate-and-Stay-Resident|TSR]]s like [[KEYB (DOS command)|KEYB]], [[SHARE (DOS command)|SHARE]], and [[NLSFUNC (DOS command)|NLSFUNC]] under [[DR DOS 6.0]] and higher. A much more sophisticated and [[byte alignment|byte-level granular]] method based on a somewhat similar approach was independently conceived and implemented by Matthias R. Paul and Axel C. Frinke for their [[dynamic dead-code elimination]] to dynamically minimize the runtime footprint of resident drivers and TSRs (like FreeKEYB).)▼
* {{cite magazine |title=Relocatable Object Code Formats |author-first1=Carol Anne |author-last1=Ogdin |author-first2=Neil |author-last2=Colvin |author-first3=Tom |author-last3=Pittman |author-first4=Philip |author-last4=Tubb |series=Technical Forum |magazine=[[BYTE]] - the small systems journal |publisher=[[Byte Publications, Inc.]] |publication-place=Peterborough, New Hampshire, USA |volume=2 |number=11 |date=November 1977 |id=ark:/13960/t59c88b4h, ark:/13960/t3kw76j24 |pages=198–205 |url=https://archive.org/details/byte-magazine-1977-11-rescan/page/n199/mode/1up |access-date=2021-12-06}} (8 pages) (NB. Describes a [[Technical Design Labs Relocable Object Module Format|relocatable hex format]] by [[Technical Design Labs|TDL]].)
▲* {{cite journal |author-first=Gary Arlen |author-last=Kildall |author-link=Gary Arlen Kildall |title=A simple technique for static relocation of absolute machine code |journal=[[Dr. Dobb's Journal of Computer Calisthenics & Orthodontia]] |publisher=[[People's Computer Company]] |
* {{cite magazine |title=Relocatable Code |author-first=Richard H. |author-last=Mossip |___location=Bloomingdale, New Jersey, USA |publisher=[[Libes, Inc.]] |publication-place=Mountainside & Springfield, New Jersey, USA |magazine=[[S-100 Microsystems (magazine)|S-100 Microsystems]] |issn=0199-7955 |volume=1 |number=5 |date=September–October 1980 |id=ark:/13960/s2cfgkmxcwg. ark:/13960/s2qdm1t01nr |pages=54–55 |url=http://bitsavers.informatik.uni-stuttgart.de/magazines/S-100_Microsystems/v01n05.pdf |access-date=2023-11-27 |url-status=live |archive-url=https://web.archive.org/web/20231127182118/http://bitsavers.informatik.uni-stuttgart.de/magazines/S-100_Microsystems/v01n05.pdf |archive-date=2023-11-27}} [https://archive.org/details/bitsavers_S100Micros_46725899/page/54/mode/1up][https://archive.org/details/v1n5_20230524/page/54/mode/1up] (2 pages) (NB. Describes page boundary relocation and relocating assemblers.)<!-- In the article the author announces a more detailed article at a later stage - this still has to be found -->
* {{cite web |title=Legacy of Gary Kildall: The CP/M IEEE Milestone Dedication |author-first1=Robert |author-last1=Huitt |author-first2=Gordon |author-last2=Eubanks |author-link2=Gordon Eubanks |author-first3=Thomas "Tom" Alan |author-last3=Rolander |author-link3=Thomas Alan Rolander |author-first4=David |author-last4=Laws |author-first5=Howard E. |author-last5=Michel |author-first6=Brian |author-last6=Halla |author-first7=John Harrison |author-last7=Wharton |author-link7=John Harrison Wharton |author-first8=Brian |author-last8=Berg |author-first9=Weilian |author-last9=Su |author-first10=Scott |author-last10=Kildall |author-link10=Scott Kildall |author-first11=Bill |author-last11=Kampe |editor-first=David |editor-last=Laws |date=2014-04-25 |___location=Pacific Grove, California, USA |type=video transscription |id=CHM Reference number: X7170.2014 |publisher=[[Computer History Museum]] |url=https://archive.computerhistory.org/resources/access/text/2014/06/102746909-05-01-acc.pdf |access-date=2020-01-19 |url-status=live |archive-url=https://web.archive.org/web/20141227142045/http://archive.computerhistory.org/resources/access/text/2014/06/102746909-05-01-acc.pdf |archive-date=2014-12-27 |quote=[…] Laws: […] "dynamic relocation" of the OS. Can you tell us what that is and why it was important? […] [[Gordon Eubanks|Eubanks]]: […] what [[Gary Arlen Kildall|Gary]] did […] was […] mind boggling. […] I remember the day at the [[Naval Postgraduate School|school]] he came bouncing into the lab and he said, I have figured out how to relocate. He took advantage of the fact that the only byte was always going to be the [[high order byte]]. And so he created a [[bitmap]]. […] 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 64K [[CP/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 the [[little-endian|bytes reversed]], right, for the memory addresses. But they were always in the same place, so you could relocate it on a [[256 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 […]}} [https://ethw.org/Milestones:The_CP/M_Microcomputer_Operating_System,_1974][https://www.youtube.com/watch?v=HO6IPpL0y8g] (33 pages)
* {{cite journal |title=CP/M 2 lernt dazu. Modulare Systemerweiterungen auch für das 'alte' CP/M |language=de |type=part 1 |author-first1=Eckhard |author-last1=Lieber |author-first2=Thomas |author-last2=von Massenbach |journal=[[c't - magazin für computertechnik]] |publisher=[[Heise Verlag]] |date=1987 |volume=1987 |issue=1 |pages=124–135 |postscript=;}} {{cite journal |title=CP/M 2 lernt dazu. Modulare Systemerweiterungen auch für das 'alte' CP/M |language=de |type=part 2 |author-first1=Eckhard |author-last1=Lieber |author-first2=Thomas |author-last2=von Massenbach |journal=[[c't - magazin für computertechnik]] |publisher=[[Heise Verlag]] |date=1987 |volume=1987 |issue=2 |pages=78–85 |postscript=;}} {{cite web |title=RSM für CP/M 2.2 |language=de |date=2016-10-09 |author-first=Alex |author-last=Huck |work=Homecomputer DDR |url=http://hc-ddr.hucki.net/wiki/doku.php/cpm:rsm:ct |access-date=2016-11-25 |url-status=live |archive-url=https://web.archive.org/web/20161125023237/http://hc-ddr.hucki.net/wiki/doku.php/cpm%3Arsm%3Act |archive-date=2016-11-25}}
* {{cite web |title=Re: CP/M assembly language programming |author-first=Charles "Chuck" P. |author-last=Guzis |date=2015-03-16 |work=Vintage Computer Forum |series=Genre: CP/M and MP/M |url=http://www.vcfed.org/forum/showthread.php?46767-CP-M-assembly-language-programming&p=360328#post360328 |access-date=2020-02-01 |url-status=live |archive-url=https://web.archive.org/web/20200201052736/http://www.vcfed.org/forum/showthread.php?46767-CP-M-assembly-language-programming&p=360328 |archive-date=2020-02-01 |quote=[…] Ever wonder how {{ill|MOVCPM|pl|MOVCPM (CP/M)}} works? Since the [[BDOS]] and [[Console Command Processor|CCP]] is in high memory, above the user application, addresses have to be changed every time the system memory size is changed. Now that requires relocating addresses in [[8080]] code, since [[relative addressing]] is not part of the hardware. Without implementing a full-blown relocating assembler and loader, how does one go about this? It's actually pretty clever and [[MP/M]] even uses this scheme to construct its page-relocatable files. You simply assemble the source program ''twice'' with the second assembly origin 100H (256 bytes) higher than the first. The two binary images are then compared, byte for byte, and a [[bitmap|map]] constructed of where pairs of bytes differ in value by exactly 100H. The result is a list of locations where the relocation value needs to be adjusted if the ___location of a program in memory is to be moved. MP/M calls this sort of file PRL (page relocatable), but I don't know that CP/M 2.2 ever coined a name for it. […]}}
* {{cite web |title=Re: How does MOVCPM.COM work? |author-first=Charles "Chuck" P. |author-last=Guzis |date=2015-07-29 |work=Vintage Computer Forum |series=Genre: CP/M and MP/M |url=http://www.vcfed.org/forum/showthread.php?48532-How-does-MOVCPM-COM-work&p=376438#post376438 |access-date=2020-02-01 |url-status=live |archive-url=https://web.archive.org/web/20200201053054/http://www.vcfed.org/forum/showthread.php?48532-How-does-MOVCPM-COM-work=&p=376438 |archive-date=2020-02-01 |quote=[…] {{ill|MOVCPM|pl|MOVCPM (CP/M)}} uses an early type of PRL format. Basically, [[CP/M]] is assembled twice; the second time is 100H bytes offset. The two binaries are compared and a [[bitmap]] constructed. A set bit implies that the [[high-order byte]] of an address is to be adjusted. Low order address bytes are not affected; hence, "Page
* {{cite web |title=Re: Is it safe to use RST 28h in CP/M assembly programs? |author-first=Charles "Chuck" P. |author-last=Guzis |date=2016-11-08 |work=Vintage Computer Forum |series=Genre: CP/M and MP/M |url=http://www.vcfed.org/forum/showthread.php?54822-Is-it-safe-to-use-RST-28h-in-CP-M-assembly-programs&p=434954#post434954 |access-date=2020-02-01 |url-status=live |archive-url=https://web.archive.org/web/20200201053327/http://www.vcfed.org/forum/showthread.php?54822-Is-it-safe-to-use-RST-28h-in-CP-M-assembly-programs&p=434954 |archive-date=2020-02-01 |quote=[…] I've referenced PRL files and how they originally got their start with {{ill|MOVCPM|pl|MOVCPM (CP/M)}}, but became an integral part of [[MP/M]] and [[CP/M 3.0]]. But PRL files use a [[bitmap|bit map]] in which every bit corresponds to a memory ___location; one bits indicate that a page relocation offset should be added to the corresponding memory ___location. If you have very few absolute memory references (as opposed to relative ones) you may want to employ a pointer list (2 bytes per reference) rather than a bitmap. This is unlikely in [[8080]] code which doesn't have relative jumps, but may be a consideration for [[Z80]] code. The trick to quickly find this out is to assemble your program twice; the second time offset by 100H, then compare the two binaries. The advantage of [[runtime (program lifecycle phase)|run-time]] relocation is that you don't have to incur a penalty for code that attempts to get around the relocation issue--no "tricks"; just write straight code. […]}}
* {{cite journal |author-first=Richard L. |author-last=Roth |title=Relocation Is Not Just Moving Programs |___location=Ridgefield,
* {{cite book |title=Assemblers, Compilers, and Program Translation |chapter=8.2.2 Relocating Loader |pages=[https://archive.org/details/assemblerscompil00cali/page/237 237]–241 |author-first=Peter |author-last=Calingaert |editor-first=Ellis |editor-last=Horowitz |editor-link=Ellis Horowitz |date=1979 |orig-
* {{cite book |title=The Microsoft OBJ File Format |publisher=[[Microsoft]], Product Support Services |id=Application Note SS0288 |url=https://www.fileformat.info/format/ms-obj/corion.htm |access-date=2017-08-21 |url-status=live |archive-url=https://archive.today/20170909092856/http://www.fileformat.info/format/ms-obj/corion.htm |archive-date=2017-09-09}}
* {{cite book |author-first1=Andrew Stuart |author-last1=Tanenbaum |author-link1=Andrew Stuart Tanenbaum |author-first2=Herbert |author-last2=Bos |title=Modern Operating Systems |edition=4 |date=2015 |publisher=[[Pearson Education Inc.]] |isbn=978-0-13359162-0}}
* {{cite web |author-first=John C. |author-last=Elliott |title=PRL file format |date=2012-06-05 |orig-
* {{cite web |author-first=John C. |author-last=Elliott |title=Microsoft REL format |date=2012-06-05 |orig-
* {{cite web |title=Support for PRL, page relocatable executable for MP/M |date=2018-09-05 |orig-
* {{cite journal |title=Understanding Relocatable Code |series=The Next Step |author-first=Hardin |author-last=Brothers |journal=[[80 Micro]] |publisher=[[1001001, Inc.]] |issn=0744-7868 |date=April 1983 |issue=39 |pages=[https://archive.org/details/80-microcomputing-magazine-1983-04/page/n37 38], 40, 42, 45 |url=https://archive.org/details/80-microcomputing-magazine-1983-04 |access-date=2020-02-06}} [https://archive.org/stream/80-microcomputing-magazine-1983-04/80Microcomputing_0483_djvu.txt][https://archive.org/download/80-microcomputing-magazine-1983-04/80Microcomputing_0483_text.pdf]
* {{cite journal |title=Relocatable Programs: Microcomputing's Hoboes |series=The Next Step |author-first=Hardin |author-last=Brothers |journal=[[80 Micro]] |publisher=[[CW Communications/Peterborough, Inc.]] |issn=0744-7868 |date=April 1985 |issue=63 <!-- |contribution=illustration |contributor-first=Peter |contributor-last=Bono --> |pages=[https://archive.org/details/80-microcomputing-magazine-1985-04/page/n99 98], 100, 102–103 |url=https://archive.org/details/80-microcomputing-magazine-1985-04 |access-date=2020-02-06
<!-- * Jesse Bob Overholt column: The Alternate Source Journal #16 James Farvour Microsoft BASIC Decoded And Other Mysteries POP HL, JP (HL) E1h E9h at offset 000Bh (+11) -->
* {{cite journal |title=ZCPR 3.4 - Type-4 Programs |series=ZCPR3 Corner |author-first=Jay |author-last=Sage |journal=[[The Computer Journal (US journal)<!-- to be distinguished from the identically named UK journal -->|The Computer Journal]] (TCJ) - Programming, User Support, Applications |issn=0748-9331 |editor-first=Art |editor-last=Carlson |date=May–June 1988 |___location=Columbia Falls, Montana, USA |issue=32 |pages=[https://archive.org/details/the-computer-journal-32/page/n10/mode/1up 10]–17 [<nowiki/>[https://archive.org/details/the-computer-journal-32/page/n15/mode/1up 15]–16] |id=ark:/13960/t1wd4v943 |url=https://archive.org/details/the-computer-journal-32 |access-date=2021-11-29}} [https://archive.org/stream/the-computer-journal-32/tcj_32_May-June_1988_djvu.txt][https://archive.org/download/the-computer-journal-32/tcj_32_May-June_1988_text.pdf]
|