xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/README.LFS (revision 6223:30d6740da48d)
1*6223Sab196087#
2*6223Sab196087# CDDL HEADER START
3*6223Sab196087#
4*6223Sab196087# The contents of this file are subject to the terms of the
5*6223Sab196087# Common Development and Distribution License (the "License").
6*6223Sab196087# You may not use this file except in compliance with the License.
7*6223Sab196087#
8*6223Sab196087# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*6223Sab196087# or http://www.opensolaris.org/os/licensing.
10*6223Sab196087# See the License for the specific language governing permissions
11*6223Sab196087# and limitations under the License.
12*6223Sab196087#
13*6223Sab196087# When distributing Covered Code, include this CDDL HEADER in each
14*6223Sab196087# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*6223Sab196087# If applicable, add the following below this CDDL HEADER, with the
16*6223Sab196087# fields enclosed by brackets "[]" replaced with your own identifying
17*6223Sab196087# information: Portions Copyright [yyyy] [name of copyright owner]
18*6223Sab196087#
19*6223Sab196087# CDDL HEADER END
20*6223Sab196087#
21*6223Sab196087
22*6223Sab196087#
23*6223Sab196087# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6223Sab196087# Use is subject to license terms.
25*6223Sab196087#
26*6223Sab196087# ident	"%Z%%M%	%I%	%E% SMI"
27*6223Sab196087
28*6223Sab196087
29*6223Sab196087
30*6223Sab196087
31*6223Sab196087Why 32-bit libelf is not Large File Aware
32*6223Sab196087-----------------------------------------
33*6223Sab196087
34*6223Sab196087The ELF format uses unsigned 32-bit integers for offsets, so the
35*6223Sab196087theoretical limit on a 32-bit ELF object is 4GB. However, libelf
36*6223Sab196087imposes a 2GB limit on the objects it can create. The Solaris
37*6223Sab196087link-editor and related tools are all based on libelf, so the
38*6223Sab19608732-bit version of the link-editor also has a 2GB limit, despite
39*6223Sab196087the theoretical limit of 4GB.
40*6223Sab196087
41*6223Sab196087Large file support (LFS) is a half step between the 32 and 64-bit
42*6223Sab196087worlds, in which an otherwise 32-bit limited process is allowed to
43*6223Sab196087read and write data to a file that can be larger than 2GB (the extent
44*6223Sab196087of a signed 32-bit integer, as represented by the system type off_t).
45*6223Sab196087LFS is useful if the program only needs to access a small subset of
46*6223Sab196087the file data at any given time (e.g. /usr/bin/cat). It is less useful
47*6223Sab196087if the program needs to access a large amount of data at once --- having
48*6223Sab196087been freed from the file limit, the program will simply hit the virtual
49*6223Sab196087memory limit (4GB).
50*6223Sab196087
51*6223Sab196087In particular, the link-editor generally requires twice as much
52*6223Sab196087memory as the size of the output object, half to hold the input
53*6223Sab196087objects, and half to hold the result. This means that a 32-bit
54*6223Sab196087link-editor process will hit the 2GB file size limit and the 4GB
55*6223Sab196087address space limit at roughly the same time. As a result, a
56*6223Sab196087large file aware 32-bit version of libelf has no significant value.
57*6223Sab196087Despite this, the question of what it would take to make libelf
58*6223Sab196087large file aware comes up from time to time.
59*6223Sab196087
60*6223Sab196087The first step would be to provide alternative versions of
61*6223Sab196087all public data structures that involve the off_t data type.
62*6223Sab196087These structs, found in /usr/include/libelf.h, are:
63*6223Sab196087
64*6223Sab196087	/*
65*6223Sab196087	 * Archive member header
66*6223Sab196087	 */
67*6223Sab196087	typedef struct {
68*6223Sab196087		char		*ar_name;
69*6223Sab196087		time_t		ar_date;
70*6223Sab196087		uid_t		ar_uid;
71*6223Sab196087		gid_t 		ar_gid;
72*6223Sab196087		mode_t		ar_mode;
73*6223Sab196087		off_t		ar_size;
74*6223Sab196087		char 		*ar_rawname;
75*6223Sab196087	} Elf_Arhdr;
76*6223Sab196087
77*6223Sab196087
78*6223Sab196087	/*
79*6223Sab196087	 * Data descriptor
80*6223Sab196087	 */
81*6223Sab196087	typedef struct {
82*6223Sab196087		Elf_Void	*d_buf;
83*6223Sab196087		Elf_Type	d_type;
84*6223Sab196087		size_t		d_size;
85*6223Sab196087		off_t		d_off;		/* offset into section */
86*6223Sab196087		size_t		d_align;	/* alignment in section */
87*6223Sab196087		unsigned	d_version;	/* elf version */
88*6223Sab196087	} Elf_Data;
89*6223Sab196087
90*6223Sab196087As off_t is a signed type, these alternative versions would have to use
91*6223Sab196087an off64_t type instead.
92*6223Sab196087
93*6223Sab196087In addition to providing alternative large file aware Elf_Arhdr and
94*6223Sab196087Elf_Data types, it would be necessary to implement large file aware
95*6223Sab196087versions of the public functions that use them, also found in
96*6223Sab196087/usr/include/libelf.h:
97*6223Sab196087
98*6223Sab196087	/*
99*6223Sab196087	 * Function declarations
100*6223Sab196087	 */
101*6223Sab196087	unsigned  elf_flagdata(Elf_Data *, Elf_Cmd, unsigned);
102*6223Sab196087	Elf_Arhdr *elf_getarhdr(Elf *);
103*6223Sab196087	off_t	  elf_getbase(Elf *);
104*6223Sab196087	Elf_Data  *elf_getdata(Elf_Scn *, Elf_Data *);
105*6223Sab196087	Elf_Data  *elf_newdata(Elf_Scn *);
106*6223Sab196087	Elf_Data  *elf_rawdata(Elf_Scn *, Elf_Data *);
107*6223Sab196087	off_t	  elf_update(Elf *, Elf_Cmd);
108*6223Sab196087	Elf_Data  *elf32_xlatetof(Elf_Data *, const Elf_Data *, unsigned);
109*6223Sab196087	Elf_Data  *elf32_xlatetom(Elf_Data *, const Elf_Data *, unsigned);
110*6223Sab196087	Elf_Data  *elf64_xlatetof(Elf_Data *, const Elf_Data *, unsigned);
111*6223Sab196087	Elf_Data  *elf64_xlatetom(Elf_Data *, const Elf_Data *, unsigned);
112*6223Sab196087
113*6223Sab196087It is important to note that these new versions cannot replace the
114*6223Sab196087original definitions. Those must continue to be available to support
115*6223Sab196087non-largefile-aware programs. These new types and functions would be in
116*6223Sab196087addition to the pre-existing versions.
117*6223Sab196087
118*6223Sab196087When you make code like this large file aware, it is necessary to undertake
119*6223Sab196087a careful analysis of the code to ensure that all the surrounding code uses
120*6223Sab196087variable types large enough to handle the increased range. Hence, this work
121*6223Sab196087is more complicated than simply supplying variants that use a bigger
122*6223Sab196087off_t and rebuilding --- that is just the first step.
123*6223Sab196087
124*6223Sab196087There are two standard preprocessor definitions used to control
125*6223Sab196087large file support:
126*6223Sab196087
127*6223Sab196087	_LARGEFILE64_SOURCE
128*6223Sab196087	_FILE_OFFSET_BITS
129*6223Sab196087
130*6223Sab196087These preprocessor definitions would be used to determine whether
131*6223Sab196087a given program linked against libelf would see the regular, or
132*6223Sab196087the large file aware versons of the above types and routines.
133*6223Sab196087This is the same approach used in other large file capable software,
134*6223Sab196087such as libc.
135*6223Sab196087
136*6223Sab196087Finally, all the applications that rely on libelf would need to be made
137*6223Sab196087large file aware. As with libelf itself, there is more to such an effort
138*6223Sab196087than recompiling with preprocessor macros set. The code in these
139*6223Sab196087applications would need to be examined carefully. Some of these programs
140*6223Sab196087are very old, and were not originally written with such type portability
141*6223Sab196087in mind. Such code can be difficult to transition.
142*6223Sab196087
143*6223Sab196087To work around the 2GB limit in 32-bit libelf:
144*6223Sab196087
145*6223Sab196087    - The fundamental limits of a 32-bit address space mean
146*6223Sab196087      that a program this large should be 64-bit. Only a 64-bit
147*6223Sab196087      address space has enough room for that much code, plus the
148*6223Sab196087      stack and heap needed to do useful work with it.
149*6223Sab196087
150*6223Sab196087    - The 64-bit version of libelf is also able to process
151*6223Sab196087      32-bit objects, and does not have a 2GB file size limit.
152*6223Sab196087      Therefore, the 64-bit link-editor can be used to build a 32-bit
153*6223Sab196087      executable which is >2GB. The resulting program will consume over
154*6223Sab196087      half the available address space just to start running. However,
155*6223Sab196087      there may be enough address space left for it to do useful work.
156*6223Sab196087
157*6223Sab196087      Note that the 32-bit limit for sharable objects remains at
158*6223Sab196087      2GB --- imposed by the runtime linker, which is also not large
159*6223Sab196087      file aware.
160