Page 167 - DCAP507_SYSTEM_SOFTWARE
P. 167
Unit 10: Programming Languages Concept (I)
10.5.2 Simple Storage Layout Notes
In an uncomplicated but not impractical situation, the input to a linker includes a set of modules,
call them M1 through Mn, each of which includes a single segment starting at location 0 of
length L1 through Ln, and the target address space also begins at zero.
The linker or loader inspects each module in turn, allocating storage in sequence. The beginning
address of Mi is the sum of L1 through Li-1, and the length of the linked program is the sum of
L1 through Ln.
Most architectures need that data be aligned on word boundaries, or at least run faster if data is
aligned, so linkers usually round each Li up to a multiple of the most stringent position that the
architecture needs, usually 4 or 8 bytes.
Example: Presume a main program known as main is to be linked with three subroutines
called calif, mass, and newyork. (It assigns venture capital geographically). The sizes of every
routine are (in hex): l r. name size _ main 1017 calif 920 mass 615 newyork 1390 Suppose that
storage allocation begins at location 1000 hex, and that the alignment is four bytes. Then the
assignments might be: l r. name location _ main 1000 - 2016 calif 2018 - 2937 mass 2938 - 2f4c
newyork 2f50 - 42df Due to alignment, one byte at 2017 and three bytes at 2f4d are wasted, not
sufficient to worry about.
10.5.3 Multiple Segment Types
In all but the easiest object formats, there are numerous kinds of segment, and the linker requires
to group matching segments from all of the input modules jointly. On a Unix system with text
and data segments, the linked file is required to have all of the text composed together, followed
by all of the data, followed reasonably by the BSS. (Although the BSS doesn't take space in the
output file, it is required to have space assigned to resolve BSS symbols, and to designate the size
of BSS to allocate when the output file is loaded.) This needs a two-level storage allocation
strategy.
Now every module Mi has text size Ti, data size Di, and BSS size Bi.
As it reads every input module, the linker assigns space for each of the Ti, Di, and Bi as though
each segment were separately assigned at zero. After reading all of the input files, the linker
now recognizes the total size of each of the three segments, Ttot, Dtot, and Btot. As the data
segment follows the text segment, the linker adds Ttot to the address allocated for each of the
data segments, and as the BSS segment follows both the text and data segments, the linker adds
the sum of Ttot and Dtot to the allocated BSS segments.
Again, the linker typically needs to round up every assigned size.
10.5.4 Segment and Page Alignment
If the text and data segments are loaded into disconnected memory pages, as is usually the case,
the size of the text segment has to be rounded up to a full page and the data and BSS segment
positions equally adjusted. Many Unix systems use a hoax that saves file space by beginning the
data instantly after the text in the object file, and mapping that page in the file into virtual
memory twice, once read-only for the text and once copy-on-write for the data. Then, the data
addresses logically begin precisely one page beyond the end of the text, so instead of rounding
up, the data addresses start exactly 4K or whatever the page size is beyond the end of the text.
LOVELY PROFESSIONAL UNIVERSITY 161