[PATCH] libelf: There is just one ELF version.

Mark Wielaard mark@klomp.org
Sun Feb 24 17:08:00 GMT 2019


Remove (partially defined out) code and data structures dealing with
multiple ELF versions. There hasn't been a new ELF version in the
last 20 years. Simplify the code a bit by just assuming there will
only be one version (EV_CURRENT == 1).

Simplifies elf_version, gets rid of __libelf_version_initialized.
Removes one (or more) array (version) dimension from various tables
and accessor functions (__elf_xfctstom, shtype_map, __libelf_data_type,
__libelf_type_aligns and __libelf_type_sizes).

Signed-off-by: Mark Wielaard <mark@klomp.org>`
---
 libelf/ChangeLog              | 45 ++++++++++++++++++++++
 libelf/common.h               |  2 +-
 libelf/elf32_fsize.c          | 10 +----
 libelf/elf32_updatefile.c     | 70 ++++++++---------------------------
 libelf/elf32_updatenull.c     |  5 +--
 libelf/elf32_xlatetof.c       | 16 +-------
 libelf/elf32_xlatetom.c       | 16 +-------
 libelf/elf_begin.c            |  2 +-
 libelf/elf_compress.c         |  2 +-
 libelf/elf_getdata.c          | 26 ++++---------
 libelf/elf_getdata_rawchunk.c |  5 +--
 libelf/elf_newdata.c          |  2 +-
 libelf/elf_version.c          | 23 ++++--------
 libelf/gelf_fsize.c           | 13 ++-----
 libelf/gelf_xlate.c           |  6 +--
 libelf/libelfP.h              | 39 +++++++------------
 libelf/nlist.c                |  5 +--
 17 files changed, 111 insertions(+), 176 deletions(-)

diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index e4d39d3f0..ff00d1de5 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,48 @@
+2019-02-21  Mark Wielaard  <mark@klomp.org>
+
+	* common.h (determine_kind): Only accept EV_CURRENT.
+	* elf32_fsize.c (fsize): Just check version is EV_CURRENT.
+	Use __libelf_type_size without version dimension.
+	* elf32_updatefile.c (updatemmap): Define fctp from __elf_xfctstom
+	without version dimension.
+	(updatefile): Likewise.
+	* elf32_updatenull.c (default_ehdr): Check e_version is EV_CURRENT.
+	(updatenull_wrlock): Check d_version is EV_CURRENT.
+	(elf32_xlatetof): Likewise. And get recsize without version
+	dimension from __elf_xfctstom.
+	(elf32_xlatetom): Likewise.
+	* elf_begin.c (elf_begin): Check __libelf_version is EV_CURRENT.
+	* elf_compress.c (__libelf_reset_rawdata): Set d_version to
+	EV_CURRENT.
+	* elf_getdata.c (shtype_map): Remove version dimension.
+	(__libelf_type_aligns): Likewise.
+	(__libelf_data_type): Use shtype_map without version dimension.
+	(convert_data): Remove unused version argument. Get fp from
+	__elf_xfctstom without version dimensions.
+	(__libelf_set_data_list_rdlock): Call convert_data without version.
+	* elf_getdata_rawchunk.c (elf_getdata_rawchunk): Call __elfcfctstom
+	conversion function without version dimensions. Set d_version to
+	EV_CURRENT.
+	* elf_newdata.c (elf_newdata): Set d_version to EV_CURRENT.
+	* elf_version.c (__libelf_version_initialized): Removed.
+	(__libelf_version): Initialized to EV_NONE.
+	(elf_version): Always return EV_CURRENT for EV_NONE version.
+	Only accept (and return) EV_CURRENT as version.
+	* gelf_fsize.c (__libelf_type_sizes): Remove version dimension.
+	(gelf_fsize): Only accept EV_CURRENT as version.
+	Use __libelf_type_sizes without version dimension.
+	* gelf_xlate.c (__elf_xftstom): Remove version dimensions.
+	* libelfP.h (__elf_xfctstom): Defined without version dimensions.
+	(__elf_xfctstof): Likewise.
+	(__libelf_type_sizes): Define without version dimension.
+	(elf_typesize): Define using __libelf_type_sizes without version
+	dimension.
+	(__libelf_version_initialized): Remove definition.
+	(__libelf_version): Add definition.
+	(LIBELF_EV_IDX): Removed define.
+	(__libelf_type_aligns): Remove version dimension.
+	* nlist.c (nlist): Call elf_version unconditionally.
+
 2019-02-19  Mark Wielaard  <mark@klomp.org>
 
 	* elf_compress.c (do_deflate_cleanup): Remove ei_data argument,
diff --git a/libelf/common.h b/libelf/common.h
index 744f1bb81..624869032 100644
--- a/libelf/common.h
+++ b/libelf/common.h
@@ -56,7 +56,7 @@ determine_kind (void *buf, size_t len)
 
       if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
 	  && data > ELFDATANONE && data < ELFDATANUM
-	  && version > EV_NONE && version < EV_NUM)
+	  && version == EV_CURRENT)
 	return ELF_K_ELF;
     }
 
diff --git a/libelf/elf32_fsize.c b/libelf/elf32_fsize.c
index fddae91ed..0f468de52 100644
--- a/libelf/elf32_fsize.c
+++ b/libelf/elf32_fsize.c
@@ -44,7 +44,7 @@ elfw2(LIBELFBITS, fsize) (Elf_Type type, size_t count, unsigned int version)
 {
   /* We do not have differences between file and memory sizes.  Better
      not since otherwise `mmap' would not work.  */
