diff options
Diffstat (limited to 'libelf')
59 files changed, 329 insertions, 204 deletions
diff --git a/libelf/Makefile b/libelf/Makefile index 5d4beaa08e44..28524d249d8a 100644 --- a/libelf/Makefile +++ b/libelf/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile 3594 2018-04-11 18:26:50Z jkoshy $ +# $Id: Makefile 3632 2018-10-10 21:12:43Z jkoshy $ TOP= .. @@ -51,6 +51,7 @@ SRCS= elf.c \ libelf_checksum.c \ libelf_data.c \ libelf_ehdr.c \ + libelf_elfmachine.c \ libelf_extended.c \ libelf_memory.c \ libelf_open.c \ diff --git a/libelf/_libelf.h b/libelf/_libelf.h index be45335907f3..26f17276b170 100644 --- a/libelf/_libelf.h +++ b/libelf/_libelf.h @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _libelf.h 3174 2015-03-27 17:13:41Z emaste $ + * $Id: _libelf.h 3632 2018-10-10 21:12:43Z jkoshy $ */ #ifndef __LIBELF_H_ @@ -194,6 +194,9 @@ enum { * Function Prototypes. */ +typedef int _libelf_translator_function(unsigned char *_dst, size_t dsz, + unsigned char *_src, size_t _cnt, int _byteswap); + #ifdef __cplusplus extern "C" { #endif @@ -207,12 +210,12 @@ Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst); Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst); long _libelf_checksum(Elf *_e, int _elfclass); void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate); +int _libelf_elfmachine(Elf *_e); unsigned int _libelf_falign(Elf_Type _t, int _elfclass); size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version, size_t count); -int (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass)) - (unsigned char *_dst, size_t dsz, unsigned char *_src, - size_t _cnt, int _byteswap); +_libelf_translator_function *_libelf_get_translator(Elf_Type _t, + int _direction, int _elfclass, int _elfmachine); void *_libelf_getphdr(Elf *_e, int _elfclass); void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass); void _libelf_init_elf(Elf *_e, Elf_Kind _kind); @@ -230,7 +233,7 @@ int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum); int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass, size_t _shstrndx); Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s, - unsigned int _encoding, int _elfclass, int _direction); + unsigned int _encoding, int _elfclass, int _elfmachine, int _direction); int _libelf_xlate_shtype(uint32_t _sht); #ifdef __cplusplus } diff --git a/libelf/elf.3 b/libelf/elf.3 index 20cb9445535d..269725a09ac8 100644 --- a/libelf/elf.3 +++ b/libelf/elf.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf.3 3195 2015-05-12 17:22:19Z emaste $ +.\" $Id: elf.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" -.Dd July 28, 2014 -.Os +.Dd October 10, 2018 .Dt ELF 3 +.Os .Sh NAME .Nm elf .Nd API for manipulating ELF objects @@ -116,9 +116,12 @@ library offers translation facilities .Xr elf64_xlatetof 3 and .Xr elf64_xlatetom 3 ) -to and from these -representations and also provides higher-level APIs that retrieve and store -data from the ELF object in a transparent manner. +to and from these representations. +It also provides higher-level APIs +.Xr ( gelf_xlatetof 3 , +.Xr gelf_xlatetom 3 ) +that retrieve and store data from the ELF object in a class-agnostic +manner. .Ss Library Working Version Conceptually, there are three version numbers associated with an application using the ELF library to manipulate ELF objects: @@ -155,7 +158,7 @@ Used for class-independent data types. .It Dv ELF_C_ Used for command values used in a few functions. These symbols are defined as members of the -.Vt Dv Elf_Cmd +.Vt Elf_Cmd enumeration. .It Dv ELF_E_ Used for error numbers. @@ -237,7 +240,6 @@ The .Vt Elf structure is opaque and has no members visible to the application. -.\" TODO describe the Elf_Arhdr and Elf_Arsym structures. .It Vt Elf_Data An .Vt Elf_Data @@ -409,7 +411,7 @@ Same as .El .Pp Section types in the range -.Ns [ Dv SHT_LOOS , +.Dv [ SHT_LOOS , .Dv SHT_HIUSER ] are otherwise considered to be of type .Dv ELF_T_BYTE . @@ -419,7 +421,7 @@ in the ELF library. Each function listed here is described further in its own manual page. .Bl -tag -width indent .It "Archive Access" -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn elf_getarsym Retrieve the archive symbol table. .It Fn elf_getarhdr @@ -436,7 +438,7 @@ Random access inside an archive. .El .It "Data Structures" -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn elf_getdata Retrieve translated data for an ELF section. .It Fn elf_getscn @@ -469,7 +471,7 @@ Allocate an Executable Header in an ELF object. Allocate or resize the Program Header Table in an ELF object. .El .It "Data Translation" -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn elf32_xlatetof , Fn elf64_xlatetof Translate an ELF data structure from its native representation to its file representation. @@ -478,14 +480,14 @@ Translate an ELF data structure from its file representation to a native representation. .El .It "Error Reporting" -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn elf_errno Retrieve the current error. .It Fn elf_errmsg Retrieve a human readable description of the current error. .El .It "Initialization" -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn elf_begin Opens an .Xr ar 1 @@ -529,9 +531,11 @@ back to the underlying file. Compute checksum of an ELF object. .It Fn elf_getident Retrieve the identification bytes for an ELF object. -.It Fn elf_getshnum +.It Fn elf_getphdrnum +Retrieve the number of program headers in an ELF object. +.It Fn elf_getshdrnum Retrieve the number of sections in an ELF object. -.It Fn elf_getshstrndx +.It Fn elf_getshdrstrndx Retrieve the section index of the section name string table in an ELF object. .It Fn elf_hash @@ -601,6 +605,7 @@ library will reclaim the space used by the descriptor itself. .Sh SEE ALSO .Xr gelf 3 , +.Xr ar 5 , .Xr elf 5 .Sh HISTORY The original ELF(3) API was developed for Unix System V. diff --git a/libelf/elf_begin.3 b/libelf/elf_begin.3 index e6c11b60e865..b7d8a9ec88b0 100644 --- a/libelf/elf_begin.3 +++ b/libelf/elf_begin.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_begin.3 3182 2015-04-10 16:08:10Z emaste $ +.\" $Id: elf_begin.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd December 11, 2011 -.Os .Dt ELF_BEGIN 3 +.Os .Sh NAME .Nm elf_begin .Nd open an ELF file or ar(1) archive diff --git a/libelf/elf_cntl.3 b/libelf/elf_cntl.3 index 257897590db2..59d375dad07d 100644 --- a/libelf/elf_cntl.3 +++ b/libelf/elf_cntl.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_cntl.3 3181 2015-04-10 13:22:51Z emaste $ +.\" $Id: elf_cntl.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 9, 2006 -.Os .Dt ELF_CNTL 3 +.Os .Sh NAME .Nm elf_cntl .Nd control an elf file descriptor diff --git a/libelf/elf_data.c b/libelf/elf_data.c index d494eda2de3f..bab70d003cee 100644 --- a/libelf/elf_data.c +++ b/libelf/elf_data.c @@ -32,7 +32,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: elf_data.c 3466 2016-05-11 18:35:44Z emaste $"); +ELFTC_VCSID("$Id: elf_data.c 3632 2018-10-10 21:12:43Z jkoshy $"); Elf_Data * elf_getdata(Elf_Scn *s, Elf_Data *ed) @@ -43,8 +43,7 @@ elf_getdata(Elf_Scn *s, Elf_Data *ed) size_t count, fsz, msz; struct _Libelf_Data *d; uint64_t sh_align, sh_offset, sh_size; - int (*xlate)(unsigned char *_d, size_t _dsz, unsigned char *_s, - size_t _c, int _swap); + _libelf_translator_function *xlate; d = (struct _Libelf_Data *) ed; @@ -151,7 +150,8 @@ elf_getdata(Elf_Scn *s, Elf_Data *ed) d->d_flags |= LIBELF_F_DATA_MALLOCED; - xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass); + xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass, + _libelf_elfmachine(e)); if (!(*xlate)(d->d_data.d_buf, (size_t) d->d_data.d_size, e->e_rawfile + sh_offset, count, e->e_byteorder != LIBELF_PRIVATE(byteorder))) { diff --git a/libelf/elf_end.3 b/libelf/elf_end.3 index 8649faaa696d..fab4df34eefb 100644 --- a/libelf/elf_end.3 +++ b/libelf/elf_end.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_end.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_end.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd June 29, 2006 -.Os .Dt ELF_END 3 +.Os .Sh NAME .Nm elf_end .Nd release an ELF descriptor diff --git a/libelf/elf_errmsg.3 b/libelf/elf_errmsg.3 index 822ba6ac894f..ec2561e42c80 100644 --- a/libelf/elf_errmsg.3 +++ b/libelf/elf_errmsg.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_errmsg.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_errmsg.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd June 11, 2006 -.Os .Dt ELF_ERRMSG 3 +.Os .Sh NAME .Nm elf_errmsg , .Nm elf_errno diff --git a/libelf/elf_fill.3 b/libelf/elf_fill.3 index ab42a4245f77..2b78d5907cf4 100644 --- a/libelf/elf_fill.3 +++ b/libelf/elf_fill.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_fill.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_fill.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd June 11, 2006 -.Os .Dt ELF_FILL 3 +.Os .Sh NAME .Nm elf_fill .Nd set fill byte for inter-section padding diff --git a/libelf/elf_flagdata.3 b/libelf/elf_flagdata.3 index de3cfd973160..c210e4a56971 100644 --- a/libelf/elf_flagdata.3 +++ b/libelf/elf_flagdata.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_flagdata.3 3479 2016-06-25 20:44:33Z jkoshy $ +.\" $Id: elf_flagdata.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd December 3, 2011 -.Os .Dt ELF_FLAGDATA 3 +.Os .Sh NAME .Nm elf_flagarhdr , .Nm elf_flagdata , diff --git a/libelf/elf_getarhdr.3 b/libelf/elf_getarhdr.3 index 1aab71ce4306..7af2e726c75c 100644 --- a/libelf/elf_getarhdr.3 +++ b/libelf/elf_getarhdr.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getarhdr.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_getarhdr.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 15, 2006 -.Os .Dt ELF_GETARHDR 3 +.Os .Sh NAME .Nm elf_getarhdr .Nd retrieve ar(1) header for an archive member diff --git a/libelf/elf_getarsym.3 b/libelf/elf_getarsym.3 index cda0511dfd7a..c42aab4ffb6f 100644 --- a/libelf/elf_getarsym.3 +++ b/libelf/elf_getarsym.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getarsym.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_getarsym.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 15, 2006 -.Os .Dt ELF_GETARSYM 3 +.Os .Sh NAME .Nm elf_getarsym .Nd retrieve the symbol table of an archive diff --git a/libelf/elf_getbase.3 b/libelf/elf_getbase.3 index fa1735394173..6319b8ad9d1a 100644 --- a/libelf/elf_getbase.3 +++ b/libelf/elf_getbase.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getbase.3 978 2010-06-06 12:40:19Z jkoshy $ +.\" $Id: elf_getbase.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd June 6, 2010 -.Os .Dt ELF_GETBASE 3 +.Os .Sh NAME .Nm elf_getbase .Nd get the base offset for an object file diff --git a/libelf/elf_getdata.3 b/libelf/elf_getdata.3 index 68b28890b4d6..b287a25a00bc 100644 --- a/libelf/elf_getdata.3 +++ b/libelf/elf_getdata.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getdata.3 3181 2015-04-10 13:22:51Z emaste $ +.\" $Id: elf_getdata.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd January 26, 2011 -.Os .Dt ELF_GETDATA 3 +.Os .Sh NAME .Nm elf_getdata , .Nm elf_newdata , @@ -144,7 +144,7 @@ structures of type .Dv ELF_T_BYTE . .Ss Special handling of zero-sized and SHT_NOBITS sections For sections of type -.Dv SHT_NOBITS, +.Dv SHT_NOBITS , and for zero-sized sections, the functions .Fn elf_getdata diff --git a/libelf/elf_getident.3 b/libelf/elf_getident.3 index 01d7f9733ade..200fa4e350fb 100644 --- a/libelf/elf_getident.3 +++ b/libelf/elf_getident.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getident.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_getident.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd July 3, 2006 -.Os .Dt ELF_GETIDENT 3 +.Os .Sh NAME .Nm elf_getident .Nd return the initial bytes of a file diff --git a/libelf/elf_getphdrnum.3 b/libelf/elf_getphdrnum.3 index f0fae5ebb01e..d20a457e408e 100644 --- a/libelf/elf_getphdrnum.3 +++ b/libelf/elf_getphdrnum.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getphdrnum.3 467 2009-08-05 18:18:49Z jkoshy $ +.\" $Id: elf_getphdrnum.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" -.Dd August 5, 2009 -.Os +.Dd July 25, 2018 .Dt ELF_GETPHDRNUM 3 +.Os .Sh NAME .Nm elf_getphdrnum .Nd return the number of program headers in an ELF file @@ -45,14 +45,13 @@ and stores it into the location pointed to by argument .Pp This routine allows applications to uniformly process both normal ELF objects and ELF objects that use extended numbering. -.Pp .Sh RETURN VALUES Function .Fn elf_getphdrnum returns a zero value if successful, or -1 in case of an error. .Sh ERRORS Function -.Fn elf_getphnum +.Fn elf_getphdrnum can fail with the following errors: .Bl -tag -width "[ELF_E_RESOURCE]" .It Bq Er ELF_E_ARGUMENT diff --git a/libelf/elf_getphnum.3 b/libelf/elf_getphnum.3 index 95c75403c35e..77c595730890 100644 --- a/libelf/elf_getphnum.3 +++ b/libelf/elf_getphnum.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getphnum.3 467 2009-08-05 18:18:49Z jkoshy $ +.\" $Id: elf_getphnum.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd August 5, 2009 -.Os .Dt ELF_GETPHNUM 3 +.Os .Sh NAME .Nm elf_getphnum .Nd return the number of program headers in an ELF file @@ -50,7 +50,6 @@ and stores it into the location pointed to by argument .Pp This routine allows applications to uniformly process both normal ELF objects and ELF objects that use extended numbering. -.Pp .Sh RETURN VALUES Function .Fn elf_getphnum diff --git a/libelf/elf_getscn.3 b/libelf/elf_getscn.3 index 0afe44340cc0..ad3c88b82b2a 100644 --- a/libelf/elf_getscn.3 +++ b/libelf/elf_getscn.3 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2006-2008 Joseph Koshy. All rights reserved. +.\" Copyright (c) 2006-2008,2018 Joseph Koshy. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getscn.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_getscn.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" -.Dd October 22, 2007 -.Os +.Dd September 24, 2018 .Dt ELF_GETSCN 3 +.Os .Sh NAME .Nm elf_getscn , .Nm elf_ndxscn , @@ -86,6 +86,10 @@ takes a section descriptor .Ar scn and returns a pointer to the section descriptor at the next higher index. +As a consequence, +.Fn elf_nextscn +will never return a pointer to the empty section at index zero +.Dv ( SHN_UNDEF ) . Argument .Ar scn is allowed to be NULL, in which case this function will return a diff --git a/libelf/elf_getshdrnum.3 b/libelf/elf_getshdrnum.3 index e2bf354baebd..744fcf2dbf6c 100644 --- a/libelf/elf_getshdrnum.3 +++ b/libelf/elf_getshdrnum.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getshdrnum.3 467 2009-08-05 18:18:49Z jkoshy $ +.\" $Id: elf_getshdrnum.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd August 4, 2009 -.Os .Dt ELF_GETSHDRNUM 3 +.Os .Sh NAME .Nm elf_getshdrnum .Nd return the number of sections in an ELF file @@ -45,7 +45,6 @@ and stores it into the location pointed to by argument .Pp This routine allows applications to uniformly process both normal ELF objects, and ELF objects that use extended section numbering. -.Pp .Sh RETURN VALUES Function .Fn elf_getshdrnum diff --git a/libelf/elf_getshdrstrndx.3 b/libelf/elf_getshdrstrndx.3 index b02e71582403..e6c4eb774f26 100644 --- a/libelf/elf_getshdrstrndx.3 +++ b/libelf/elf_getshdrstrndx.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getshdrstrndx.3 467 2009-08-05 18:18:49Z jkoshy $ +.\" $Id: elf_getshdrstrndx.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd August 5, 2009 -.Os .Dt ELF_GETSHDRSTRNDX 3 +.Os .Sh NAME .Nm elf_getshdrstrndx .Nd retrieve the index of the section name string table @@ -46,7 +46,6 @@ and stores it into the location pointed to by argument .Pp This function allow applications to process both normal ELF objects and ELF objects that use extended section numbering uniformly. -.Pp .Sh RETURN VALUES These functions return zero if successful, or -1 in case of an error. .Sh ERRORS diff --git a/libelf/elf_getshnum.3 b/libelf/elf_getshnum.3 index 615aa71faa84..20be5c546773 100644 --- a/libelf/elf_getshnum.3 +++ b/libelf/elf_getshnum.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getshnum.3 467 2009-08-05 18:18:49Z jkoshy $ +.\" $Id: elf_getshnum.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd August 5, 2009 -.Os .Dt ELF_GETSHNUM 3 +.Os .Sh NAME .Nm elf_getshnum .Nd return the number of sections in an ELF file @@ -50,7 +50,6 @@ and stores it into the location pointed to by argument .Pp This routine allows applications to uniformly process both normal ELF objects, and ELF objects that use extended section numbering. -.Pp .Sh RETURN VALUES Function .Fn elf_getshnum diff --git a/libelf/elf_getshstrndx.3 b/libelf/elf_getshstrndx.3 index 71c6f9518c93..b03820f9c02c 100644 --- a/libelf/elf_getshstrndx.3 +++ b/libelf/elf_getshstrndx.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getshstrndx.3 467 2009-08-05 18:18:49Z jkoshy $ +.\" $Id: elf_getshstrndx.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" -.Dd August 5, 2009 -.Os +.Dd July 25, 2018 .Dt ELF_GETSHSTRNDX 3 +.Os .Sh NAME .Nm elf_getshstrndx , .Nm elf_setshstrndx @@ -41,17 +41,18 @@ .Sh DESCRIPTION Function .Fn elf_getshstrndx +is deprecated. +Please use +.Xr elf_getshdrstrndx 3 +instead. +.Pp +Function +.Fn elf_getshstrndx retrieves the section index of the string table containing section names from descriptor .Ar elf and stores it into the location pointed to by argument .Ar ndxptr . -Function -.Fn elf_getshstrndx -is deprecated. -Please use -.Xr elf_getshdrstrndx 3 -instead. .Pp Function .Fn elf_setshstrndx @@ -60,7 +61,6 @@ sets the index of the section name string table to argument .Pp These routines allow applications to process both normal ELF objects and ELF objects that use extended section numbering uniformly. -.Pp .Sh RETURN VALUES These functions return a non-zero value if successful, or zero in case of an error. diff --git a/libelf/elf_hash.3 b/libelf/elf_hash.3 index f099558570fd..a772a1a52ac1 100644 --- a/libelf/elf_hash.3 +++ b/libelf/elf_hash.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_hash.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_hash.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 15, 2006 -.Os .Dt ELF_HASH 3 +.Os .Sh NAME .Nm elf_hash .Nd compute a hash value for a string diff --git a/libelf/elf_kind.3 b/libelf/elf_kind.3 index a5bbf9df4554..e7f5b9d232a0 100644 --- a/libelf/elf_kind.3 +++ b/libelf/elf_kind.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_kind.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_kind.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd June 1, 2006 -.Os .Dt ELF_KIND 3 +.Os .Sh NAME .Nm elf_kind .Nd determine ELF file type diff --git a/libelf/elf_memory.3 b/libelf/elf_memory.3 index bcb888ff8d8b..f7b0e7f2270e 100644 --- a/libelf/elf_memory.3 +++ b/libelf/elf_memory.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_memory.3 2314 2011-12-11 06:19:51Z jkoshy $ +.\" $Id: elf_memory.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd June 28, 2006 -.Os .Dt ELF_MEMORY 3 +.Os .Sh NAME .Nm elf_memory .Nd process an ELF or ar(1) archive mapped into memory diff --git a/libelf/elf_next.3 b/libelf/elf_next.3 index 859d06cccda9..0c2d92d67e2d 100644 --- a/libelf/elf_next.3 +++ b/libelf/elf_next.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_next.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_next.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd June 17, 2006 -.Os .Dt ELF_NEXT 3 +.Os .Sh NAME .Nm elf_next .Nd provide sequential access to the next archive member diff --git a/libelf/elf_open.3 b/libelf/elf_open.3 index 3dd1e36a03f4..acfbe8bc1cfd 100644 --- a/libelf/elf_open.3 +++ b/libelf/elf_open.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_open.3 3181 2015-04-10 13:22:51Z emaste $ +.\" $Id: elf_open.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd May 31, 2012 -.Os .Dt ELF_OPEN 3 +.Os .Sh NAME .Nm elf_open .Nd open ELF objects and ar(1) archives @@ -67,6 +67,9 @@ archive contained in the memory area pointed to by the argument The argument .Ar sz specifies the size of the memory area in bytes. +.Sh RETURN VALUES +The function returns a pointer to a ELF descriptor if successful, or +NULL if an error occurred. .Sh COMPATIBILITY These functions are non-standard extensions to the ELF(3) API set. .Pp @@ -79,9 +82,6 @@ and .Xr ar 1 archives, returning an Elf descriptor of type .Dv ELF_K_NONE . -.Sh RETURN VALUES -The function returns a pointer to a ELF descriptor if successful, or -NULL if an error occurred. .Sh ERRORS These functions can fail with the following errors: .Bl -tag -width "[ELF_E_RESOURCE]" diff --git a/libelf/elf_rand.3 b/libelf/elf_rand.3 index e5affd6a3f7d..1a4696073f09 100644 --- a/libelf/elf_rand.3 +++ b/libelf/elf_rand.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_rand.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_rand.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd June 17, 2006 -.Os .Dt ELF_RAND 3 +.Os .Sh NAME .Nm elf_rand .Nd provide sequential access to the next archive member diff --git a/libelf/elf_rawfile.3 b/libelf/elf_rawfile.3 index a713b4295305..355301cf6c12 100644 --- a/libelf/elf_rawfile.3 +++ b/libelf/elf_rawfile.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_rawfile.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: elf_rawfile.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd July 3, 2006 -.Os .Dt ELF_RAWFILE 3 +.Os .Sh NAME .Nm elf_rawfile .Nd return uninterpreted contents of an ELF file diff --git a/libelf/elf_scn.c b/libelf/elf_scn.c index d3e274715b78..b72fef66c46b 100644 --- a/libelf/elf_scn.c +++ b/libelf/elf_scn.c @@ -36,7 +36,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: elf_scn.c 3177 2015-03-30 18:19:41Z emaste $"); +ELFTC_VCSID("$Id: elf_scn.c 3632 2018-10-10 21:12:43Z jkoshy $"); /* * Load an ELF section table and create a list of Elf_Scn structures. @@ -51,8 +51,7 @@ _libelf_load_section_headers(Elf *e, void *ehdr) int ec, swapbytes; unsigned char *src; size_t fsz, i, shnum; - int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, - size_t _c, int _swap); + _libelf_translator_function *xlator; assert(e != NULL); assert(ehdr != NULL); @@ -84,7 +83,8 @@ _libelf_load_section_headers(Elf *e, void *ehdr) CHECK_EHDR(e, eh64); } - xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec); + xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec, + _libelf_elfmachine(e)); swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder); src = e->e_rawfile + shoff; diff --git a/libelf/elf_strptr.3 b/libelf/elf_strptr.3 index 31e0f830a0af..8fb9118ebada 100644 --- a/libelf/elf_strptr.3 +++ b/libelf/elf_strptr.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_strptr.3 1081 2010-08-14 02:23:48Z jkoshy $ +.\" $Id: elf_strptr.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd December 16, 2006 -.Os .Dt ELF_STRPTR 3 +.Os .Sh NAME .Nm elf_strptr .Nd retrieve a string pointer in a string table diff --git a/libelf/elf_update.3 b/libelf/elf_update.3 index 40a1e40c2d50..b6419cf9bf53 100644 --- a/libelf/elf_update.3 +++ b/libelf/elf_update.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_update.3 1729 2011-08-14 09:13:00Z jkoshy $ +.\" $Id: elf_update.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 14, 2011 -.Os .Dt ELF_UPDATE 3 +.Os .Sh NAME .Nm elf_update .Nd update an ELF descriptor diff --git a/libelf/elf_update.c b/libelf/elf_update.c index 6cc109daafcc..25fa98d0bb12 100644 --- a/libelf/elf_update.c +++ b/libelf/elf_update.c @@ -41,7 +41,7 @@ #include <sys/mman.h> #endif -ELFTC_VCSID("$Id: elf_update.c 3190 2015-05-04 15:23:08Z jkoshy $"); +ELFTC_VCSID("$Id: elf_update.c 3632 2018-10-10 21:12:43Z jkoshy $"); /* * Layout strategy: @@ -712,8 +712,8 @@ _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) static off_t _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) { - int ec; off_t rc; + int ec, em; Elf_Scn *s; int elftype; Elf_Data *d, dst; @@ -747,6 +747,9 @@ _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) sh_off = s->s_offset; assert(sh_off % _libelf_falign(elftype, ec) == 0); + em = _libelf_elfmachine(e); + assert(em >= EM_NONE && em < EM__LAST__); + /* * If the section has a `rawdata' descriptor, and the section * contents have not been modified, use its contents directly. @@ -817,8 +820,8 @@ _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) dst.d_buf = nf + rc; dst.d_size = fsz; - if (_libelf_xlate(&dst, d, e->e_byteorder, ec, ELF_TOFILE) == - NULL) + if (_libelf_xlate(&dst, d, e->e_byteorder, ec, em, ELF_TOFILE) + == NULL) return ((off_t) -1); rc += (off_t) fsz; @@ -834,7 +837,7 @@ _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) static off_t _libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) { - int ec; + int ec, em; void *ehdr; size_t fsz, msz; Elf_Data dst, src; @@ -850,6 +853,8 @@ _libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1); msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version); + em = _libelf_elfmachine(e); + (void) memset(&dst, 0, sizeof(dst)); (void) memset(&src, 0, sizeof(src)); @@ -861,7 +866,7 @@ _libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) dst.d_buf = nf; dst.d_size = fsz; - if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == + if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, em, ELF_TOFILE) == NULL) return ((off_t) -1); @@ -875,7 +880,7 @@ _libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) static off_t _libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) { - int ec; + int ec, em; void *ehdr; Elf32_Ehdr *eh32; Elf64_Ehdr *eh64; @@ -899,6 +904,8 @@ _libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) phoff = eh64->e_phoff; } + em = _libelf_elfmachine(e); + assert(phoff > 0); assert(ex->ex_start == phoff); assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0); @@ -918,7 +925,7 @@ _libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) dst.d_size = fsz; dst.d_buf = nf + ex->ex_start; - if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == + if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, em, ELF_TOFILE) == NULL) return ((off_t) -1); @@ -932,7 +939,7 @@ _libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) static off_t _libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) { - int ec; + int ec, em; void *ehdr; Elf_Scn *scn; uint64_t shoff; @@ -955,6 +962,8 @@ _libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) shoff = eh64->e_shoff; } + em = _libelf_elfmachine(e); + assert(nscn > 0); assert(shoff % _libelf_falign(ELF_T_SHDR, ec) == 0); assert(ex->ex_start == shoff); @@ -977,8 +986,8 @@ _libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) dst.d_size = fsz; dst.d_buf = nf + ex->ex_start + scn->s_ndx * fsz; - if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, - ELF_TOFILE) == NULL) + if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, em, + ELF_TOFILE) == NULL) return ((off_t) -1); } diff --git a/libelf/elf_version.3 b/libelf/elf_version.3 index b09fb479cfae..b679aa7aaa03 100644 --- a/libelf/elf_version.3 +++ b/libelf/elf_version.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_version.3 2123 2011-11-09 15:40:09Z jkoshy $ +.\" $Id: elf_version.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd November 9, 2011 -.Os .Dt ELF_VERSION 3 +.Os .Sh NAME .Nm elf_version .Nd retrieve or set ELF library operating version diff --git a/libelf/gelf.3 b/libelf/gelf.3 index d00d5b330980..704ae154700f 100644 --- a/libelf/gelf.3 +++ b/libelf/gelf.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf.3 3195 2015-05-12 17:22:19Z emaste $ +.\" $Id: gelf.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd September 1, 2006 -.Os .Dt GELF 3 +.Os .Sh NAME .Nm GElf .Nd class-independent API for ELF manipulation @@ -115,7 +115,7 @@ routines will signal an error if a GElf value is out of range for the underlying ELF data type. .Ss Namespace use The GElf interface uses the following symbols: -.Bl -tag +.Bl -tag -width indent .It GElf_* Class-independent data types. .It gelf_* @@ -125,16 +125,16 @@ For functions defined in the API set. This section provides an overview of the GElf programming APIs. Further information is provided in the manual page of each function listed here. -.Bl -tag +.Bl -tag -width indent .It "Allocating ELF Data Structures" -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn gelf_newehdr Allocate a new ELF Executable Header. .It Fn gelf_newphdr Allocate a new ELF Program Header Table. .El .It "Data Translation" -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn gelf_xlatetof Translate the native representation of an ELF data structure to its file representation. @@ -143,7 +143,7 @@ Translate from the file representation of an ELF data structure to a native representation. .El .It "Retrieving ELF Data" -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn gelf_getdyn Retrieve an ELF .Sy .dynamic @@ -162,7 +162,7 @@ Retrieve an ELF Section Header Table entry from the underlying ELF descriptor. Retrieve an ELF symbol table entry. .El .It Queries -.Bl -tag -compact +.Bl -tag -compact -width indent .It Fn gelf_checksum Retrieves the ELF checksum for an ELF descriptor. .It Fn gelf_fsize diff --git a/libelf/gelf_checksum.3 b/libelf/gelf_checksum.3 index e5f845f6c922..7972cb90cd55 100644 --- a/libelf/gelf_checksum.3 +++ b/libelf/gelf_checksum.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_checksum.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_checksum.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 29, 2006 -.Os .Dt GELF_CHECKSUM 3 +.Os .Sh NAME .Nm elf32_checksum , .Nm elf64_checksum , diff --git a/libelf/gelf_fsize.3 b/libelf/gelf_fsize.3 index ac7996f9296b..7103d69ba1c8 100644 --- a/libelf/gelf_fsize.3 +++ b/libelf/gelf_fsize.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_fsize.3 317 2009-03-06 17:29:22Z jkoshy $ +.\" $Id: gelf_fsize.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd February 5, 2008 -.Os .Dt GELF_FSIZE 3 +.Os .Sh NAME .Nm gelf_fsize , .Nm elf32_fsize , diff --git a/libelf/gelf_getcap.3 b/libelf/gelf_getcap.3 index ed8eb0275825..276933a8365a 100644 --- a/libelf/gelf_getcap.3 +++ b/libelf/gelf_getcap.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getcap.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getcap.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 29, 2006 -.Os .Dt GELF_GETCAP 3 +.Os .Sh NAME .Nm gelf_getcap , .Nm gelf_update_cap diff --git a/libelf/gelf_getclass.3 b/libelf/gelf_getclass.3 index 3504569fd141..f3d204995cd4 100644 --- a/libelf/gelf_getclass.3 +++ b/libelf/gelf_getclass.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getclass.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getclass.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd July 3, 2006 -.Os .Dt GELF_GETCLASS 3 +.Os .Sh NAME .Nm gelf_getclass .Nd retrieve the class of an ELF descriptor diff --git a/libelf/gelf_getdyn.3 b/libelf/gelf_getdyn.3 index f8c177876d07..a4e51ecd0ee2 100644 --- a/libelf/gelf_getdyn.3 +++ b/libelf/gelf_getdyn.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getdyn.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getdyn.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 29, 2006 -.Os .Dt GELF_GETDYN 3 +.Os .Sh NAME .Nm gelf_getdyn , .Nm gelf_update_dyn diff --git a/libelf/gelf_getehdr.3 b/libelf/gelf_getehdr.3 index 56bdcd405646..ce27ea7f0006 100644 --- a/libelf/gelf_getehdr.3 +++ b/libelf/gelf_getehdr.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getehdr.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getehdr.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd December 16, 2006 -.Os .Dt GELF_GETEHDR 3 +.Os .Sh NAME .Nm elf32_getehdr , .Nm elf64_getehdr , diff --git a/libelf/gelf_getmove.3 b/libelf/gelf_getmove.3 index 871a040ad447..64c52d9682ed 100644 --- a/libelf/gelf_getmove.3 +++ b/libelf/gelf_getmove.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getmove.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getmove.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 29, 2006 -.Os .Dt GELF_GETMOVE 3 +.Os .Sh NAME .Nm gelf_getmove , .Nm gelf_update_move diff --git a/libelf/gelf_getphdr.3 b/libelf/gelf_getphdr.3 index f2d38aa8316b..5ccac4aa6ff2 100644 --- a/libelf/gelf_getphdr.3 +++ b/libelf/gelf_getphdr.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getphdr.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getphdr.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd October 21, 2007 -.Os .Dt GELF_GETPHDR 3 +.Os .Sh NAME .Nm elf32_getphdr , .Nm elf64_getphdr , @@ -66,7 +66,7 @@ Function will retrieve the program header table entry at index .Ar index from ELF descriptor -.Ar elf. +.Ar elf . The translated program header table entry will be written to the address pointed to be argument .Ar dst . diff --git a/libelf/gelf_getrel.3 b/libelf/gelf_getrel.3 index c7566e64e697..c21be0da8b46 100644 --- a/libelf/gelf_getrel.3 +++ b/libelf/gelf_getrel.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getrel.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getrel.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 29, 2006 -.Os .Dt GELF_GETREL 3 +.Os .Sh NAME .Nm gelf_getrel , .Nm gelf_update_rel diff --git a/libelf/gelf_getrela.3 b/libelf/gelf_getrela.3 index c77d52a4021d..946482e605ef 100644 --- a/libelf/gelf_getrela.3 +++ b/libelf/gelf_getrela.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getrela.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getrela.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 29, 2006 -.Os .Dt GELF_GETRELA 3 +.Os .Sh NAME .Nm gelf_getrela , .Nm gelf_update_rela diff --git a/libelf/gelf_getshdr.3 b/libelf/gelf_getshdr.3 index e92d414371d1..8bb06d6be57e 100644 --- a/libelf/gelf_getshdr.3 +++ b/libelf/gelf_getshdr.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getshdr.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getshdr.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 27, 2006 -.Os .Dt GELF_GETSHDR 3 +.Os .Sh NAME .Nm elf32_getshdr , .Nm elf64_getshdr , diff --git a/libelf/gelf_getsym.3 b/libelf/gelf_getsym.3 index 98d886f88223..a139c152d15d 100644 --- a/libelf/gelf_getsym.3 +++ b/libelf/gelf_getsym.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getsym.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getsym.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 29, 2006 -.Os .Dt GELF_GETSYM 3 +.Os .Sh NAME .Nm gelf_getsym , .Nm gelf_update_sym diff --git a/libelf/gelf_getsyminfo.3 b/libelf/gelf_getsyminfo.3 index a1169f80dc08..0f602c289795 100644 --- a/libelf/gelf_getsyminfo.3 +++ b/libelf/gelf_getsyminfo.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getsyminfo.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getsyminfo.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 29, 2006 -.Os .Dt GELF_GETSYMINFO 3 +.Os .Sh NAME .Nm gelf_getsyminfo , .Nm gelf_update_syminfo diff --git a/libelf/gelf_getsymshndx.3 b/libelf/gelf_getsymshndx.3 index b635aac6d514..595472a429e3 100644 --- a/libelf/gelf_getsymshndx.3 +++ b/libelf/gelf_getsymshndx.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_getsymshndx.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_getsymshndx.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd November 5, 2006 -.Os .Dt GELF_GETSYMSHNDX 3 +.Os .Sh NAME .Nm gelf_getsymshndx , .Nm gelf_update_symshndx diff --git a/libelf/gelf_newehdr.3 b/libelf/gelf_newehdr.3 index cba9de0c8fe5..38698c32e8b0 100644 --- a/libelf/gelf_newehdr.3 +++ b/libelf/gelf_newehdr.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_newehdr.3 3500 2016-12-04 11:08:44Z jkoshy $ +.\" $Id: gelf_newehdr.3 3643 2018-10-14 21:09:24Z jkoshy $ .\" .Dd October 22, 2007 -.Os .Dt GELF_NEWEHDR 3 +.Os .Sh NAME .Nm elf32_newehdr , .Nm elf64_newehdr , @@ -58,7 +58,8 @@ structure. Function .Fn elf64_newehdr returns a pointer to a 64 bit -.Vt Elf64_Ehdr structure. +.Vt Elf64_Ehdr +structure. .Pp When argument .Ar elfclass diff --git a/libelf/gelf_newphdr.3 b/libelf/gelf_newphdr.3 index 27e561d01052..a881aab4dd6e 100644 --- a/libelf/gelf_newphdr.3 +++ b/libelf/gelf_newphdr.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_newphdr.3 3500 2016-12-04 11:08:44Z jkoshy $ +.\" $Id: gelf_newphdr.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd October 22, 2007 -.Os .Dt GELF_NEWPHDR 3 +.Os .Sh NAME .Nm elf32_newphdr , .Nm elf64_newphdr , diff --git a/libelf/gelf_update_ehdr.3 b/libelf/gelf_update_ehdr.3 index f5e041d797b8..02cf51a2751e 100644 --- a/libelf/gelf_update_ehdr.3 +++ b/libelf/gelf_update_ehdr.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_update_ehdr.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_update_ehdr.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" .Dd August 27, 2006 -.Os .Dt GELF_UPDATE_EHDR 3 +.Os .Sh NAME .Nm gelf_update_ehdr , .Nm gelf_update_phdr , diff --git a/libelf/gelf_xlate.c b/libelf/gelf_xlate.c index f50181925f21..11618a4537af 100644 --- a/libelf/gelf_xlate.c +++ b/libelf/gelf_xlate.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2006,2008 Joseph Koshy + * Copyright (c) 2006,2008,2018 Joseph Koshy * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,30 +30,34 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_xlate.c 3174 2015-03-27 17:13:41Z emaste $"); +ELFTC_VCSID("$Id: gelf_xlate.c 3632 2018-10-10 21:12:43Z jkoshy $"); Elf_Data * elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) { - return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOFILE); + return _libelf_xlate(dst, src, encoding, ELFCLASS32, EM_NONE, + ELF_TOFILE); } Elf_Data * elf64_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) { - return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOFILE); + return _libelf_xlate(dst, src, encoding, ELFCLASS64, EM_NONE, + ELF_TOFILE); } Elf_Data * elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) { - return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOMEMORY); + return _libelf_xlate(dst, src, encoding, ELFCLASS32, EM_NONE, + ELF_TOMEMORY); } Elf_Data * elf64_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) { - return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOMEMORY); + return _libelf_xlate(dst, src, encoding, ELFCLASS64, EM_NONE, + ELF_TOMEMORY); } Elf_Data * @@ -62,7 +66,7 @@ gelf_xlatetom(Elf *e, Elf_Data *dst, const Elf_Data *src, { if (e != NULL) return (_libelf_xlate(dst, src, encoding, e->e_class, - ELF_TOMEMORY)); + _libelf_elfmachine(e), ELF_TOMEMORY)); LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } @@ -73,7 +77,7 @@ gelf_xlatetof(Elf *e, Elf_Data *dst, const Elf_Data *src, { if (e != NULL) return (_libelf_xlate(dst, src, encoding, e->e_class, - ELF_TOFILE)); + _libelf_elfmachine(e), ELF_TOFILE)); LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } diff --git a/libelf/gelf_xlatetof.3 b/libelf/gelf_xlatetof.3 index ca90002096eb..2e5ee0fe15e0 100644 --- a/libelf/gelf_xlatetof.3 +++ b/libelf/gelf_xlatetof.3 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2006,2008 Joseph Koshy. All rights reserved. +.\" Copyright (c) 2006,2008,2018 Joseph Koshy. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf_xlatetof.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf_xlatetof.3 3639 2018-10-14 14:07:02Z jkoshy $ .\" -.Dd July 24, 2006 -.Os +.Dd October 11, 2018 .Dt GELF_XLATETOF 3 +.Os .Sh NAME .Nm elf32_xlate , .Nm elf64_xlate , @@ -36,36 +36,39 @@ .Sh SYNOPSIS .In libelf.h .Ft "Elf_Data *" -.Fn elf32_xlatetof "Elf_Data *dst" "Elf_Data *src" "unsigned int encode" +.Fn elf32_xlatetof "Elf_Data *dst" "Elf_Data *src" "unsigned int file_encoding" .Ft "Elf_Data *" -.Fn elf32_xlatetom "Elf_Data *dst" "Elf_Data *src" "unsigned int encode" +.Fn elf32_xlatetom "Elf_Data *dst" "Elf_Data *src" "unsigned int file_encoding" .Ft "Elf_Data *" -.Fn elf64_xlatetof "Elf_Data *dst" "Elf_Data *src" "unsigned int encode" +.Fn elf64_xlatetof "Elf_Data *dst" "Elf_Data *src" "unsigned int file_encoding" .Ft "Elf_Data *" -.Fn elf64_xlatetom "Elf_Data *dst" "Elf_Data *src" "unsigned int encode" +.Fn elf64_xlatetom "Elf_Data *dst" "Elf_Data *src" "unsigned int file_encoding" .In gelf.h .Ft "Elf_Data *" .Fo gelf_xlatetof .Fa "Elf *elf" .Fa "Elf_Data *dst" .Fa "Elf_Data *src" -.Fa "unsigned int encode" +.Fa "unsigned int file_encoding" .Fc .Ft "Elf_Data *" .Fo gelf_xlatetom .Fa "Elf *elf" .Fa "Elf_Data *dst" .Fa "Elf_Data *src" -.Fa "unsigned int encode" +.Fa "unsigned int file_encoding" .Fc .Sh DESCRIPTION These functions translate between the file and memory representations of ELF data structures. -The in-memory representation of an ELF data structure would confirm to +The in-memory representation of an ELF data structure would conform to the byte ordering and data alignment restrictions dictated by the host processor. -A file representation of the same data structure could use a non-native byte -ordering and in addition may be laid out differently with the file. +As described in +.Xr elf 3 , +the file representation of this data structure could use a different byte +ordering from that of the host, or could use a different layout within +the file. .Pp Functions .Fn elf32_xlatetom , @@ -153,7 +156,7 @@ value of the source data descriptor. .El .Pp Argument -.Ar encode +.Ar file_encoding specifies the encoding in which the file objects are represented. It must be one of: .Bl -hang -offset indent @@ -169,14 +172,41 @@ The functions .Fn gelf_xlatetof and .Fn gelf_xlatetom -select the appropriate 32 or 64 bit translations based on the class of argument +select the appropriate translation scheme based on the properties of +argument .Ar elf . .Sh RETURN VALUES These functions return argument .Ar dst if successful, or NULL in case of an error. .Sh EXAMPLES -TODO +To translate a +.Vt GElf_Rel +structure to its LSB file representation use: +.Bd -literal -offset indent +Elf_Data dst, src; +GElf_Rel rel; +Elf *e; + +e = ...; /* See elf_begin(3). */ + +/* Set up the 'src' descriptor. */ +memset(&src, 0, sizeof src); +src.d_buf = &rel; +src.d_size = sizeof(rel); +src.d_type = ELF_T_REL; +src.d_version = EV_CURRENT; + +/* Set up the 'dst' descriptor. */ +memset(&dst, 0, sizeof dst); +dst.d_buf = filebuf; +dst.d_size = gelf_fsize(e, ELF_T_REL, 1, EV_CURRENT); +dst.d_version = EV_CURRENT; + +if (gelf_xlatetof(e, &dst, &src, ELFDATA2LSB) == NULL) { + printf("error: %s", elf_errmsg(0)); +} +.Ed .Sh ERRORS These functions may fail with the following errors: .Bl -tag -width "[ELF_E_RESOURCE]" diff --git a/libelf/libelf_convert.m4 b/libelf/libelf_convert.m4 index ec395902a182..04adeb3114a7 100644 --- a/libelf/libelf_convert.m4 +++ b/libelf/libelf_convert.m4 @@ -30,7 +30,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_convert.m4 3429 2016-03-12 04:12:39Z emaste $"); +ELFTC_VCSID("$Id: libelf_convert.m4 3632 2018-10-10 21:12:43Z jkoshy $"); /* WARNING: GENERATED FROM __file__. */ @@ -1070,11 +1070,15 @@ CONVERTER_NAMES(ELF_TYPE_LIST) } }; -int (*_libelf_get_translator(Elf_Type t, int direction, int elfclass)) - (unsigned char *_dst, size_t dsz, unsigned char *_src, size_t _cnt, - int _byteswap) +/* + * Return a translator function for the specified ELF section type, conversion + * direction, ELF class and ELF machine. + */ +_libelf_translator_function * +_libelf_get_translator(Elf_Type t, int direction, int elfclass, int elfmachine) { assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); + assert(elfmachine >= EM_NONE && elfmachine < EM__LAST__); assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY); if (t >= ELF_T_NUM || @@ -1082,6 +1086,9 @@ int (*_libelf_get_translator(Elf_Type t, int direction, int elfclass)) (direction != ELF_TOFILE && direction != ELF_TOMEMORY)) return (NULL); + /* TODO: Handle MIPS64 REL{,A} sections (ticket #559). */ + (void) elfmachine; + return ((elfclass == ELFCLASS32) ? (direction == ELF_TOFILE ? cvt[t].tof32 : cvt[t].tom32) : (direction == ELF_TOFILE ? cvt[t].tof64 : cvt[t].tom64)); diff --git a/libelf/libelf_ehdr.c b/libelf/libelf_ehdr.c index 6630e6eba73e..38e4e74e14d2 100644 --- a/libelf/libelf_ehdr.c +++ b/libelf/libelf_ehdr.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_ehdr.c 3575 2017-09-14 02:13:36Z emaste $"); +ELFTC_VCSID("$Id: libelf_ehdr.c 3632 2018-10-10 21:12:43Z jkoshy $"); /* * Retrieve counts for sections, phdrs and the section string table index @@ -41,11 +41,10 @@ static int _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum, uint16_t strndx) { - Elf_Scn *scn; size_t fsz; - int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, - size_t _c, int _swap); + Elf_Scn *scn; uint32_t shtype; + _libelf_translator_function *xlator; assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn)); @@ -60,7 +59,8 @@ _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum, if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL) return (0); - xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec); + xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec, + _libelf_elfmachine(e)); (*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr), (unsigned char *) e->e_rawfile + shoff, (size_t) 1, e->e_byteorder != LIBELF_PRIVATE(byteorder)); @@ -166,7 +166,8 @@ _libelf_ehdr(Elf *e, int ec, int allocate) if (e->e_cmd == ELF_C_WRITE) return (ehdr); - xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec); + xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec, + _libelf_elfmachine(e)); (*xlator)((unsigned char*) ehdr, msz, e->e_rawfile, (size_t) 1, e->e_byteorder != LIBELF_PRIVATE(byteorder)); diff --git a/libelf/libelf_elfmachine.c b/libelf/libelf_elfmachine.c new file mode 100644 index 000000000000..f9c423950886 --- /dev/null +++ b/libelf/libelf_elfmachine.c @@ -0,0 +1,63 @@ +/*- + * Copyright (c) 2018 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <assert.h> +#include <libelf.h> + +#include "_libelf.h" + +ELFTC_VCSID("$Id$"); + +/* + * A convenience helper that returns the ELF machine architecture for + * a ELF descriptor. + */ +int +_libelf_elfmachine(Elf *e) +{ + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + + if (!e) + return EM_NONE; + + assert(e->e_kind == ELF_K_ELF); + assert(e->e_class != ELFCLASSNONE); + + eh32 = NULL; + eh64 = NULL; + + switch (e->e_class) { + case ELFCLASS32: + eh32 = e->e_u.e_elf.e_ehdr.e_ehdr32; + return eh32 ? eh32->e_machine : EM_NONE; + case ELFCLASS64: + eh64 = e->e_u.e_elf.e_ehdr.e_ehdr64; + return eh64 ? eh64->e_machine : EM_NONE; + } + + return (EM_NONE); +} diff --git a/libelf/libelf_phdr.c b/libelf/libelf_phdr.c index ba872bbb9744..336ef3877f6f 100644 --- a/libelf/libelf_phdr.c +++ b/libelf/libelf_phdr.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_phdr.c 3174 2015-03-27 17:13:41Z emaste $"); +ELFTC_VCSID("$Id: libelf_phdr.c 3632 2018-10-10 21:12:43Z jkoshy $"); void * _libelf_getphdr(Elf *e, int ec) @@ -42,8 +42,7 @@ _libelf_getphdr(Elf *e, int ec) Elf32_Ehdr *eh32; Elf64_Ehdr *eh64; void *ehdr, *phdr; - int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, - size_t _c, int _swap); + _libelf_translator_function *xlator; assert(ec == ELFCLASS32 || ec == ELFCLASS64); @@ -98,7 +97,8 @@ _libelf_getphdr(Elf *e, int ec) e->e_u.e_elf.e_phdr.e_phdr64 = phdr; - xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec); + xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec, + _libelf_elfmachine(e)); (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum, e->e_byteorder != LIBELF_PRIVATE(byteorder)); diff --git a/libelf/libelf_xlate.c b/libelf/libelf_xlate.c index 6ee76244b83e..cfe6a0bdfdbd 100644 --- a/libelf/libelf_xlate.c +++ b/libelf/libelf_xlate.c @@ -29,7 +29,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_xlate.c 3174 2015-03-27 17:13:41Z emaste $"); +ELFTC_VCSID("$Id: libelf_xlate.c 3632 2018-10-10 21:12:43Z jkoshy $"); /* * Translate to/from the file representation of ELF objects. @@ -45,11 +45,12 @@ ELFTC_VCSID("$Id: libelf_xlate.c 3174 2015-03-27 17:13:41Z emaste $"); Elf_Data * _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding, - int elfclass, int direction) + int elfclass, int elfmachine, int direction) { int byteswap; size_t cnt, dsz, fsz, msz; uintptr_t sb, se, db, de; + _libelf_translator_function *xlator; if (encoding == ELFDATANONE) encoding = LIBELF_PRIVATE(byteorder); @@ -138,8 +139,9 @@ _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding, (db == sb && !byteswap && fsz == msz)) return (dst); /* nothing more to do */ - if (!(_libelf_get_translator(src->d_type, direction, elfclass)) - (dst->d_buf, dsz, src->d_buf, cnt, byteswap)) { + xlator = _libelf_get_translator(src->d_type, direction, elfclass, + elfmachine); + if (!xlator(dst->d_buf, dsz, src->d_buf, cnt, byteswap)) { LIBELF_SET_ERROR(DATA, 0); return (NULL); } |
