HP-UX Linker and Libraries User's Guide
8 Writing and Generating Position-Independent Code
This chapter is useful mainly to programmers who want to write position-independent assembly
language code, or who want to convert existing assembly language programs to be position
independent. It is also of interest to compiler developers. This chapter assumes you have a good
understanding of virtual memory concepts and memory management. This chapter discusses the
following topics:
• “What is a Relocatable Object Code?” (page 185)
• “What is a Absolute Object Code?” (page 185)
• “What is a Position-Independent Code?” (page 186)
• “Generating Position-Independent Code” (page 186)
NOTE: This chapter is applicable to only PA-RISC 32-bit applications. Throughout this chapter,
examples of position-independent code (PIC) are shown in assembly code. For the corresponding
information for 64-bit mode, see 64-bit Runtime Architecture for PA-RISC 2.0 available at:
http://devresource.hp.com/drc/STK/docs/archive/pa64rt.pdf
For the corresponding information for IPF applications, see Itanium Software Conventions and
Runtime Architecture available at:
http://devresource.hp.com/drc/resources/ia64rt-12-gen.pdf
What is a Relocatable Object Code?
Relocatable object code is machine code that is generated by compilers and assemblers and stored
in relocatable object files or .o files. A relocatable object file contains symbolic references to
locations defined within the compilation unit as well as symbolic references to locations defined
outside the compilation unit. The object file also contains relocation information. The linker uses
this information to replace the symbolic references with actual addresses.
For example, if you write a program that references the external variable errno, the object code
created by the compiler contains only a symbolic reference to errno because errno is not defined
in your program. Only when the linker links this object code does the reference to errno change
(relocate) to an absolute address in virtual memory.
If your program defines a global variable, the compiler assigns a relocatable address to that
variable. The compiler also marks all references to that variable as relocatable. The linker replaces
the references to the variable with the absolute address of the variable.
What is a Absolute Object Code?
Absolute object code is a machine code that contains references to actual addresses within the
program's address space. When the linker combines relocatable object files to build a program
file or a.out file, it writes absolute object code into the file. Thus, when the program is executed,
its routines and data reside at the addresses determined by the linker.
Note that absolute object code does not contain physical addresses. Physical addresses refer to
exact locations in physical memory. Instead, absolute object code contains virtual addresses within
a process's address space. These virtual addresses are mapped to physical addresses by the HP-UX
virtual memory management system.
Program files contain absolute virtual addresses. Hence exec the HP-UX program loader, must
always load the code and data into the same location within a process's address space. Because
this code always resides at the same location within the address space, and because it contains
virtual addresses, it is not suitable for shared libraries, although it can be shared by several
processes running the same program.
What is a Relocatable Object Code? 185