Position-independent code

In computing, position-independent code[1] (PIC[1]) or position-independent executable (PIE)[2] is a body of machine code that executes properly regardless of its memory address.[a] PIC is commonly used for shared libraries, so that the same library code can be loaded at a location in each program's address space where it does not overlap with other memory in use by, for example, other shared libraries. PIC was also used on older computer systems that lacked an MMU,[3] so that the operating system could keep applications away from each other even within the single address space of an MMU-less system.

Position-independent code can be executed at any memory address without modification. This differs from absolute code,[1] which must be loaded at a specific location to function correctly,[1] and load-time locatable (LTL) code,[1] in which a linker or program loader modifies a program before execution, so it can be run only from a particular memory location.[1] Generating position-independent code is often the default behavior for compilers, but they may place restrictions on the use of some language features, such as disallowing use of absolute addresses (position-independent code has to use relative addressing). Instructions that refer directly to specific memory addresses sometimes execute faster, and replacing them with equivalent relative-addressing instructions may result in slightly slower execution, although modern processors make the difference practically negligible.[4]

History

In early computers such as the IBM 701[5] (29 April 1952) or the UNIVAC I (31 March 1951) code was not position-independent: each program was built to load into and run from a particular address. Those early computers did not have an operating system and were not multitasking-capable. Programs were loaded into main storage (or even stored on magnetic drum for execution directly from there) and run one at a time. In such an operational context, position-independent code was not necessary.

Even on base and bounds[b] systems such as the CDC 6600, the GE 625 and the UNIVAC 1107, once the OS loaded code into a job's storage, it could only run from the relative address at which it was loaded.

Burroughs introduced a segmented system, the B5000 (1961), in which programs addressed segments indirectly via control words on the stack or in the program reference table (PRT); a shared segment could be addressed via different PRT locations in different processes. Similarly, on the later B6500, all segment references were via positions in a stack frame.

The IBM System/360 (7 April 1964) was designed with truncated addressing similar to that of the UNIVAC III,[6] with code position independence in mind. In truncated addressing, memory addresses are calculated from a base register and an offset. At the beginning of a program, the programmer must establish addressability by loading a base register; normally, the programmer also informs the assembler with a USING pseudo-op. The programmer can load the base register from a register known to contain the entry point address, typically R15, or can use the BALR (Branch And Link, Register form) instruction (with a R2 Value of 0) to store the next sequential instruction's address into the base register, which was then coded explicitly or implicitly in each instruction that referred to a storage location within the program. Multiple base registers could be used, for code or for data. Such instructions require less memory because they do not have to hold a full 24, 31, 32, or 64 bit address (4 or 8 bytes), but instead a base register number (encoded in 4 bits) and a 12–bit address offset (encoded in 12 bits), requiring only two bytes.

This programming technique is standard on IBM S/360 type systems. It has been in use through to today's IBM System/z. When coding in assembly language, the programmer has to establish addressability for the program as described above and also use other base registers for dynamically allocated storage. Compilers automatically take care of this kind of addressing.

IBM's early operating system DOS/360 (1966) was not using virtual storage (since the early models of System S/360 did not support it), but it did have the ability to place programs to an arbitrary (or automatically chosen) storage location during loading via the PHASE name,* JCL (Job Control Language) statement.

So, on S/360 systems without virtual storage, a program could be loaded at any storage location, but this required a contiguous memory area large enough to hold that program. Sometimes memory fragmentation would occur from loading and unloading differently sized modules. Virtual storage - by design - does not have that limitation.

While DOS/360 and OS/360 did not support PIC, transient SVC routines in OS/360 could not contain relocatable address constants and could run in any of the transient areas without relocation.

IBM first introduced virtual storage on IBM System/360 model 67 in (1965) to support IBM's first multi-tasking operating and time-sharing operating system TSS/360. Later versions of DOS/360 (DOS/VS etc.) and later IBM operating systems all utilized virtual storage. Truncated addressing remained as part of the base architecture, and still advantageous when multiple modules must be loaded into the same virtual address space.

