xref: /freebsd/contrib/elftoolchain/libelf/gelf_xlatetof.3 (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
1.\" Copyright (c) 2006,2008,2018 Joseph Koshy.  All rights reserved.
2.\"
3.\" Redistribution and use in source and binary forms, with or without
4.\" modification, are permitted provided that the following conditions
5.\" are met:
6.\" 1. Redistributions of source code must retain the above copyright
7.\"    notice, this list of conditions and the following disclaimer.
8.\" 2. Redistributions in binary form must reproduce the above copyright
9.\"    notice, this list of conditions and the following disclaimer in the
10.\"    documentation and/or other materials provided with the distribution.
11.\"
12.\" This software is provided by Joseph Koshy ``as is'' and
13.\" any express or implied warranties, including, but not limited to, the
14.\" implied warranties of merchantability and fitness for a particular purpose
15.\" are disclaimed.  in no event shall Joseph Koshy be liable
16.\" for any direct, indirect, incidental, special, exemplary, or consequential
17.\" damages (including, but not limited to, procurement of substitute goods
18.\" or services; loss of use, data, or profits; or business interruption)
19.\" however caused and on any theory of liability, whether in contract, strict
20.\" liability, or tort (including negligence or otherwise) arising in any way
21.\" out of the use of this software, even if advised of the possibility of
22.\" such damage.
23.\"
24.\" $Id: gelf_xlatetof.3 3639 2018-10-14 14:07:02Z jkoshy $
25.\"
26.Dd July 15, 2025
27.Dt GELF_XLATETOF 3
28.Os
29.Sh NAME
30.Nm elf32_xlatetof ,
31.Nm elf32_xlatetom ,
32.Nm elf64_xlatetof ,
33.Nm elf64_xlatetom ,
34.Nm gelf_xlatetof ,
35.Nm gelf_xlatetom
36.Nd translate data between files and memory
37.Sh LIBRARY
38.Lb libelf
39.Sh SYNOPSIS
40.In libelf.h
41.Ft "Elf_Data *"
42.Fn elf32_xlatetof "Elf_Data *dst" "Elf_Data *src" "unsigned int file_encoding"
43.Ft "Elf_Data *"
44.Fn elf32_xlatetom "Elf_Data *dst" "Elf_Data *src" "unsigned int file_encoding"
45.Ft "Elf_Data *"
46.Fn elf64_xlatetof "Elf_Data *dst" "Elf_Data *src" "unsigned int file_encoding"
47.Ft "Elf_Data *"
48.Fn elf64_xlatetom "Elf_Data *dst" "Elf_Data *src" "unsigned int file_encoding"
49.In gelf.h
50.Ft "Elf_Data *"
51.Fo gelf_xlatetof
52.Fa "Elf *elf"
53.Fa "Elf_Data *dst"
54.Fa "Elf_Data *src"
55.Fa "unsigned int file_encoding"
56.Fc
57.Ft "Elf_Data *"
58.Fo gelf_xlatetom
59.Fa "Elf *elf"
60.Fa "Elf_Data *dst"
61.Fa "Elf_Data *src"
62.Fa "unsigned int file_encoding"
63.Fc
64.Sh DESCRIPTION
65These functions translate between the file and memory representations
66of ELF data structures.
67The in-memory representation of an ELF data structure would conform to
68the byte ordering and data alignment restrictions dictated by the host
69processor.
70As described in
71.Xr elf 3 ,
72the file representation of this data structure could use a different byte
73ordering from that of the host, or could use a different layout within
74the file.
75.Pp
76Functions
77.Fn elf32_xlatetom ,
78.Fn elf64_xlatetom ,
79and
80.Fn gelf_xlatetom
81translate data from file representations to native, in-memory representations.
82Functions
83.Fn elf32_xlatetof ,
84.Fn elf64_xlatetof ,
85and
86.Fn gelf_xlatetof
87translate data from in-memory representations to file representations.
88.Pp
89Argument
90.Ar src
91denotes an
92.Vt Elf_Data
93descriptor describing the source to be translated.
94The following elements of the descriptor need to be set before
95invoking these functions:
96.Bl -hang -offset indent
97.It Va d_buf
98Set to a valid pointer value denoting the beginning of the data area
99to be translated.
100.It Va d_size
101Set to the total size in bytes of the source data area to be
102translated.
103.It Va d_type
104Set to the type of the source data being translated.
105This value is one of the values defined in the
106.Vt Elf_Type
107enumeration.
108The
109.Vt Elf_Type
110enumeration is described in
111.Xr elf 3 .
112.It Va d_version
113Set to the version number of the ELF data structures being
114translated.
115Currently only version
116.Dv EV_CURRENT
117is supported.
118.El
119.Pp
120Argument
121.Ar dst
122describes the destination buffer.
123The following elements of the
124.Vt Elf_Data
125descriptor need to be set before invoking these functions:
126.Bl -hang -offset indent
127.It Va d_buf
128Set to a valid pointer value that denotes the start of the destination
129buffer that will hold translated data.
130This value may be the same as that of the source buffer, in which case
131an in-place conversion will be attempted.
132.It Va d_size
133Set to the size of the destination buffer in bytes.
134This value will be modified if the function call succeeds.
135.It Va d_version
136Set to the desired version number of the destination.
137Currently only version
138.Dv EV_CURRENT
139is supported.
140.El
141.Pp
142These translations routines allow the source and destination buffers
143to coincide, in which case an in-place translation will be done
144if the destination is large enough to hold the translated data.
145Other kinds of overlap between the source and destination buffers
146are not permitted.
147.Pp
148On successful completion of the translation request the following
149fields of the
150.Ar dst
151descriptor would be modified:
152.Bl -hang -offset indent
153.It Va d_size
154Set to the size in bytes of the translated data.
155.It Va d_type
156Set to the
157.Va d_type
158value of the source data descriptor.
159.El
160.Pp
161Argument
162.Ar file_encoding
163specifies the encoding in which the file objects are represented.
164It must be one of:
165.Bl -hang -offset indent
166.It Dv ELFDATANONE
167File objects use the library's native byte ordering.
168.It Dv ELFDATA2LSB
169File objects use a little-endian ordering.
170.It Dv ELFDATA2MSB
171File objects use a big-endian ordering.
172.El
173.Pp
174The functions
175.Fn gelf_xlatetof
176and
177.Fn gelf_xlatetom
178select the appropriate translation scheme based on the properties of
179argument
180.Ar elf .
181.Sh RETURN VALUES
182These functions return argument
183.Ar dst
184if successful, or NULL in case of an error.
185.Sh EXAMPLES
186To translate a
187.Vt GElf_Rel
188structure to its LSB file representation use:
189.Bd -literal -offset indent
190Elf_Data dst, src;
191GElf_Rel rel;
192Elf *e;
193
194e = ...; /* See elf_begin(3). */
195
196/* Set up the 'src' descriptor. */
197memset(&src, 0, sizeof src);
198src.d_buf = &rel;
199src.d_size = sizeof(rel);
200src.d_type = ELF_T_REL;
201src.d_version = EV_CURRENT;
202
203/* Set up the 'dst' descriptor. */
204memset(&dst, 0, sizeof dst);
205dst.d_buf = filebuf;
206dst.d_size = gelf_fsize(e, ELF_T_REL, 1, EV_CURRENT);
207dst.d_version = EV_CURRENT;
208
209if (gelf_xlatetof(e, &dst, &src, ELFDATA2LSB) == NULL) {
210	printf("error: %s", elf_errmsg(0));
211}
212.Ed
213.Sh ERRORS
214These functions may fail with the following errors:
215.Bl -tag -width "[ELF_E_RESOURCE]"
216.It Bq Er ELF_E_ARGUMENT
217One of arguments
218.Ar src ,
219.Ar dst
220or
221.Ar elf
222was NULL.
223.It Bq Er ELF_E_ARGUMENT
224Arguments
225.Ar src
226and
227.Ar dst
228were equal.
229.It Bq Er ELF_E_ARGUMENT
230The desired encoding parameter was not one of
231.Dv ELFDATANONE ,
232.Dv ELFDATA2LSB
233or
234.Dv ELFDATA2MSB .
235.It Bq Er ELF_E_ARGUMENT
236The
237.Ar d_type
238field of argument
239.Ar src
240specified an unsupported type.
241.It Bq Er ELF_E_DATA
242The
243.Ar src
244argument specified a buffer size that was not an integral multiple of
245its underlying type.
246.It Bq Er ELF_E_DATA
247The
248.Ar dst
249argument specified a buffer size that was too small.
250.It Bq Er ELF_E_DATA
251Argument
252.Ar dst
253specified a destination buffer that overlaps with the source
254buffer.
255.It Bq Er ELF_E_DATA
256The destination buffer for a conversion to memory had an alignment
257inappropriate for the underlying ELF type.
258.It Bq Er ELF_E_DATA
259The source buffer for a conversion to file had an alignment
260inappropriate for the underlying ELF type.
261.It Bq Er ELF_E_UNIMPL
262The version numbers for arguments
263.Ar dst
264and
265.Ar src
266were not identical.
267.It Bq Er ELF_E_UNIMPL
268The argument
269.Ar src
270requested conversion for a type which is not currently
271supported.
272.It Bq Er ELF_E_VERSION
273Argument
274.Ar src
275specified an unsupported version number.
276.El
277.Sh SEE ALSO
278.Xr elf 3 ,
279.Xr elf_getdata 3 ,
280.Xr gelf 3
281