-  if (unlikely (version == EV_NONE) || unlikely (version >= EV_NUM))
+  if (unlikely (version != EV_CURRENT))
     {
       __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
       return 0;
@@ -56,13 +56,7 @@ elfw2(LIBELFBITS, fsize) (Elf_Type type, size_t count, unsigned int version)
       return 0;
     }
 
-#if EV_NUM != 2
-  return (count
-	  * __libelf_type_sizes[version - 1][ELFW(ELFCLASS,LIBELFBITS) - 1][type]);
-#else
-  return (count
-	  * __libelf_type_sizes[0][ELFW(ELFCLASS,LIBELFBITS) - 1][type]);
-#endif
+  return (count * __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][type]);
 }
 #define local_strong_alias(n1, n2) strong_alias (n1, n2)
 local_strong_alias (elfw2(LIBELFBITS, fsize), __elfw2(LIBELFBITS, msize))
diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c
index 284bacc90..2899c6fbe 100644
--- a/libelf/elf32_updatefile.c
+++ b/libelf/elf32_updatefile.c
@@ -143,13 +143,8 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
       if (unlikely (change_bo))
 	{
 	  /* Today there is only one version of the ELF header.  */
-#if EV_NUM != 2
-	  xfct_t fctp;
-	  fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_EHDR];
-#else
-# undef fctp
-# define fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_EHDR]
-#endif
+#undef fctp
+#define fctp __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_EHDR]
 
 	  /* Do the real work.  */
 	  (*fctp) ((char *) elf->map_address + elf->start_offset, ehdr,
@@ -189,13 +184,8 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
       if (unlikely (change_bo))
 	{
 	  /* Today there is only one version of the ELF header.  */
-#if EV_NUM != 2
-	  xfct_t fctp;
-	  fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_PHDR];
-#else
-# undef fctp
-# define fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_PHDR]
-#endif
+#undef fctp
+#define fctp __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_PHDR]
 
 	  /* Do the real work.  */
 	  (*fctp) (elf->map_address + elf->start_offset + ehdr->e_phoff,
@@ -238,12 +228,8 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
 				+ ehdr->e_shoff);
       char *const shdr_end = shdr_start + shnum * ehdr->e_shentsize;
 
-#if EV_NUM != 2
-      xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
-#else
-# undef shdr_fctp
-# define shdr_fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR]
-#endif
+#undef shdr_fctp
+#define shdr_fctp __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR]
 #define shdr_dest ((ElfW2(LIBELFBITS,Shdr) *) shdr_start)
 
       /* Get all sections into the array and sort them.  */
@@ -358,13 +344,8 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
 				  && dl->data.d.d_size != 0
 				  && dl->data.d.d_type != ELF_T_BYTE))
 		      {
-#if EV_NUM != 2
-			xfct_t fctp;
-			fctp = __elf_xfctstom[__libelf_version - 1][dl->data.d.d_version - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][dl->data.d.d_type];
-#else
-# undef fctp
-# define fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][dl->data.d.d_type]
-#endif
+#undef fctp
+#define fctp __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][dl->data.d.d_type]
 
 			size_t align;
 			align = __libelf_type_align (ELFW(ELFCLASS,LIBELFBITS),
@@ -559,13 +540,8 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf, int change_bo, size_t shnum)
       if (unlikely (change_bo))
 	{
 	  /* Today there is only one version of the ELF header.  */
-#if EV_NUM != 2
-	  xfct_t fctp;
-	  fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_EHDR];
-#else
-# undef fctp
-# define fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_EHDR]
-#endif
+#undef fctp
+#define fctp __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_EHDR]
 
 	  /* Write the converted ELF header in a temporary buffer.  */
 	  (*fctp) (&tmp_ehdr, ehdr, sizeof (ElfW2(LIBELFBITS,Ehdr)), 1);
@@ -618,13 +594,8 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf, int change_bo, size_t shnum)
       if (unlikely (change_bo))
 	{
 	  /* Today there is only one version of the ELF header.  */
-#if EV_NUM != 2
-	  xfct_t fctp;
-	  fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_PHDR];
-#else
-# undef fctp
-# define fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_PHDR]
-#endif
+#undef fctp
+#define fctp __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_PHDR]
 
 	  /* Allocate sufficient memory.  */
 	  tmp_phdr = (ElfW2(LIBELFBITS,Phdr) *)