By way of comparison, on early segmented systems such as Burroughs MCP on the Burroughs B5000 (1961) and Multics (1964), and on paging systems such as IBM TSS/360 (1967)[c], code was also inherently position-independent, since subroutine virtual addresses in a program were located in private data external to the code, e.g., program reference table, linkage segment, prototype section.

The invention of dynamic address translation (the function provided by an MMU) originally reduced the need for position-independent code because every process could have its own independent address space (range of addresses). However, multiple simultaneous jobs using the same code created a waste of physical memory. If two jobs run entirely identical programs, dynamic address translation provides a solution by allowing the system simply to map two different jobs' address 32K to the same bytes of real memory, containing the single copy of the program.

Different programs may share common code. For example, the payroll program and the accounts receivable program may both contain an identical sort subroutine. A shared module (a shared library is a form of shared module) gets loaded once and mapped into the two address spaces.

SunOS 4.x and ELF

Procedure calls inside a shared library are typically made through small procedure linkage table (PLT) stubs, which then call the definitive function. This notably allows a shared library to inherit certain function calls from previously loaded libraries rather than using its own versions.[7]

Data references from position-independent code are usually made indirectly, through Global Offset Tables (GOTs), which store the addresses of all accessed global variables. There is one GOT per compilation unit or object module, and it is located at a fixed offset from the code (although this offset is not known until the library is linked). When a linker links modules to create a shared library, it merges the GOTs and sets the final offsets in code. It is not necessary to adjust the offsets when loading the shared library later.[7]

Position-independent functions accessing global data start by determining the absolute address of the GOT given their own current program counter value. This often takes the form of a fake function call in order to obtain the return value on stack (x86), in a specific standard register (SPARC, MIPS), or a special register (POWER/PowerPC/Power ISA), which can then be moved to a predefined standard register, or to obtain it into that standard register (PA-RISC, Alpha, ESA/390 and z/Architecture). Some processor architectures, such as the Motorola 68000, ARM, x86-64, newer versions of z/Architecture, Motorola 6809, WDC 65C816, and Knuth's MMIX allow referencing data by offset from the program counter. This is specifically targeted at making position-independent code smaller, less register demanding and hence more efficient.

Windows DLLs

Dynamic-link libraries (DLLs) in Microsoft Windows use variant E8 of the CALL instruction (Call near, relative, displacement relative to next instruction). These instructions do not need modification when the DLL is loaded.

Some global variables (e.g. arrays of string literals, virtual function tables) are expected to contain an address of an object in data section respectively in code section of the dynamic library; therefore, the stored address in the global variable must be updated to reflect the address where the DLL was loaded to. The dynamic loader calculates the address referred to by a global variable and stores the value in such global variable; this triggers copy-on-write of a memory page containing such global variable. Pages with code and pages with global variables that do not contain pointers to code or global data remain shared between processes. This operation must be done in any OS that can load a dynamic library at arbitrary address.

In Windows Vista and later versions of Windows, the relocation of DLLs and executables is done by the kernel memory manager, which shares the relocated binaries across multiple processes. Images are always relocated from their preferred base addresses, achieving address space layout randomization (ASLR).[8]

Versions of Windows prior to Vista require that system DLLs be prelinked at non-conflicting fixed addresses at the link time in order to avoid runtime relocation of images. Runtime relocation in these older versions of Windows is performed by the DLL loader within the context of each process, and the resulting relocated portions of each image can no longer be shared between processes.

The handling of DLLs in Windows differs from the earlier OS/2 procedure it derives from. OS/2 presents a third alternative and attempts to load DLLs that are not position-independent into a dedicated "shared arena" in memory, and maps them once they are loaded. All users of the DLL are able to use the same in-memory copy.

Multics

In Multics each procedure conceptually[d] has a code segment and a linkage segment.[9] [10] The code segment contains only code and the linkage section serves as a template for a new linkage segment. Pointer register 4 (PR4) points to the linkage segment of the procedure. A call to a procedure saves PR4 in the stack before loading it with a pointer to the callee's linkage segment. The procedure call uses an indirect pointer pair[11] with a flag to cause a trap on the first call so that the dynamic linkage mechanism can add the new procedure and its linkage segment to the Known Segment Table (KST), construct a new linkage segment, put their segment numbers in the caller's linkage section and reset the flag in the indirect pointer pair.

