2 .\" Copyright 1989 AT&T Copyright (c) 2001, Sun Microsystems, Inc. All Rights Reserved
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH ELF_GETDATA 3ELF "Jul 11, 2001"
8 elf_getdata, elf_newdata, elf_rawdata \- get section data
12 cc [ \fIflag\fR ... ] \fIfile\fR ... \fB-lelf\fR [ \fIlibrary\fR ... ]
15 \fBElf_Data *\fR\fBelf_getdata\fR(\fBElf_Scn *\fR\fIscn\fR, \fBElf_Data *\fR\fIdata\fR);
20 \fBElf_Data *\fR\fBelf_newdata\fR(\fBElf_Scn *\fR\fIscn\fR);
25 \fBElf_Data *\fR\fBelf_rawdata\fR(\fBElf_Scn *\fR\fIscn\fR, \fBElf_Data *\fR\fIdata\fR);
31 These functions access and manipulate the data associated with a section
32 descriptor, \fIscn\fR. When reading an existing file, a section will have a
33 single data buffer associated with it. A program may build a new section in
34 pieces, however, composing the new data from multiple data buffers. For this
35 reason, the data for a section should be viewed as a list of buffers, each of
36 which is available through a data descriptor.
39 The \fBelf_getdata()\fR function lets a program step through a section's data
40 list. If the incoming data descriptor, \fIdata\fR, is null, the function
41 returns the first buffer associated with the section. Otherwise, \fIdata\fR
42 should be a data descriptor associated with \fIscn\fR, and the function gives
43 the program access to the next data element for the section. If \fIscn\fR is
44 null or an error occurs, \fBelf_getdata()\fR returns a null pointer.
47 The \fBelf_getdata()\fR function translates the data from file representations
48 into memory representations (see \fBelf32_xlatetof\fR(3ELF)) and presents
49 objects with memory data types to the program, based on the file's \fIclass\fR
50 (see \fBelf\fR(3ELF)). The working library version (see
51 \fBelf_version\fR(3ELF)) specifies what version of the memory structures the
52 program wishes \fBelf_getdata()\fR to present.
55 The \fBelf_newdata()\fR function creates a new data descriptor for a section,
56 appending it to any data elements already associated with the section. As
57 described below, the new data descriptor appears empty, indicating the element
58 holds no data. For convenience, the descriptor's type (\fBd_type\fR below) is
59 set to \fBELF_T_BYTE\fR, and the version (\fBd_version\fR below) is set to the
60 working version. The program is responsible for setting (or changing) the
61 descriptor members as needed. This function implicitly sets the
62 \fBELF_F_DIRTY\fR bit for the section's data (see \fBelf_flagdata\fR(3ELF)). If
63 \fI scn\fR is null or an error occurs, \fBelf_newdata()\fR returns a null
67 The \fBelf_rawdata()\fR function differs from \fBelf_getdata()\fR by returning
68 only uninterpreted bytes, regardless of the section type. This function
69 typically should be used only to retrieve a section image from a file being
70 read, and then only when a program must avoid the automatic data translation
71 described below. Moreover, a program may not close or disable (see
72 \fBelf_cntl\fR(3ELF)) the file descriptor associated with \fIelf\fR before the
73 initial raw operation, because \fBelf_rawdata()\fR might read the data from the
74 file to ensure it doesn't interfere with \fBelf_getdata()\fR. See
75 \fBelf_rawfile\fR(3ELF) for a related facility that applies to the entire file.
76 When \fBelf_getdata()\fR provides the right translation, its use is recommended
77 over \fBelf_rawdata()\fR. If \fIscn\fR is null or an error occurs,
78 \fBelf_rawdata()\fR returns a null pointer.
81 The \fBElf_Data\fR structure includes the following members:
96 These members are available for direct manipulation by the program.
97 Descriptions appear below.
104 A pointer to the data buffer resides here. A data element with no data has a
114 This member's value specifies the type of the data to which \fBd_buf\fR points.
115 A section's type determines how to interpret the section contents, as
125 This member holds the total size, in bytes, of the memory occupied by the data.
126 This may differ from the size as represented in the file. The size will be zero
127 if no data exist. (See the discussion of \fBSHT_NOBITS\fR below for more
137 This member gives the offset, within the section, at which the buffer resides.
138 This offset is relative to the file's section, not the memory object's.
147 This member holds the buffer's required alignment, from the beginning of the
148 section. That is, \fBd_off\fR will be a multiple of this member's value. For
149 example, if this member's value is \fB4\fR, the beginning of the buffer will be
150 four-byte aligned within the section. Moreover, the entire section will be
151 aligned to the maximum of its constituents, thus ensuring appropriate alignment
152 for a buffer within the section and within the file.
158 \fB\fBd_version\fR\fR
161 This member holds the version number of the objects in the buffer. When the
162 library originally read the data from the object file, it used the working
163 version to control the translation to memory objects.
169 As mentioned above, data buffers within a section have explicit alignment
170 constraints. Consequently, adjacent buffers sometimes will not abut, causing
171 ``holes'' within a section. Programs that create output files have two ways of
172 dealing with these holes.
175 First, the program can use \fBelf_fill()\fR to tell the library how to set the
176 intervening bytes. When the library must generate gaps in the file, it uses the
177 fill byte to initialize the data there. The library's initial fill value is
178 \fB0\fR, and \fBelf_fill()\fR lets the application change that.
181 Second, the application can generate its own data buffers to occupy the gaps,
182 filling the gaps with values appropriate for the section being created. A
183 program might even use different fill values for different sections. For
184 example, it could set text sections' bytes to no-operation instructions, while
185 filling data section holes with zero. Using this technique, the library finds
186 no holes to fill, because the application eliminated them.
187 .SS "Section and Memory Types"
190 The \fBelf_getdata()\fR function interprets sections' data according to the
191 section type, as noted in the section header available through
192 \fBelf32_getshdr()\fR. The following table shows the section types and how the
193 library represents them with memory data types for the 32-bit file class. Other
194 classes would have similar tables. By implication, the memory data types
195 control translation by \fBelf32_xlatetof\fR(3ELF)
202 Section Type Elf_Type 32-bit Type
203 \fBSHT_DYNAMIC\fR \fBELF_T_DYN\fR \fBElf32_Dyn\fR
204 \fBSHT_DYNSYM\fR \fBELF_T_SYM\fR \fBElf32_Sym\fR
205 \fBSHT_FINI_ARRAY\fR \fBELF_T_ADDR\fR \fBElf32_Addr\fR
206 \fBSHT_GROUP\fR \fBELF_T_WORD\fR \fBElf32_Word\fR
207 \fBSHT_HASH\fR \fBELF_T_WORD\fR \fBElf32_Word\fR
208 \fBSHT_INIT_ARRAY\fR \fBELF_T_ADDR\fR \fBElf32_Addr\fR
209 \fBSHT_NOBITS\fR \fBELF_T_BYTE\fR \fBunsigned char\fR
210 \fBSHT_NOTE\fR \fBELF_T_NOTE\fR \fBunsigned char\fR
211 \fBSHT_NULL\fR \fInone\fR \fInone\fR
212 \fBSHT_PREINIT_ARRAY\fR \fBELF_T_ADDR\fR \fBElf32_Addr\fR
213 \fBSHT_PROGBITS\fR \fBELF_T_BYTE\fR \fBunsigned char\fR
214 \fBSHT_REL\fR \fBELF_T_REL\fR \fBElf32_Rel\fR
215 \fBSHT_RELA\fR \fBELF_T_RELA\fR \fBElf32_Rela\fR
216 \fBSHT_STRTAB\fR \fBELF_T_BYTE\fR \fBunsigned char\fR
217 \fBSHT_SYMTAB\fR \fBELF_T_SYM\fR \fBElf32_Sym\fR
218 \fBSHT_SUNW_comdat\fR \fBELF_T_BYTE\fR \fBunsigned char\fR
219 \fBSHT_SUNW_move\fR \fBELF_T_MOVE\fR \fBElf32_Move\fR (sparc)
220 \fBSHT_SUNW_move\fR \fBELF_T_MOVEP\fR \fBElf32_Move\fR (ia32)
221 \fBSHT_SUNW_syminfo\fR \fBELF_T_SYMINFO\fR \fBElf32_Syminfo\fR
222 \fBSHT_SUNW_verdef\fR \fBELF_T_VDEF\fR \fBElf32_Verdef\fR
223 \fBSHT_SUNW_verneed\fR \fBELF_T_VNEED\fR \fBElf32_Verneed\fR
224 \fBSHT_SUNW_versym\fR \fBELF_T_HALF\fR \fBElf32_Versym\fR
225 \fIother\fR \fBELF_T_BYTE\fR \fBunsigned char\fR
230 The \fBelf_rawdata()\fR function creates a buffer with type \fBELF_T_BYTE\fR.
233 As mentioned above, the program's working version controls what structures the
234 library creates for the application. The library similarly interprets section
235 types according to the versions. If a section type belongs to a version newer
236 than the application's working version, the library does not translate the
237 section data. Because the application cannot know the data format in this case,
238 the library presents an untranslated buffer of type \fBELF_T_BYTE\fR, just as
239 it would for an unrecognized section type.
242 A section with a special type, \fBSHT_NOBITS\fR, occupies no space in an object
243 file, even when the section header indicates a non-zero size.
244 \fBelf_getdata()\fR and \fBelf_rawdata()\fR work on such a section, setting the
245 \fIdata\fR structure to have a null buffer pointer and the type indicated
246 above. Although no data are present, the \fBd_size\fR value is set to the size
247 from the section header. When a program is creating a new section of type
248 \fBSHT_NOBITS\fR, it should use \fBelf_newdata()\fR to add data buffers to the
249 section. These empty data buffers should have the \fBd_size\fR members set to
250 the desired size and the \fBd_buf\fR members set to \fI NULL\fR.
253 \fBExample 1 \fRA sample program of calling \fBelf_getdata()\fR.
256 The following fragment obtains the string table that holds section names
257 (ignoring error checking). See \fBelf_strptr\fR(3ELF) for a variation of string
263 ehdr = elf32_getehdr(elf);
264 scn = elf_getscn(elf, (size_t)ehdr->e_shstrndx);
265 shdr = elf32_getshdr(scn);
266 if (shdr->sh_type != SHT_STRTAB)
268 /* not a string table */
271 if ((data = elf_getdata(scn, data)) == 0 || data->d_size == 0)
273 /* error or no data */
280 The \fBe_shstrndx\fR member in an \fBELF\fR header holds the section table
281 index of the string table. The program gets a section descriptor for that
282 section, verifies it is a string table, and then retrieves the data. When this
283 fragment finishes, \fBdata->d_buf\fR points at the first byte of the string
284 table, and \fBdata->d_size\fR holds the string table's size in bytes.
289 See \fBattributes\fR(5) for descriptions of the following attributes:
297 ATTRIBUTE TYPE ATTRIBUTE VALUE
299 Interface Stability Stable
307 \fBelf\fR(3ELF), \fBelf32_getehdr\fR(3ELF), \fBelf64_getehdr\fR(3ELF),
308 \fBelf32_getshdr\fR(3ELF), \fBelf64_getshdr\fR(3ELF),
309 \fBelf32_xlatetof\fR(3ELF), \fBelf64_xlatetof\fR(3ELF), \fBelf_cntl\fR(3ELF),
310 \fBelf_fill\fR(3ELF), \fBelf_flagdata\fR(3ELF), \fBelf_getscn\fR(3ELF),
311 \fBelf_rawfile\fR(3ELF), \fBelf_strptr\fR(3ELF), \fBelf_version\fR(3ELF),
312 \fBlibelf\fR(3LIB), \fBattributes\fR(5)