diff --git a/lib/libelf/elf_begin.3 b/lib/libelf/elf_begin.3 index dac49102eafa..22c76ac006f5 100644 --- a/lib/libelf/elf_begin.3 +++ b/lib/libelf/elf_begin.3 @@ -23,7 +23,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 21, 2006 +.Dd April 11, 2010 .Dt ELF_BEGIN 3 .Os .Sh NAME @@ -163,7 +163,9 @@ archive may be opened in read mode (with argument set to .Dv ELF_C_READ ) using -.Fn elf_begin . +.Fn elf_begin +or +.Fn elf_memory . The returned ELF descriptor can be passed into to subsequent calls to .Fn elf_begin diff --git a/lib/libelf/elf_begin.c b/lib/libelf/elf_begin.c index 7879dbefd31c..6e81e32ced6c 100644 --- a/lib/libelf/elf_begin.c +++ b/lib/libelf/elf_begin.c @@ -131,13 +131,15 @@ elf_begin(int fd, Elf_Cmd c, Elf *a) case ELF_C_READ: /* * Descriptor `a' could be for a regular ELF file, or - * for an ar(1) archive. + * for an ar(1) archive. If descriptor `a' was opened + * using a valid file descriptor, we need to check if + * the passed in `fd' value matches the original one. */ - if (a && (a->e_fd != fd || c != a->e_cmd)) { + if (a && + ((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } - break; default: @@ -149,7 +151,7 @@ elf_begin(int fd, Elf_Cmd c, Elf *a) if (a == NULL) e = _libelf_open_object(fd, c); else if (a->e_kind == ELF_K_AR) - e = _libelf_ar_open_member(fd, c, a); + e = _libelf_ar_open_member(a->e_fd, c, a); else (e = a)->e_activations++;