TSS

In IBM S/360 Time Sharing System (TSS/360 and TSS/370) each procedure may have a read-only public CSECT and a writable private Prototype Section (PSECT). A caller loads a V-constant for the routine into General Register 15 (GR15) and copies an R-constant for the routine's PSECT into the 19th word of the save area pointed to be GR13.[12]

The Dynamic Loader[13] does not load program pages or resolve address constants until the first page fault.

Position-independent executables

Position-independent executables (PIE) are executable binaries made entirely from position-independent code. While some systems only run PIC executables, there are other reasons they are used. PIE binaries are used in some security-focused Linux distributions to allow PaX or Exec Shield to use address space layout randomization (ASLR) to prevent attackers from knowing where existing executable code is during a security attack using exploits that rely on knowing the offset of the executable code in the binary, such as return-to-libc attacks. (The official Linux kernel since 2.6.12 of 2005 has a weaker ASLR that also works with PIE. It is weak in that randomness is applied to whole ELF file units.)[14]

Apple's macOS and iOS fully support PIE executables as of versions 10.7 and 4.3, respectively; a warning is issued when non-PIE iOS executables are submitted for approval to Apple's App Store but there's no hard requirement yet [when?] and non-PIE applications are not rejected.[15][16]

OpenBSD has PIE enabled by default on most architectures since OpenBSD 5.3, released on 1 May 2013.[17] Support for PIE in statically linked binaries, such as the executables in /bin and /sbin directories, was added near the end of 2014.[18] openSUSE added PIE as a default in 2015-02. Beginning with Fedora 23, Fedora maintainers decided to build packages with PIE enabled as the default.[19] Ubuntu 17.10 has PIE enabled by default across all architectures.[20] Gentoo's new profiles now support PIE by default.[21] Around July 2017, Debian enabled PIE by default.[22]

Android enabled support for PIEs in Jelly Bean[23] and removed non-PIE linker support in Lollipop.[24]

See also

Notes

  1. ^ This allows each process using a shared copy to see it at a different virtual address.
  2. ^ But a separate copy of the code was loaded for each job.
  3. ^ While TSS/360 supported shared PIC, that was not true of all paging systems
  4. ^ There are some technical deviations for performance reasons that are beyond the scope of this article.