@@ -679,12 +650,8 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf, int change_bo, size_t shnum)
 	return 1;
 
       off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
-#if EV_NUM != 2
-      xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
-#else
-# undef shdr_fctp
-# define shdr_fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR]
-#endif
+#undef shdr_fctp
+#define shdr_fctp __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR]
 
       ElfW2(LIBELFBITS,Shdr) *shdr_data;
       ElfW2(LIBELFBITS,Shdr) *shdr_data_mem = NULL;
@@ -769,13 +736,8 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf, int change_bo, size_t shnum)
 
 		    if (unlikely (change_bo))
 		      {
-#if EV_NUM != 2
-			xfct_t fctp;
-			fctp = __elf_xfctstom[__libelf_version - 1][dl->data.d.d_version - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][dl->data.d.d_type];
-#else
-# undef fctp
-# define fctp __elf_xfctstom[0][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][dl->data.d.d_type]
-#endif
+#undef fctp
+#define fctp __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][dl->data.d.d_type]
 
 			buf = tmpbuf;
 			if (dl->data.d.d_size > MAX_TMPBUF)
diff --git a/libelf/elf32_updatenull.c b/libelf/elf32_updatenull.c
index 3e9ef61b4..2ce6a5974 100644
--- a/libelf/elf32_updatenull.c
+++ b/libelf/elf32_updatenull.c
@@ -89,7 +89,7 @@ ELFW(default_ehdr,LIBELFBITS) (Elf *elf, ElfW2(LIBELFBITS,Ehdr) *ehdr,
       ehdr->e_version = EV_CURRENT;
       elf->state.ELFW(elf,LIBELFBITS).ehdr_flags |= ELF_F_DIRTY;
     }
-  else if (unlikely (ehdr->e_version >= EV_NUM))
+  else if (unlikely (ehdr->e_version != EV_CURRENT))
     {
       __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
       return 1;
@@ -280,8 +280,7 @@ __elfw2(LIBELFBITS,updatenull_wrlock) (Elf *elf, int *change_bop, size_t shnum)
 			  && scn->rawdata.d.d_buf != NULL)
 			data = &scn->rawdata.d;
 
-		      if (unlikely (data->d_version == EV_NONE)
-			  || unlikely (data->d_version >= EV_NUM))
+		      if (unlikely (data->d_version != EV_CURRENT))
 			{
 			  __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
 			  return -1;
diff --git a/libelf/elf32_xlatetof.c b/libelf/elf32_xlatetof.c
index ac4eaf400..082d833fe 100644
--- a/libelf/elf32_xlatetof.c
+++ b/libelf/elf32_xlatetof.c
@@ -50,11 +50,7 @@ elfw2(LIBELFBITS, xlatetof) (Elf_Data *dest, const Elf_Data *src,
      type.  This means, whether there is an integer number of records.
      Note that for this implementation the memory and file size of the
      data types are identical.  */
-#if EV_NUM != 2
-  size_t recsize = __libelf_type_sizes[src->d_version - 1][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
-#else
-  size_t recsize = __libelf_type_sizes[0][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
-#endif
+  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
 
   if (src->d_size % recsize != 0)
     {
@@ -97,15 +93,7 @@ elfw2(LIBELFBITS, xlatetof) (Elf_Data *dest, const Elf_Data *src,
   else
     {
       xfct_t fctp;
-
-      /* Get a pointer to the transformation functions.  The `#ifdef' is
-	 a small optimization since we don't anticipate another ELF
-	 version and so would waste "precious" code.  */
-#if EV_NUM != 2
-      fctp = __elf_xfctstom[dest->d_version - 1][src->d_version - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
-#else
-      fctp = __elf_xfctstom[0][0][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
-#endif
+      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
 
       /* Do the real work.  */
       (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
diff --git a/libelf/elf32_xlatetom.c b/libelf/elf32_xlatetom.c
index 3b94cac73..cb0bb8d52 100644
--- a/libelf/elf32_xlatetom.c
+++ b/libelf/elf32_xlatetom.c
@@ -50,11 +50,7 @@ elfw2(LIBELFBITS, xlatetom) (Elf_Data *dest, const Elf_Data *src,
      type.  This means, whether there is an integer number of records.
      Note that for this implementation the memory and file size of the
      data types are identical.  */
-#if EV_NUM != 2
-  size_t recsize = __libelf_type_sizes[src->d_version - 1][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
-#else
-  size_t recsize = __libelf_type_sizes[0][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
-#endif
+  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
 
 
   /* We shouldn't require integer number of records when processing
@@ -102,15 +98,7 @@ elfw2(LIBELFBITS, xlatetom) (Elf_Data *dest, const Elf_Data *src,
   else
     {
       xfct_t fctp;
-
-      /* Get a pointer to the transformation functions.  The `#ifdef' is
-	 a small optimization since we don't anticipate another ELF
-	 version and so would waste "precious" code.  */
-#if EV_NUM != 2
-      fctp = __elf_xfctstom[src->d_version - 1][dest->d_version - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
-#else
-      fctp = __elf_xfctstom[0][0][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
-#endif
+      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
 
       /* Do the real work.  */
       (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index fde14c618..5d095ff0f 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -1108,7 +1108,7 @@ elf_begin (int fildes, Elf_Cmd cmd, Elf *ref)
 {
   Elf *retval;
 
-  if (unlikely (! __libelf_version_initialized))
+  if (unlikely (__libelf_version != EV_CURRENT))
     {
       /* Version wasn't set so far.  */
       __libelf_seterrno (ELF_E_NO_VERSION);
diff --git a/libelf/elf_compress.c b/libelf/elf_compress.c
index 874054ac6..244467b5e 100644
--- a/libelf/elf_compress.c
+++ b/libelf/elf_compress.c
@@ -313,7 +313,7 @@ __libelf_reset_rawdata (Elf_Scn *scn, void *buf, size_t size, size_t align,
 {
   /* This is the new raw data, replace and possibly free old data.  */
   scn->rawdata.d.d_off = 0;
-  scn->rawdata.d.d_version = __libelf_version;
+  scn->rawdata.d.d_version = EV_CURRENT;
   scn->rawdata.d.d_buf = buf;
   scn->rawdata.d.d_size = size;
   scn->rawdata.d.d_align = align;
diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
index 639a798e9..40fe16945 100644
--- a/libelf/elf_getdata.c
+++ b/libelf/elf_getdata.c
@@ -50,10 +50,8 @@
       : 0))
 
 /* Associate section types with libelf types.  */
-static const Elf_Type shtype_map[EV_NUM - 1][TYPEIDX (SHT_HISUNW) + 1] =
+static const Elf_Type shtype_map[TYPEIDX (SHT_HISUNW) + 1] =
   {
-    [EV_CURRENT - 1] =
-    {
       [SHT_SYMTAB] = ELF_T_SYM,
       [SHT_RELA] = ELF_T_RELA,
       [SHT_HASH] = ELF_T_WORD,
@@ -73,11 +71,10 @@ static const Elf_Type shtype_map[EV_NUM - 1][TYPEIDX (SHT_HISUNW) + 1] =
       [TYPEIDX (SHT_SUNW_move)] = ELF_T_MOVE,
       [TYPEIDX (SHT_GNU_LIBLIST)] = ELF_T_LIB,
       [TYPEIDX (SHT_GNU_HASH)] = ELF_T_GNUHASH,
-    }
   };
 
 /* Associate libelf types with their internal alignment requirements.  */
-const uint_fast8_t __libelf_type_aligns[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
+const uint_fast8_t __libelf_type_aligns[ELFCLASSNUM - 1][ELF_T_NUM] =
   {
 # define TYPE_ALIGNS(Bits)						      \
     {									      \
@@ -108,11 +105,8 @@ const uint_fast8_t __libelf_type_aligns[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM]
       [ELF_T_CHDR] = __alignof__ (ElfW2(Bits,Chdr)),			      \
       [ELF_T_NHDR8] = 8 /* Special case for GNU Property note.  */	      \
     }
-    [EV_CURRENT - 1] =
-    {
       [ELFCLASS32 - 1] = TYPE_ALIGNS (32),
       [ELFCLASS64 - 1] = TYPE_ALIGNS (64),
-    }
 # undef TYPE_ALIGNS
   };
 
@@ -131,7 +125,7 @@ __libelf_data_type (Elf *elf, int sh_type, GElf_Xword align)
     }
   else
     {
-      Elf_Type t = shtype_map[LIBELF_EV_IDX][TYPEIDX (sh_type)];
+      Elf_Type t = shtype_map[TYPEIDX (sh_type)];
       /* Special case for GNU Property notes.  */
       if (t == ELF_T_NHDR && align == 8)
 	t = ELF_T_NHDR8;
@@ -141,7 +135,7 @@ __libelf_data_type (Elf *elf, int sh_type, GElf_Xword align)
 
 /* Convert the data in the current section.  */
 static void
-convert_data (Elf_Scn *scn, int version __attribute__ ((unused)), int eclass,
+convert_data (Elf_Scn *scn, int eclass,
 	      int data, size_t size, Elf_Type type)
 {
   const size_t align = __libelf_type_align (eclass, type);
@@ -195,11 +189,7 @@ convert_data (Elf_Scn *scn, int version __attribute__ ((unused)), int eclass,
 	}
 
       /* Get the conversion function.  */
-#if EV_NUM != 2
-      fp = __elf_xfctstom[version - 1][__libelf_version - 1][eclass - 1][type];
-#else
-      fp = __elf_xfctstom[0][0][eclass - 1][type];
-#endif
+      fp = __elf_xfctstom[eclass - 1][type];
 
       fp (scn->data_base, rawdata_source, size, 0);
 
@@ -285,14 +275,14 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
 	}
       else
 	{
-	  Elf_Type t = shtype_map[LIBELF_EV_IDX][TYPEIDX (type)];
+	  Elf_Type t = shtype_map[TYPEIDX (type)];
 	  if (t == ELF_T_NHDR && align == 8)
 	    t = ELF_T_NHDR8;
 	  if (t == ELF_T_VDEF || t == ELF_T_NHDR || t == ELF_T_NHDR8
 	      || (t == ELF_T_GNUHASH && elf->class == ELFCLASS64))
 	    entsize = 1;
 	  else
-	    entsize = __libelf_type_sizes[LIBELF_EV_IDX][elf->class - 1][t];
+	    entsize = __libelf_type_sizes[elf->class - 1][t];
 	}
 
       /* We assume it is an array of bytes if it is none of the structured
@@ -444,7 +434,7 @@ __libelf_set_data_list_rdlock (Elf_Scn *scn, int wrlocked)
 	}
 
       /* Convert according to the version and the type.   */
-      convert_data (scn, __libelf_version, elf->class,
+      convert_data (scn, elf->class,
 		    (elf->class == ELFCLASS32
 		     || (offsetof (struct Elf, state.elf32.ehdr)
 			 == offsetof (struct Elf, state.elf64.ehdr))
diff --git a/libelf/elf_getdata_rawchunk.c b/libelf/elf_getdata_rawchunk.c
index d0c0b75f7..6a130737b 100644
--- a/libelf/elf_getdata_rawchunk.c
+++ b/libelf/elf_getdata_rawchunk.c
@@ -151,8 +151,7 @@ elf_getdata_rawchunk (Elf *elf, off_t offset, size_t size, Elf_Type type)
 	}
 
       /* Call the conversion function.  */
-      (*__elf_xfctstom[LIBELF_EV_IDX][LIBELF_EV_IDX][elf->class - 1][type])
-	(buffer, rawchunk, size, 0);
+      (*__elf_xfctstom[elf->class - 1][type])(buffer, rawchunk, size, 0);
     }
 
   /* Allocate the dummy container to point at this buffer.  */
@@ -171,7 +170,7 @@ elf_getdata_rawchunk (Elf *elf, off_t offset, size_t size, Elf_Type type)
   chunk->data.d.d_size = size;
   chunk->data.d.d_type = type;
   chunk->data.d.d_align = align;
-  chunk->data.d.d_version = __libelf_version;
+  chunk->data.d.d_version = EV_CURRENT;
 
   rwlock_unlock (elf->lock);
   rwlock_wrlock (elf->lock);
diff --git a/libelf/elf_newdata.c b/libelf/elf_newdata.c
index f6609a809..896f22cd3 100644
--- a/libelf/elf_newdata.c
+++ b/libelf/elf_newdata.c
@@ -117,7 +117,7 @@ elf_newdata (Elf_Scn *scn)
     }
 
   /* Set the predefined values.  */
-  result->data.d.d_version = __libelf_version;
+  result->data.d.d_version = EV_CURRENT;
 
   result->data.s = scn;
 
diff --git a/libelf/elf_version.c b/libelf/elf_version.c
index 7c336ff92..6ec534ab1 100644
--- a/libelf/elf_version.c
+++ b/libelf/elf_version.c
@@ -34,32 +34,25 @@
 #include <libelfP.h>
 
 
-/* Is the version initialized?  */
-int __libelf_version_initialized;
-
-/* Currently selected version.  */
-unsigned int __libelf_version = EV_CURRENT;
-
+/* Currently selected version.  Should be EV_CURRENT.
+   Will be EV_NONE if elf_version () has not been called yet.  */
+unsigned int __libelf_version = EV_NONE;
 
 unsigned int
 elf_version (unsigned int version)
 {
   if (version == EV_NONE)
-    return __libelf_version;
+    return EV_CURRENT;
 
-  if (likely (version < EV_NUM))
+  if (likely (version == EV_CURRENT))
     {
       /* Phew, we know this version.  */
-      unsigned int last_version = __libelf_version;
-
-      /* Store the new version.  */
-      __libelf_version = version;
 
       /* Signal that the version is now initialized.  */
-      __libelf_version_initialized = 1;
+      __libelf_version = EV_CURRENT;
 
-      /* And return the last version.  */
-      return last_version;
+      /* And return the last (or initial) version.  */
+      return EV_CURRENT;
     }
 
   /* We cannot handle this version.  */
diff --git a/libelf/gelf_fsize.c b/libelf/gelf_fsize.c
index d04ec5d54..493d79161 100644
--- a/libelf/gelf_fsize.c
+++ b/libelf/gelf_fsize.c
@@ -38,10 +38,8 @@
 
 
 /* These are the sizes for all the known types.  */
-const size_t __libelf_type_sizes[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
+const size_t __libelf_type_sizes[ELFCLASSNUM - 1][ELF_T_NUM] =
 {
-  /* We have no entry for EV_NONE since we have to set an error.  */
-  [EV_CURRENT - 1] = {
     [ELFCLASS32 - 1] = {
 #define TYPE_SIZES(LIBELFBITS) \
       [ELF_T_ADDR]	= ELFW2(LIBELFBITS, FSZ_ADDR),			      \
@@ -77,7 +75,6 @@ const size_t __libelf_type_sizes[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
     [ELFCLASS64 - 1] = {
       TYPE_SIZES (64)
     }
-  }
 };
 
 
@@ -89,7 +86,7 @@ gelf_fsize (Elf *elf, Elf_Type type, size_t count, unsigned int version)
   if (elf == NULL)
     return 0;
 
-  if (version == EV_NONE || version >= EV_NUM)
+  if (version != EV_CURRENT)
     {
       __libelf_seterrno (ELF_E_UNKNOWN_VERSION);
       return 0;
@@ -101,10 +98,6 @@ gelf_fsize (Elf *elf, Elf_Type type, size_t count, unsigned int version)
       return 0;
     }
 
-#if EV_NUM != 2
-  return count * __libelf_type_sizes[version - 1][elf->class - 1][type];
-#else
-  return count * __libelf_type_sizes[0][elf->class - 1][type];
-#endif
+  return count * __libelf_type_sizes[elf->class - 1][type];
 }
 INTDEF(gelf_fsize)
diff --git a/libelf/gelf_xlate.c b/libelf/gelf_xlate.c
index b5d6ef3de..f6f496d47 100644
--- a/libelf/gelf_xlate.c
+++ b/libelf/gelf_xlate.c
@@ -170,10 +170,8 @@ union unaligned
 
 
 /* Now the externally visible table with the function pointers.  */
-const xfct_t __elf_xfctstom[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
+const xfct_t __elf_xfctstom[ELFCLASSNUM - 1][ELF_T_NUM] =
 {
-  [EV_CURRENT - 1] = {
-    [EV_CURRENT - 1] = {
       [ELFCLASS32 - 1] = {
 #define define_xfcts(Bits) \
 	[ELF_T_BYTE]	= elf_cvt_Byte,					      \
@@ -209,8 +207,6 @@ const xfct_t __elf_xfctstom[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM]
 	define_xfcts (64),
 	[ELF_T_GNUHASH] = elf_cvt_gnuhash
       }
-    }
-  }
 };
 /* For now we only handle the case where the memory representation is the
    same as the file representation.  Should this change we have to define
diff --git a/libelf/libelfP.h b/libelf/libelfP.h
index 9f3e8e9df..bc9a404d5 100644
--- a/libelf/libelfP.h
+++ b/libelf/libelfP.h
@@ -411,43 +411,32 @@ struct Elf
 typedef void (*xfct_t) (void *, const void *, size_t, int);
 
 /* The table with the function pointers.  */
-extern const xfct_t __elf_xfctstom[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] attribute_hidden;
-extern const xfct_t __elf_xfctstof[EV_NUM - 1][EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] attribute_hidden;
+extern const xfct_t __elf_xfctstom[ELFCLASSNUM - 1][ELF_T_NUM]
+  attribute_hidden;
+extern const xfct_t __elf_xfctstof[ELFCLASSNUM - 1][ELF_T_NUM]
+  attribute_hidden;
 
 
 /* Array with sizes of the external types indexed by ELF version, binary
    class, and type. */
-extern const size_t __libelf_type_sizes[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] attribute_hidden;
+extern const size_t __libelf_type_sizes[ELFCLASSNUM - 1][ELF_T_NUM]
+  attribute_hidden;
 /* We often have to access the size for a type in the current version.  */
-#if EV_NUM != 2
 # define elf_typesize(class,type,n) \
-  elfw2(class,fsize) (type, n, __libelf_version)
-#else
-# define elf_typesize(class,type,n) \
-  (__libelf_type_sizes[EV_CURRENT - 1][ELFW(ELFCLASS,class) - 1][type] * n)
-#endif
-
-/* Currently selected version of the ELF specification.  */
-extern unsigned int __libelf_version attribute_hidden;
+  (__libelf_type_sizes[ELFW(ELFCLASS,class) - 1][type] * n)
 
 /* The byte value used for filling gaps.  */
 extern int __libelf_fill_byte attribute_hidden;
 
-/* Nonzero if the version was set.  */
-extern int __libelf_version_initialized attribute_hidden;
-
-/* Index for __libelf_type_sizes et al.  */
-#if EV_NUM == 2
-# define LIBELF_EV_IDX	0
-#else
-# define LIBELF_EV_IDX	(__libelf_version - 1)
-#endif
+/* EV_CURRENT if the version was set, EV_NONE otherwise.  */
+extern unsigned int __libelf_version attribute_hidden;
 
-/* Array with alignment requirements of the internal types indexed by ELF
-   version, binary class, and type. */
-extern const uint_fast8_t __libelf_type_aligns[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] attribute_hidden;
+/* Array with alignment requirements of the internal types indexed by
+   binary class, and type. */
+extern const uint_fast8_t __libelf_type_aligns[ELFCLASSNUM - 1][ELF_T_NUM]
+  attribute_hidden;
 # define __libelf_type_align(class, type)	\
-    (__libelf_type_aligns[LIBELF_EV_IDX][class - 1][type] ?: 1)
+    (__libelf_type_aligns[class - 1][type] ?: 1)
 
 /* Given an Elf handle and a section type returns the Elf_Data d_type.
    Should not be called when SHF_COMPRESSED is set, the d_type should
diff --git a/libelf/nlist.c b/libelf/nlist.c
index c7b32fdbe..8593c1de8 100644
--- a/libelf/nlist.c
+++ b/libelf/nlist.c
@@ -80,9 +80,8 @@ nlist (const char *filename, struct nlist *nl)
 
   /* For compatibility reasons (`nlist' existed before ELF and libelf)
      we don't expect the caller to set the ELF version.  Do this here
-     if it hasn't happened yet.  */
-  if (__libelf_version_initialized == 0)
-    INTUSE(elf_version) (EV_CURRENT);
+     as if it hasn't happened yet.  */
+  INTUSE(elf_version) (EV_CURRENT);
 
   /* Now get an ELF descriptor.  */
   elf = INTUSE(elf_begin) (fd, ELF_C_READ_MMAP, NULL);
-- 
2.20.1



More information about the Elfutils-devel mailing list