References

  1. ^ a b c d e f "Types of Object Code". iRMX 86 Application Loader Reference Manual (PDF). Intel. pp. 1-2, 1-3. Retrieved 2017-08-21. […] Absolute code, and an absolute object module, is code that has been processed by LOC86 to run only at a specific location in memory. The Loader loads an absolute object module only into the specific location the module must occupy. Position-independent code (commonly referred to as PIC) differs from absolute code in that PIC can be loaded into any memory location. The advantage of PIC over absolute code is that PIC does not require you to reserve a specific block of memory. When the Loader loads PIC, it obtains iRMX 86 memory segments from the pool of the calling task's job and loads the PIC into the segments. A restriction concerning PIC is that, as in the PL/M-86 COMPACT model of segmentation […], it can have only one code segment and one data segment, rather than letting the base addresses of these segments, and therefore the segments themselves, vary dynamically. This means that PIC programs are necessarily less than 64K bytes in length. PIC code can be produced by means of the BIND control of LINK86. Load-time locatable code (commonly referred to as LTL code) is the third form of object code. LTL code is similar to PIC in that LTL code can be loaded anywhere in memory. However, when loading LTL code, the Loader changes the base portion of pointers so that the pointers are independent of the initial contents of the registers in the microprocessor. Because of this fixup (adjustment of base addresses), LTL code can be used by tasks having more than one code segment or more than one data segment. This means that LTL programs may be more than 64K bytes in length. FORTRAN 86 and Pascal 86 automatically produce LTL code, even for short programs. LTL code can be produced by means of the BIND control of LINK86. […]
  2. ^ "Position Independent Executables (PIE)".
  3. ^ Levine, John R. (2000) [October 1999]. "Chapter 8: Loading and overlays". Linkers and Loaders. The Morgan Kaufmann Series in Software Engineering and Programming (1 ed.). San Francisco, USA: Morgan Kaufmann. pp. 170–171. ISBN 1-55860-496-0. OCLC 42413382. ISBN 978-1-55860-496-4. Archived from the original on 2012-12-05. Retrieved 2020-01-12. Code: [1][2] Errata: [3]
  4. ^ Gabert, Alexander (January 2004). "Position Independent Code internals". Hardened Gentoo. Retrieved 2009-12-03. […] direct non-PIC-aware addressing is always cheaper (read: faster) than PIC addressing. […]
  5. ^ "701 Announced", IBM, 1952-04-29
  6. ^ Reference Manual UNIVAC III Data Processing System (PDF). Sperry Rand Corporation. 1962. UT-2488.
  7. ^ a b Gingell, Robert A.; Lee, Meng; Dang, Xuong T.; Weeks, Mary S. Shared Libraries in SunOS (PDF). 1987 Summer USENIX Technical Conference and Exhibition. pp. 131–146.
  8. ^ "Advances in Memory Management for Windows". View.officeapps.live.com. Retrieved 2017-06-23.
  9. ^ Organick, Elliott Irving (1972). The Multics system; an examination of its structure. MIT Press. ISBN 9780262150125. LCCN 78157477.
  10. ^ Daley, Robert C.; Dennis, Jack B (May 1968). "Virtual Memory, Processes, and Sharing in Multics". Communications of the ACM. 11 (5). Association for Computing Machinery: 306–312. doi:10.1145/363095.363139. Retrieved 2024-07-21.
  11. ^ "Section 6 Virtual Address Formation", DPS/LEVEL 68 & DPS 8M MULTICS PROCESSOR MANUAL (PDF) (Rev. 1 ed.), Honeywell Information Systems Inc., 1982, pp. 6–21, AL39, retrieved 2023-03-25
  12. ^ "Section 3: TSS for the: Svslcm Programmer". IBM Time Sharing System Concepts and Facilities (PDF) (Seventh ed.). April 1978. p. 61. GC28-2003-6.
  13. ^ IBM System/360 Time Sharing System Dynamic Loader (PDF) (Fourth ed.). September 1971. GY28-2031-3.
  14. ^ Lettieri, G. "Address Space Layout Randomization" (PDF).
  15. ^ "iphone - Non-PIE Binary - The executable 'project name' is not a Position Independent Executable. - Stack Overflow". stackoverflow.com.
  16. ^ "iOS Developer Library". apple.com.
  17. ^ "OpenBSD 5.3 Release". 2013-05-01. Retrieved 2020-05-09.
  18. ^ "Heads Up: Snapshot Upgrades for Static PIE". 2014-12-24. Retrieved 2014-12-24.
  19. ^ "Changes/Harden All Packages - FedoraProject". fedoraproject.org.
  20. ^ "Ubuntu Foundations Team - Weekly Newsletter, 2017-06-15". 2017-06-15. Retrieved 2017-06-17.
  21. ^ "New 17.0 profiles in the Gentoo repository". 2017-11-30. Retrieved 2017-12-10.
  22. ^ Liang, Mudong (2017-08-08). "When did Debian decide to enabled PIE by default?". debian.org. Retrieved 2021-07-06.
  23. ^ "Security Enhancements in Android 1.5 through 4.1  -  Android Open Source Project". Android Open Source Project.
  24. ^ "Security Enhancements in Android 5.0  -  Android Open Source Project". Android Open Source Project.

Read other articles:

National Historical and Cultural Reserve “Hetman's Capital”Location of National Historical and Cultural Reserve “Hetman's Capital”LocationBaturyn, Chernihiv Oblast, UkraineNearest cityBaturynCoordinates51°21′N 32°53′E / 51.350°N 32.883°E / 51.350; 32.883Area0.57 km2 (0.22 sq mi)Established1993Governing bodyMinistry of Culture and Information Policy National Historical and Cultural Reserve Hetman's Capital (Ukrainian: Гетьм�...

Опис файлу Опис Знімок екрану відеогри Darksiders Джерело власний знімок Автор зображення THQ Ліцензія див. нижче Обґрунтування добропорядного використання для статті «Darksiders: Wrath of War» [?] Мета використання Основний ігровий процес відеогри Darksiders: Wrath of War Замінність Замі�...

هذه المقالة يتيمة إذ تصل إليها مقالات أخرى قليلة جدًا. فضلًا، ساعد بإضافة وصلة إليها في مقالات متعلقة بها. (ديسمبر 2020) إدوارد كيوغ   معلومات شخصية الميلاد 22 يناير 1835[1]  مقاطعة كافان[1]  الوفاة 29 نوفمبر 1898 (63 سنة) [2]  ميلواكي، ويسكنسن[2]  مواطنة الولا�...

This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages) This article is written like a personal reflection, personal essay, or argumentative essay that states a Wikipedia editor's personal feelings or presents an original argument about a topic. Please help improve it by rewriting it in an encyclopedic style. (December 2012) (Learn how and when to remove this template message) This article needs ...

NGC 4267   جزء من عنقود العذراء المجري  الكوكبة العذراء[1]  رمز الفهرس NGC 4267 (الفهرس العام الجديد)MCG+02-32-004 (فهرس المجرات الموروفولوجي)PGC 39710 (فهرس المجرات الرئيسية)UGC 7373 (فهرس أوبسالا العام)2MASX J12194528+1247540 (Two Micron All Sky Survey, Extended source catalogue)VCC 369 (Virgo Cluster Catalog)EVCC 353 (Extended Virgo Cluster Catalog...

Núñez UbicaciónCoordenadas 34°32′54″S 58°27′46″O / -34.5483, -58.4628Dirección Manuela Pedraza 1900Barrio NúñezComuna 13Ciudad  Ciudad de Buenos AiresDatos de la estaciónPunto kilométrico 11,4 (desde Retiro)Altitud 9 m s. n. m.Accesibilidad Sí, andenes y boletaríasInauguración 27 de abril de 1873Pasajeros 747.801 por año (año 2022)[1]​Servicios N.º de andenes 3 (uno en desuso)N.º de vías 3 (una en desuso)Plataformas 2Propietario  Estad...

Russian government document on endangered species This article relies largely or entirely on a single source. Relevant discussion may be found on the talk page. Please help improve this article by introducing citations to additional sources.Find sources: Red Data Book of the Russian Federation – news · newspapers · books · scholar · JSTOR (December 2018) Red Data Book of the Russian Federation (RDBRF), also known as Red Book (Russian: Красная к...

Residenza dell'Arte degli AlbergatoriPortale della Residenza dell'Arte degli Albergatori, in situ prima della demolizioneLocalizzazioneStato Italia RegioneToscana LocalitàFirenze IndirizzoVia dei Lamberti (già piazza del Monte di Pietà dei Pilli angolo tra via dei Lontanmorti e via dei Cavalieri) Coordinate43°46′14.37″N 11°15′14.65″E / 43.77066°N 11.25407°E43.77066; 11.25407Coordinate: 43°46′14.37″N 11°15′14.65″E / 43.77066°N 11.2...

Опис файлу Опис Літня людина 80-ти років стоїть на голові без допомоги рук, Голлівуд Джерело Власна робота Час створення 15 лютого 2017 Автор зображення Shustov Ліцензія див. нижче Ліцензування Увага: це зображення не може бути завантажене до Wikimedia Commons. Через значно суворішу пол�...

American photographer Edward BierstadtBorn(1824-09-11)September 11, 1824Solingen, Rhine Province, PrussiaDiedJune 15, 1906(1906-06-15) (aged 81)New York City, U.S.Occupation(s)Photographer, engraver Edward Bierstadt (September 11, 1824 – June 15, 1906) was a photographer of portraits and landscapes as well as an engraver and a pioneer of color photography in the United States. Early life Bierstadt was born in Solingen, Rhine Province, Prussia on September 11, 1824. He was the son of He...

Subway line in Tokyo, Japan You can help expand this article with text translated from the corresponding article in Japanese. (August 2021) Click [show] for important translation instructions. View a machine-translated version of the Japanese article. Machine translation, like DeepL or Google Translate, is a useful starting point for translations, but translators must revise errors as necessary and confirm that the translation is accurate, rather than simply copy-pasting machine-translat...

This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages) This article includes a list of general references, but it lacks sufficient corresponding inline citations. Please help to improve this article by introducing more precise citations. (March 2023) (Learn how and when to remove this template message) This article needs additional citations for verification. Please help improve this article by ...

German-born American baseball player Baseball player Mike BlowersBlowers with the Jacksonville Expos in 1988Third basemanBorn: (1965-04-24) April 24, 1965 (age 58)Würzburg, West GermanyBatted: RightThrew: RightMLB debutSeptember 1, 1989, for the New York YankeesLast MLB appearanceOctober 3, 1999, for the Seattle MarinersMLB statisticsBatting average.257Home runs78Runs batted in365 Teams New York Yankees (1989–1991) Seattle Mariners (1992–1995) Los Angel...

Alabama prison and execution center Holman Correctional FacilityLocationAtmore, AlabamaStatusOpenSecurity classMaximumCapacity1002OpenedDecember 1969Managed byAlabama Department of CorrectionsWardenTerry Raybon William C. Holman Correctional Facility is an Alabama Department of Corrections prison located in Atmore, Alabama.[1] The facility is along Alabama State Highway 21, 9 miles (14 km) north of Atmore in southern Alabama.[2] The facility was originally built to house ...

This article does not cite any sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Nissay Theatre – news · newspapers · books · scholar · JSTOR (September 2023) (Learn how and when to remove this template message) Theater in Chiyoda Ward, Tokyo, JapanNissay TheatreLocation1-1-1 Yūrakuchō, Chiyoda, TokyoOwnerNissay Culture FoundationTypeIndoor theatreSeating ...

أركاديوس باك   معلومات شخصية الميلاد 6 أكتوبر 1974 (49 سنة)  ستارغارد[1]  الطول 1.76 م (5 قدم 9 1⁄2 بوصة) مركز اللعب وسط الجنسية بولندا  المسيرة الاحترافية1 سنوات فريق م. (هـ.) 1992–1993 بلكيتني ستارغارد  [لغات أخرى]‏ 1993–1995 أولمبيا بوزنان 32 (5) 1995 Olimpia-Lechia Gda...

Austrian singer Petra FreyBackground informationBorn (1978-06-27) 27 June 1978 (age 45)WattensYears active1993–presentMusical artist Petra Frey (born 27 June 1978, in Wattens[1]), is an Austrian singer. She has released twelve studio albums and five compilation albums and participated in the Eurovision Song Contest.[1] Biography Frey released her debut album, Bloß Träume im Kopf in 1993. Petra represented Austria in the Eurovision Song Contest in 1994 with the song F�...

Swedish university college University of BoråsHögskolan i BoråsTypepublicEstablished1977[citation needed]Vice-ChancellorMats Tinnsten[1]Academic staff497 (52 professors, 2022)Total staff822 (2022),[2] 497 lecturers (57% PhD)Students18,300[2]LocationBorås, SwedenAffiliationsEUAWebsitehb.se/en The University of Borås (UB), or Högskolan i Borås, is a Swedish university in the city of Borås. As of 2021 it has around 18,300 students and 803 staff. The Swedis...

2005 studio jazz album Big Chief DreamingStudio album by John Tchicai, Garrison Fewell, Tino Tracanna, Paolino Dalla Porta, and Massimo ManziReleased2005RecordedJune 15–16, 2003StudioCavo Studio, Bergamo, ItalyGenreJazzLength51:38LabelSoul Note121385-2Dudu Pukwana chronology John Tchicai with Strings(2005) Big Chief Dreaming(2005) Hymne til Sofia(2005) Big Chief Dreaming is an album by saxophonists John Tchicai and Tino Tracanna, guitarist Garrison Fewell, double bassist Paolino Dalla ...

Pour les articles homonymes, voir Maison de Bragance et Bragance. Si ce bandeau n'est plus pertinent, retirez-le. Cliquez ici pour en savoir plus. Cet article ne cite pas suffisamment ses sources (novembre 2021). Si vous disposez d'ouvrages ou d'articles de référence ou si vous connaissez des sites web de qualité traitant du thème abordé ici, merci de compléter l'article en donnant les références utiles à sa vérifiabilité et en les liant à la section « Notes et références...