Fix up bogon and missing log entries from .pmachata.threads branch.
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 0e4a928..d258de8 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,85 +1,43 @@
-2008-08-15  Ulrich Drepper  <drepper@redhat.com>
+2008-08-25  Roland McGrath  <roland@redhat.com>
 
-	* libelfP.h (LIBELF_CHECKED_LOCK): Use dummy definition also if
-	USE_TLS is not defined.
+	* Makefile.am (libelf_so_LDLIBS): New variable.
+	(libelf.so): Use it in the link.
 
-2008-08-05  Petr Machata  <pmachata@redhat.com>
+2008-08-21  Petr Machata  <pmachata@redhat.com>
 
-	* elf_getdata.c, libelfP.h (__elf_getdata_internal):
-	Add lockstat argument in a fashion similar to elf32_getehdr below.
-	* elf32_updatenull.c (__elf32_updatenull, __elf64_updatenull):
-	Call __elf32_getshdr_internal and __elf64_getshdr_internal
-	explicitly, not via INTUSE.
-	Call __elf_getdata_internal explicitly, not via INTUSE.
-
-2008-08-05  Petr Machata  <pmachata@redhat.com>
-
-	* gelf_getehdr.c, libelfP.h (__gelf_getehdr_internal):
-	Add lockstat argument in a fashion similar to elf32_getehdr below.
-	* elf_getdata.c, libelfP.h (__libelf_set_rawdata):
-	Add lockstat argument.
-	Call __gelf_getehdr_internal explicitly, not via INTUSE.
-	Call rwlock_to_wrlock instead of RWLOCK_UNLOCK/_WRLOCK sequence.
-	(elf_getdata): Pass lockstat argument to __libelf_set_rawdata.
+	* elf_getdata.c, libelfP.h
+	(__elf_getdata_internal): Rename to __elf_getdata_rdlock.
+	(__libelf_set_rawdata_wrlock): New function.
+	(__libelf_set_rawdata): Make it a wrapper that calls *_wrlock.
 	* elf32_updatenull.c, libelfP.h
-	(__elf32_updatenull, __elf64_updatenull):
-	Add lockstat argument in a fashion similar to elf32_getehdr below.
-	Pass lockstat argument to __libelf_set_rawdata.
-	* elf_strptr.c (elf_strptr):
-	Pass lockstat argument to __libelf_set_rawdata.
-	* elf_update.c (elf_update):
-	Pass lockstat argument to __elf32_updatenull and
-	__elf64_updatenull.
+	(__elfNN_updatenull): Rename to __elfNN_updatenull_wrlock.
 
-2008-08-05  Petr Machata  <pmachata@redhat.com>
+2008-08-21  Petr Machata  <pmachata@redhat.com>
 
-	* common.h (rwlock_to_wrlock, rwlock_from_wrlock): New functions.
 	* elf32_getphdr.c, libelfP.h
-	(__elf32_getphdr_internal, __elf64_getphdr_internal):
-	Add lockstat argument in a fashion similar to elf32_getehdr below.
-	* gelf_getphdr.c (gelf_getphdr): Call __elf32_getphdr_internal and
-	__elf32_getphdr_internal explicitly, not via INTUSE.
+	(__elfNN_getphdr_internal): Drop.  Move __elfNN_getphdr_internal
+	code to __elfNN_getphdr_wrlock.
+	(__elfNN_getphdr_rdlock, __elfNN_getphdr_wrlock): New functions.
+	(__elfNN_getphdr_rdlock, __elfNN_getphdr_wrlock): Make these
+	wrappers of getphdr_impl.
 
-2008-08-05  Petr Machata  <pmachata@redhat.com>
+2008-08-21  Petr Machata  <pmachata@redhat.com>
 
-	* libelfP.h (lockstat_t): New enum that describes what type of
-	lock is currently being held.  To be used by _internal functions.
 	* elf32_getehdr.c, libelfP.h
-	(__elf32_getehdr_internal, __elf64_getehdr_internal):
-	Take extra lockstat argument.
-	Move "meat" of the getehdr functinonality here.
-	* elf32_getehdr.c (elf32_getehdr, elf64_getehdr):
-	Make this a simple wrapper that calls _internal variant.
-	* elf32_updatenull.c: Call __elf32_getehdr_internal explicitly,
-	not via INTUSE.
-	* elf32_getshdr.c, libelfP.h
-	(__elf32_getshdr_internal, __elf64_getshdr_internal):
-	Add lockstat argument in a fashion similar to elf32_getehdr.
-	* elf_getshnum.c, libelfP.h (__elf_getshnum_internal):
-	Likewise.
-	* gelf_getshdr.c (gelf_getshdr): Call __elf32_getshdr_internal,
-	__elf64_getshdr_internal explicitly, not via INTUSE.
+	(__elfNN_getehdr_internal): Rename to __elfNN_getehdr_rdlock.
+	* gelf_getehdr, libelfP.h:
+	(__gelf_getehdr_internal): Rename to __gelf_getehdr_rdlock.
 
-2008-08-04  Petr Machata  <pmachata@redhat.com>
+2008-08-21  Petr Machata  <pmachata@redhat.com>
 
-	* common.h, elf32_getehdr.c, elf32_getphdr.c, elf32_getshdr.c,
-	elf32_newehdr.c, elf32_newphdr.c, elf32_offscn.c, elf_begin.c,
-	elf_clone.c, elf_cntl.c, elf_end.c, elf_getarsym.c, elf_getdata.c,
-	elf_getscn.c, elf_getshnum.c, elf_getshstrndx.c, elf_newdata.c,
-	elf_newscn.c, elf_nextscn.c, elf_readall.c, elf_strptr.c,
-	elf_update.c, gelf_getauxv.c, gelf_getdyn.c, gelf_getehdr.c,
-	gelf_getlib.c, gelf_getmove.c, gelf_getnote.c, gelf_getphdr.c,
-	gelf_getrel.c, gelf_getrela.c, gelf_getshdr.c, gelf_getsym.c,
-	gelf_getsyminfo.c, gelf_getsymshndx.c, gelf_getverdaux.c,
-	gelf_getverdef.c, gelf_getvernaux.c, gelf_getverneed.c,
-	gelf_getversym.c, gelf_update_auxv.c, gelf_update_dyn.c,
-	gelf_update_ehdr.c, gelf_update_lib.c, gelf_update_move.c,
-	gelf_update_phdr.c, gelf_update_rel.c, gelf_update_rela.c,
-	gelf_update_sym.c, gelf_update_syminfo.c, gelf_update_symshndx.c,
-	gelf_update_verdaux.c, gelf_update_verdef.c,
-	gelf_update_vernaux.c, gelf_update_verneed.c,
-	gelf_update_versym.c:
-		Change rwlock_{rd,wr,un}lock calls to RWLOCK_{RD,WR,UN}LOCK.
+	* elf32_getshdr.c
+	(__elfNN_getshdr_internal): Drop.
+	(load_shdr_rwlock, scn_valid): New functions, contain bits of
+	behaviour from __elfNN_getshdr_internal.
+	(__elfNN_getshdr_rdlock, __elfNN_getshdr_wrlock): Replacements for
+	dropped _internal functions above.
+	* elf_getshnum.c
+	(__elf_getshnum_internal): Rename to __elf_getshnum_rdlock.
 
 2008-08-04  Petr Machata  <pmachata@redhat.com>
 
diff --git a/libelf/common.h b/libelf/common.h
index c00bfbc..8eef2a7 100644
--- a/libelf/common.h
+++ b/libelf/common.h
@@ -118,7 +118,7 @@
 __attribute__ ((unused))
 libelf_acquire_all (Elf *elf)
 {
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   if (elf->kind == ELF_K_AR)
     {
@@ -150,33 +150,7 @@
 	}
     }
 
-  RWLOCK_UNLOCK (elf->lock);
-}
-
-/* Convert given lock LOCK with lock state FROM to lock state
-   LS_WRLOCKED. */
-static void
-__attribute__ ((unused))
-rwlock_to_wrlock(lockstat_t from, rwlock_define (,*lock))
-{
-  if (from == LS_WRLOCKED)
-    return;
-  if (from == LS_RDLOCKED)
-    RWLOCK_UNLOCK (*lock);
-  RWLOCK_WRLOCK (*lock);
-}
-
-/* Convert given lock LOCK with lock state LS_WRLOCKED to lock state
-   TO. */
-static void
-__attribute__ ((unused))
-rwlock_from_wrlock(lockstat_t to, rwlock_define (,*lock))
-{
-  if (to == LS_WRLOCKED)
-    return;
-  RWLOCK_UNLOCK (*lock);
-  if (to == LS_RDLOCKED)
-    RWLOCK_RDLOCK (*lock);
+  rwlock_unlock (elf->lock);
 }
 
 
diff --git a/libelf/elf32_checksum.c b/libelf/elf32_checksum.c
index ddc978d..0e4ab9f 100644
--- a/libelf/elf32_checksum.c
+++ b/libelf/elf32_checksum.c
@@ -150,7 +150,7 @@
 	}
 
       /* Iterate through the list of data blocks.  */
-      while ((data = __elf_getdata_internal (scn, data, LS_UNLOCKED)) != NULL)
+      while ((data = INTUSE(elf_getdata) (scn, data)) != NULL)
 	/* If the file byte order is the same as the host byte order
 	   process the buffer directly.  If the data is just a stream
 	   of bytes which the library will not convert we can use it
diff --git a/libelf/elf32_getehdr.c b/libelf/elf32_getehdr.c
index e5c218a..d62e255 100644
--- a/libelf/elf32_getehdr.c
+++ b/libelf/elf32_getehdr.c
@@ -63,9 +63,8 @@
 
 
 ElfW2(LIBELFBITS,Ehdr) *
-__elfw2(LIBELFBITS,getehdr_internal) (elf, locked)
+__elfw2(LIBELFBITS,getehdr_rdlock) (elf)
      Elf *elf;
-     lockstat_t locked;
 {
   ElfW2(LIBELFBITS,Ehdr) *result;
 
@@ -78,9 +77,6 @@
       return NULL;
     }
 
-  if (locked == LS_UNLOCKED)
-    RWLOCK_RDLOCK (elf->lock);
-
   if (elf->class == 0)
     elf->class = ELFW(ELFCLASS,LIBELFBITS);
   else if (unlikely (elf->class != ELFW(ELFCLASS,LIBELFBITS)))
@@ -93,9 +89,6 @@
   result = elf->state.ELFW(elf,LIBELFBITS).ehdr;
 
  out:
-  if (locked == LS_UNLOCKED)
-    RWLOCK_UNLOCK (elf->lock);
-
   return result;
 }
 
@@ -103,8 +96,13 @@
 elfw2(LIBELFBITS,getehdr) (elf)
      Elf *elf;
 {
+  ElfW2(LIBELFBITS,Ehdr) *result;
   if (elf == NULL)
     return NULL;
 
-  return __elfw2(LIBELFBITS,getehdr_internal) (elf, LS_UNLOCKED);
+  rwlock_rdlock (elf->lock);
+  result = __elfw2(LIBELFBITS,getehdr_rdlock) (elf);
+  rwlock_unlock (elf->lock);
+
+  return result;
 }
diff --git a/libelf/elf32_getphdr.c b/libelf/elf32_getphdr.c
index 972132e..c0b28aa 100644
--- a/libelf/elf32_getphdr.c
+++ b/libelf/elf32_getphdr.c
@@ -53,6 +53,7 @@
 #endif
 
 #include <errno.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <assert.h>
@@ -65,23 +66,12 @@
 # define LIBELFBITS 32
 #endif
 
-
 ElfW2(LIBELFBITS,Phdr) *
-__elfw2(LIBELFBITS,getphdr_internal) (elf, locked)
-     lockstat_t locked;
+__elfw2(LIBELFBITS,getphdr_wrlock) (elf)
      Elf *elf;
 {
   ElfW2(LIBELFBITS,Phdr) *result;
 
-  if (elf == NULL)
-    return NULL;
-
-  if (unlikely (elf->kind != ELF_K_ELF))
-    {
-      __libelf_seterrno (ELF_E_INVALID_HANDLE);
-      return NULL;
-    }
-
   /* If the program header entry has already been filled in the code
      below must already have been run.  So the class is set, too.  No
      need to waste any more time here.  */
@@ -89,8 +79,6 @@
   if (likely (result != NULL))
     return result;
 
-  rwlock_to_wrlock (locked, &elf->lock);
-
   if (elf->class == 0)
     elf->class = ELFW(ELFCLASS,LIBELFBITS);
   else if (elf->class != ELFW(ELFCLASS,LIBELFBITS))
@@ -235,7 +223,6 @@
     }
 
  out:
-  rwlock_from_wrlock (locked, &elf->lock);
   return result;
 }
 
@@ -243,8 +230,12 @@
 elfw2(LIBELFBITS,getphdr) (elf)
      Elf *elf;
 {
-  if (elf == NULL)
-    return NULL;
+  ElfW2(LIBELFBITS,Phdr) *result;
 
-  return __elfw2(LIBELFBITS,getphdr_internal) (elf, LS_UNLOCKED);
+  rwlock_wrlock (elf->lock);
+  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
+  rwlock_unlock (elf->lock);
+
+  return result;
 }
+INTDEF(elfw2(LIBELFBITS,getphdr))
diff --git a/libelf/elf32_getshdr.c b/libelf/elf32_getshdr.c
index 8d65875..b36e543 100644
--- a/libelf/elf32_getshdr.c
+++ b/libelf/elf32_getshdr.c
@@ -54,6 +54,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <stdbool.h>
 #include <unistd.h>
 
 #include <system.h>
@@ -65,187 +66,230 @@
 #endif
 
 
-ElfW2(LIBELFBITS,Shdr) *
-__elfw2(LIBELFBITS,getshdr_internal) (scn, locked)
-     Elf_Scn *scn;
-     lockstat_t locked;
+static ElfW2(LIBELFBITS,Shdr) *
+load_shdr_rwlock (Elf_Scn *scn)
 {
-  /* XXX: no read locking here, figure out why is it not necessary. */
   ElfW2(LIBELFBITS,Shdr) *result;
 
+  /* Read the section header table.  */
+  Elf *elf = scn->elf;
+  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
+
+  /* Try again, maybe the data is there now.  */
+  result = scn->shdr.ELFW(e,LIBELFBITS);
+  if (result != NULL)
+    goto out;
+
+  size_t shnum;
+  if (__elf_getshnum_rdlock (elf, &shnum) != 0)
+    goto out;
+  size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
+
+  /* Allocate memory for the section headers.  We know the number
+     of entries from the ELF header.  */
+  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
+    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
+  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
+    {
+      __libelf_seterrno (ELF_E_NOMEM);
+      goto out;
+    }
+  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
+
+  if (elf->map_address != NULL)
+    {
+      ElfW2(LIBELFBITS,Shdr) *notcvt;
+
+      /* All the data is already mapped.  If we could use it
+	 directly this would already have happened.  */
+      void *file_shdr = ((char *) elf->map_address
+			 + elf->start_offset + ehdr->e_shoff);
+
+      assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
+	      || (! ALLOW_UNALIGNED
+		  && ((uintptr_t) file_shdr
+		      & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
+
+      /* Now copy the data and at the same time convert the byte order.  */
+      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
+	{
+	  assert (! ALLOW_UNALIGNED);
+	  memcpy (shdr, file_shdr, size);
+	}
+      else
+	{
+	  if (ALLOW_UNALIGNED
+	      || ((uintptr_t) file_shdr
+		  & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
+	    notcvt = (ElfW2(LIBELFBITS,Shdr) *)
+	      ((char *) elf->map_address
+	       + elf->start_offset + ehdr->e_shoff);
+	  else
+	    {
+	      notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
+	      memcpy (notcvt, ((char *) elf->map_address
+			       + elf->start_offset + ehdr->e_shoff),
+		      size);
+	    }
+
+	  for (size_t cnt = 0; cnt < shnum; ++cnt)
+	    {
+	      CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
+	      CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
+	      CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
+	      CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
+	      CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
+	      CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
+	      CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
+	      CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
+	      CONVERT_TO (shdr[cnt].sh_addralign,
+			  notcvt[cnt].sh_addralign);
+	      CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
+
+	      /* If this is a section with an extended index add a
+		 reference in the section which uses the extended
+		 index.  */
+	      if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
+		  && shdr[cnt].sh_link < shnum)
+		elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
+		  = cnt;
+
+	      /* Set the own shndx_index field in case it has not yet
+		 been set.  */
+	      if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
+		elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
+		  = -1;
+	    }
+	}
+    }
+  else if (likely (elf->fildes != -1))
+    {
+      /* Read the header.  */
+      ssize_t n = pread_retry (elf->fildes,
+			       elf->state.ELFW(elf,LIBELFBITS).shdr, size,
+			       elf->start_offset + ehdr->e_shoff);
+      if (unlikely ((size_t) n != size))
+	{
+	  /* Severe problems.  We cannot read the data.  */
+	  __libelf_seterrno (ELF_E_READ_ERROR);
+	  goto free_and_out;
+	}
+
+      /* If the byte order of the file is not the same as the one
+	 of the host convert the data now.  */
+      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
+	for (size_t cnt = 0; cnt < shnum; ++cnt)
+	  {
+	    CONVERT (shdr[cnt].sh_name);
+	    CONVERT (shdr[cnt].sh_type);
+	    CONVERT (shdr[cnt].sh_flags);
+	    CONVERT (shdr[cnt].sh_addr);
+	    CONVERT (shdr[cnt].sh_offset);
+	    CONVERT (shdr[cnt].sh_size);
+	    CONVERT (shdr[cnt].sh_link);
+	    CONVERT (shdr[cnt].sh_info);
+	    CONVERT (shdr[cnt].sh_addralign);
+	    CONVERT (shdr[cnt].sh_entsize);
+	  }
+    }
+  else
+    {
+      /* The file descriptor was already enabled and not all data was
+	 read.  Undo the allocation.  */
+      __libelf_seterrno (ELF_E_FD_DISABLED);
+
+    free_and_out:
+      free (shdr);
+      elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
+      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
+
+      goto out;
+    }
+
+  /* Set the pointers in the `scn's.  */
+  for (size_t cnt = 0; cnt < shnum; ++cnt)
+    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
+      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
+
+  result = scn->shdr.ELFW(e,LIBELFBITS);
+  assert (result != NULL);
+
+out:
+  return result;
+}
+
+static bool
+scn_valid (Elf_Scn *scn)
+{
   if (scn == NULL)
-    return NULL;
+    return false;
 
   if (unlikely (scn->elf->state.elf.ehdr == NULL))
     {
       __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
-      return NULL;
+      return false;
     }
 
   if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
     {
       __libelf_seterrno (ELF_E_INVALID_CLASS);
-      return NULL;
+      return false;
     }
 
+  return true;
+}
+
+ElfW2(LIBELFBITS,Shdr) *
+__elfw2(LIBELFBITS,getshdr_rdlock) (scn)
+     Elf_Scn *scn;
+{
+  /* XXX: no read locking here, figure out why is it not necessary. */
+  ElfW2(LIBELFBITS,Shdr) *result;
+
+  if (!scn_valid (scn))
+    return NULL;
+
   result = scn->shdr.ELFW(e,LIBELFBITS);
   if (result == NULL)
     {
-      /* Read the section header table.  */
-      Elf *elf = scn->elf;
-      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
-
-      rwlock_to_wrlock (locked, &elf->lock);
-
-      /* Try again, maybe the data is there now.  */
+      rwlock_unlock (scn->elf->lock);
+      rwlock_wrlock (scn->elf->lock);
       result = scn->shdr.ELFW(e,LIBELFBITS);
-      if (result != NULL)
-	goto out;
-
-      size_t shnum;
-      if (__elf_getshnum_internal (elf, &shnum, LS_WRLOCKED) != 0)
-	goto out;
-      size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
-
-      /* Allocate memory for the section headers.  We know the number
-	 of entries from the ELF header.  */
-      ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
-	(ElfW2(LIBELFBITS,Shdr) *) malloc (size);
-      if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
-	{
-	  __libelf_seterrno (ELF_E_NOMEM);
-	  goto out;
-	}
-      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
-
-      if (elf->map_address != NULL)
-	{
-	  ElfW2(LIBELFBITS,Shdr) *notcvt;
-
-	  /* All the data is already mapped.  If we could use it
-	     directly this would already have happened.  */
-	  void *file_shdr = ((char *) elf->map_address
-			     + elf->start_offset + ehdr->e_shoff);
-
-	  assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
-		  || (! ALLOW_UNALIGNED
-		      && ((uintptr_t) file_shdr
-			  & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
-
-	  /* Now copy the data and at the same time convert the byte order.  */
-	  if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
-	    {
-	      assert (! ALLOW_UNALIGNED);
-	      memcpy (shdr, file_shdr, size);
-	    }
-	  else
-	    {
-	      if (ALLOW_UNALIGNED
-		  || ((uintptr_t) file_shdr
-		      & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
-		notcvt = (ElfW2(LIBELFBITS,Shdr) *)
-		  ((char *) elf->map_address
-		   + elf->start_offset + ehdr->e_shoff);
-	      else
-		{
-		  notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
-		  memcpy (notcvt, ((char *) elf->map_address
-				   + elf->start_offset + ehdr->e_shoff),
-			  size);
-		}
-
-	      for (size_t cnt = 0; cnt < shnum; ++cnt)
-		{
-		  CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
-		  CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
-		  CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
-		  CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
-		  CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
-		  CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
-		  CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
-		  CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
-		  CONVERT_TO (shdr[cnt].sh_addralign,
-			      notcvt[cnt].sh_addralign);
-		  CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
-
-		  /* If this is a section with an extended index add a
-		     reference in the section which uses the extended
-		     index.  */
-		  if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
-		      && shdr[cnt].sh_link < shnum)
-		    elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
-		      = cnt;
-
-		  /* Set the own shndx_index field in case it has not yet
-		     been set.  */
-		  if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
-		    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
-		      = -1;
-		}
-	    }
-	}
-      else if (likely (elf->fildes != -1))
-	{
-	  /* Read the header.  */
-	  ssize_t n = pread_retry (elf->fildes,
-				   elf->state.ELFW(elf,LIBELFBITS).shdr, size,
-				   elf->start_offset + ehdr->e_shoff);
-	  if (unlikely ((size_t) n != size))
-	    {
-	      /* Severe problems.  We cannot read the data.  */
-	      __libelf_seterrno (ELF_E_READ_ERROR);
-	      goto free_and_out;
-	    }
-
-	  /* If the byte order of the file is not the same as the one
-	     of the host convert the data now.  */
-	  if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
-	    for (size_t cnt = 0; cnt < shnum; ++cnt)
-	      {
-		CONVERT (shdr[cnt].sh_name);
-		CONVERT (shdr[cnt].sh_type);
-		CONVERT (shdr[cnt].sh_flags);
-		CONVERT (shdr[cnt].sh_addr);
-		CONVERT (shdr[cnt].sh_offset);
-		CONVERT (shdr[cnt].sh_size);
-		CONVERT (shdr[cnt].sh_link);
-		CONVERT (shdr[cnt].sh_info);
-		CONVERT (shdr[cnt].sh_addralign);
-		CONVERT (shdr[cnt].sh_entsize);
-	      }
-	}
-      else
-	{
-	  /* The file descriptor was already enabled and not all data was
-	     read.  Undo the allocation.  */
-	  __libelf_seterrno (ELF_E_FD_DISABLED);
-
-	free_and_out:
-	  free (shdr);
-	  elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
-	  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
-
-	  goto out;
-	}
-
-      /* Set the pointers in the `scn's.  */
-      for (size_t cnt = 0; cnt < shnum; ++cnt)
-	elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
-	  = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
-
-      result = scn->shdr.ELFW(e,LIBELFBITS);
-      assert (result != NULL);
-
-    out:
-      rwlock_from_wrlock (locked, &elf->lock);
+      if (result == NULL)
+	result = load_shdr_rwlock (scn);
     }
 
   return result;
 }
 
 ElfW2(LIBELFBITS,Shdr) *
+__elfw2(LIBELFBITS,getshdr_wrlock) (scn)
+     Elf_Scn *scn;
+{
+  ElfW2(LIBELFBITS,Shdr) *result;
+
+  if (!scn_valid (scn))
+    return NULL;
+
+  result = scn->shdr.ELFW(e,LIBELFBITS);
+  if (result == NULL)
+    result = load_shdr_rwlock (scn);
+
+  return result;
+}
+
+ElfW2(LIBELFBITS,Shdr) *
 elfw2(LIBELFBITS,getshdr) (scn)
      Elf_Scn *scn;
 {
-  return __elfw2(LIBELFBITS,getshdr_internal) (scn, LS_UNLOCKED);
+  ElfW2(LIBELFBITS,Shdr) *result;
+
+  if (!scn_valid (scn))
+    return NULL;
+
+  rwlock_rdlock (scn->elf->lock);
+  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
+  rwlock_unlock (scn->elf->lock);
+
+  return result;
 }
diff --git a/libelf/elf32_newehdr.c b/libelf/elf32_newehdr.c
index 0ef68c5..4e20056 100644
--- a/libelf/elf32_newehdr.c
+++ b/libelf/elf32_newehdr.c
@@ -77,7 +77,7 @@
       return NULL;
     }
 
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   if (elf->class == 0)
     elf->class = ELFW(ELFCLASS,LIBELFBITS);
@@ -106,7 +106,7 @@
   result = elf->state.ELFW(elf,LIBELFBITS).ehdr;
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf32_newphdr.c b/libelf/elf32_newphdr.c
index bc93785..d1b1608 100644
--- a/libelf/elf32_newphdr.c
+++ b/libelf/elf32_newphdr.c
@@ -79,7 +79,7 @@
       return NULL;
     }
 
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   if (elf->class == 0)
     elf->class = ELFW(ELFCLASS,LIBELFBITS);
@@ -164,7 +164,7 @@
     }
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf32_offscn.c b/libelf/elf32_offscn.c
index 2f9e1ce..86eff8b 100644
--- a/libelf/elf32_offscn.c
+++ b/libelf/elf32_offscn.c
@@ -86,7 +86,7 @@
       && unlikely (elfw2(LIBELFBITS,getshdr) (&runp->data[0]) == NULL))
     return NULL;
 
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   Elf_Scn *result = NULL;
 
@@ -114,7 +114,7 @@
     }
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf32_updatenull.c b/libelf/elf32_updatenull.c
index dd50fb2..ae8696e 100644
--- a/libelf/elf32_updatenull.c
+++ b/libelf/elf32_updatenull.c
@@ -133,14 +133,13 @@
 
 off_t
 internal_function
-__elfw2(LIBELFBITS,updatenull) (Elf *elf, int *change_bop,
-				size_t shnum, lockstat_t locked)
+__elfw2(LIBELFBITS,updatenull_wrlock) (Elf *elf, int *change_bop, size_t shnum)
 {
   ElfW2(LIBELFBITS,Ehdr) *ehdr;
   int changed = 0;
   int ehdr_flags = 0;
 
-  ehdr = __elfw2(LIBELFBITS,getehdr_internal) (elf, locked);
+  ehdr = __elfw2(LIBELFBITS,getehdr_rdlock) (elf);
 
   /* Set the default values.  */
   if (ELFW(default_ehdr,LIBELFBITS) (elf, ehdr, shnum, change_bop) != 0)
@@ -153,7 +152,7 @@
   if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL
       && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN
 	  || ehdr->e_type == ET_CORE))
-    (void) __elfw2(LIBELFBITS,getphdr_internal) (elf, locked);
+    (void) __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
   if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
     {
       /* Only executables, shared objects, and core files have a program
@@ -207,7 +206,7 @@
       /* Load the section headers if necessary.  This loads the
 	 headers for all sections.  */
       if (list->data[1].shdr.ELFW(e,LIBELFBITS) == NULL)
-	(void) __elfw2(LIBELFBITS,getshdr_internal) (&list->data[1], locked);
+	(void) __elfw2(LIBELFBITS,getshdr_wrlock) (&list->data[1]);
 
       do
 	{
@@ -269,7 +268,7 @@
 				 scn->shdr_flags);
 
 	      if (scn->data_read == 0
-		  && __libelf_set_rawdata (scn, locked) != 0)
+		  && __libelf_set_rawdata_wrlock (scn) != 0)
 		/* Something went wrong.  The error value is already set.  */
 		return -1;
 
@@ -368,7 +367,7 @@
 		    {
 		      /* The position of the section in the file
 			 changed.  Create the section data list.  */
-		      if (__elf_getdata_internal (scn, NULL, locked) == NULL)
+		      if (__elf_getdata_rdlock (scn, NULL) == NULL)
 			return -1;
 		    }
 
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 1ce8c6a..b95b06b 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -1005,7 +1005,7 @@
 
   if (ref != NULL)
     /* Make sure the descriptor is not suddenly going away.  */
-    RWLOCK_RDLOCK (ref->lock);
+    rwlock_rdlock (ref->lock);
   else if (unlikely (fcntl (fildes, F_GETFL) == -1 && errno == EBADF))
     {
       /* We cannot do anything productive without a file descriptor.  */
@@ -1077,7 +1077,7 @@
 
   /* Release the lock.  */
   if (ref != NULL)
-    RWLOCK_UNLOCK (ref->lock);
+    rwlock_unlock (ref->lock);
 
   return retval;
 }
diff --git a/libelf/elf_clone.c b/libelf/elf_clone.c
index 3ace80d..8b699fa 100644
--- a/libelf/elf_clone.c
+++ b/libelf/elf_clone.c
@@ -68,7 +68,7 @@
     return NULL;
 
   /* Make sure the descriptor is not suddenly going away.  */
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   if (cmd != ELF_C_EMPTY)
     // XXX TODO handle ELF_C_READ/WRITE etc
@@ -96,7 +96,7 @@
 
   /* Release the lock.  */
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return retval;
 }
diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c
index 6c4fec3..fd5b47b 100644
--- a/libelf/elf_cntl.c
+++ b/libelf/elf_cntl.c
@@ -73,7 +73,7 @@
       return -1;
     }
 
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   switch (cmd)
     {
@@ -98,7 +98,7 @@
       break;
     }
 
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf_end.c b/libelf/elf_end.c
index 1329ea1..5112eae 100644
--- a/libelf/elf_end.c
+++ b/libelf/elf_end.c
@@ -71,13 +71,13 @@
     return 0;
 
   /* Make sure we are alone.  */
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   if (elf->ref_count != 0 && --elf->ref_count != 0)
     {
       /* Not yet the last activation.  */
       int result = elf->ref_count;
-      RWLOCK_UNLOCK (elf->lock);
+      rwlock_unlock (elf->lock);
       return result;
     }
 
@@ -106,9 +106,9 @@
 	 solve this problem by giving free the child lock.  The
 	 state of REF_COUNT==0 is handled all over the library, so
 	 this should be ok.  */
-      RWLOCK_UNLOCK (elf->lock);
-      RWLOCK_RDLOCK (parent->lock);
-      RWLOCK_WRLOCK (elf->lock);
+      rwlock_unlock (elf->lock);
+      rwlock_rdlock (parent->lock);
+      rwlock_wrlock (elf->lock);
 
       if (parent->state.ar.children == elf)
 	parent->state.ar.children = elf->next;
@@ -122,7 +122,7 @@
 	  child->next = elf->next;
 	}
 
-      RWLOCK_UNLOCK (parent->lock);
+      rwlock_unlock (parent->lock);
     }
 
   /* This was the last activation.  Free all resources.  */
diff --git a/libelf/elf_getarsym.c b/libelf/elf_getarsym.c
index 5959805..e6ecaad 100644
--- a/libelf/elf_getarsym.c
+++ b/libelf/elf_getarsym.c
@@ -94,7 +94,7 @@
   if (result == NULL)
     {
       /* We have not yet read the index.  */
-      RWLOCK_WRLOCK (elf->lock);
+      rwlock_wrlock (elf->lock);
 
       /* In case we find no index remember this for the next call.  */
       elf->state.ar.ar_sym = (Elf_Arsym *) -1l;
@@ -268,7 +268,7 @@
       result = elf->state.ar.ar_sym;
 
     out:
-      RWLOCK_UNLOCK (elf->lock);
+      rwlock_unlock (elf->lock);
     }
 
   if (ptr != NULL)
diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
index dc10a68..216905a 100644
--- a/libelf/elf_getdata.c
+++ b/libelf/elf_getdata.c
@@ -189,7 +189,7 @@
 /* Store the information for the raw data in the `rawdata' element.  */
 int
 internal_function
-__libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
+__libelf_set_rawdata_wrlock (Elf_Scn *scn)
 {
   size_t offset;
   size_t size;
@@ -200,7 +200,7 @@
   if (elf->class == ELFCLASS32)
     {
       Elf32_Shdr *shdr
-	= scn->shdr.e32 ?: __elf32_getshdr_internal (scn, locked);
+	= scn->shdr.e32 ?: __elf32_getshdr_wrlock (scn);
 
       if (shdr == NULL)
 	/* Something went terribly wrong.  */
@@ -214,7 +214,7 @@
   else
     {
       Elf64_Shdr *shdr
-	= scn->shdr.e64 ?: __elf64_getshdr_internal (scn, locked);
+	= scn->shdr.e64 ?: __elf64_getshdr_wrlock (scn);
 
       if (shdr == NULL)
 	/* Something went terribly wrong.  */
@@ -236,7 +236,7 @@
       if (type == SHT_HASH)
 	{
 	  GElf_Ehdr ehdr_mem;
-	  GElf_Ehdr *ehdr = __gelf_getehdr_internal (elf, &ehdr_mem, locked);
+	  GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
 	  entsize = SH_ENTSIZE_HASH (ehdr);
 	}
       else
@@ -313,7 +313,7 @@
   if (type == SHT_HASH && elf->class == ELFCLASS64)
     {
       GElf_Ehdr ehdr_mem;
-      GElf_Ehdr *ehdr = __gelf_getehdr_internal (elf, &ehdr_mem, locked);
+      GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
       scn->rawdata.d.d_type
 	= (SH_ENTSIZE_HASH (ehdr) == 4 ? ELF_T_WORD : ELF_T_XWORD);
     }
@@ -340,12 +340,27 @@
   return 0;
 }
 
+int
+internal_function
+__libelf_set_rawdata (Elf_Scn *scn)
+{
+  int result;
+
+  if (scn == NULL)
+    return 1;
+
+  rwlock_wrlock (scn->elf->lock);
+  result = __libelf_set_rawdata_wrlock (scn);
+  rwlock_unlock (scn->elf->lock);
+
+  return result;
+}
 
 Elf_Data *
-__elf_getdata_internal (scn, data, locked)
+internal_function
+__elf_getdata_rdlock (scn, data)
      Elf_Scn *scn;
      Elf_Data *data;
-     lockstat_t locked;
 {
   Elf_Data *result = NULL;
   Elf *elf;
@@ -362,9 +377,6 @@
   /* We will need this multiple times later on.  */
   elf = scn->elf;
 
-  if (locked == LS_UNLOCKED)
-    RWLOCK_RDLOCK (elf->lock);
-
   /* If `data' is not NULL this means we are not addressing the initial
      data in the file.  But this also means this data is already read
      (since otherwise it is not possible to have a valid `data' pointer)
@@ -417,12 +429,13 @@
          lock.  Therefore give up the read lock and then get the write
          lock.  But this means that the data could meanwhile be
          modified, therefore start the tests again.  */
-      rwlock_to_wrlock (LS_RDLOCKED, &elf->lock);
+      rwlock_unlock (elf->lock);
+      rwlock_wrlock (elf->lock);
 
       /* Read the data from the file.  There is always a file (or
 	 memory region) associated with this descriptor since
 	 otherwise the `data_read' flag would be set.  */
-      if (scn->data_read == 0 && __libelf_set_rawdata (scn, LS_WRLOCKED) != 0)
+      if (scn->data_read == 0 && __libelf_set_rawdata_wrlock (scn) != 0)
 	/* Something went wrong.  The error value is already set.  */
 	goto out;
     }
@@ -455,9 +468,6 @@
     result = &scn->data_list.data.d;
 
  out:
-  if (locked == LS_UNLOCKED)
-    RWLOCK_UNLOCK (elf->lock);
-
   return result;
 }
 
@@ -466,8 +476,15 @@
      Elf_Scn *scn;
      Elf_Data *data;
 {
+  Elf_Data *result;
+
   if (scn == NULL)
     return NULL;
 
-  return __elf_getdata_internal (scn, data, LS_UNLOCKED);
+  rwlock_rdlock (scn->elf->lock);
+  result = __elf_getdata_rdlock (scn, data);
+  rwlock_unlock (scn->elf->lock);
+
+  return result;
 }
+INTDEF(elf_getdata)
diff --git a/libelf/elf_getscn.c b/libelf/elf_getscn.c
index 5bcceb6..5c14a58 100644
--- a/libelf/elf_getscn.c
+++ b/libelf/elf_getscn.c
@@ -73,7 +73,7 @@
       return NULL;
     }
 
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   Elf_Scn *result = NULL;
 
@@ -103,7 +103,7 @@
 	}
     }
 
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf_getshnum.c b/libelf/elf_getshnum.c
index 10402d8..3c65ef4 100644
--- a/libelf/elf_getshnum.c
+++ b/libelf/elf_getshnum.c
@@ -60,10 +60,9 @@
 
 
 int
-__elf_getshnum_internal (elf, dst, locked)
+__elf_getshnum_rdlock (elf, dst)
      Elf *elf;
      size_t *dst;
-     lockstat_t locked;
 {
   int result = 0;
   int idx;
@@ -77,9 +76,6 @@
       return -1;
     }
 
-  if (locked == LS_UNLOCKED)
-    RWLOCK_RDLOCK (elf->lock);
-
   idx = elf->state.elf.scns_last->cnt;
   if (idx != 0
       || (elf->state.elf.scns_last
@@ -92,9 +88,6 @@
   else
     *dst = 0;
 
-  if (locked == LS_UNLOCKED)
-    RWLOCK_UNLOCK (elf->lock);
-
   return result;
 }
 
@@ -103,8 +96,14 @@
      Elf *elf;
      size_t *dst;
 {
+  int result;
+
   if (elf == NULL)
     return -1;
 
-  return __elf_getshnum_internal (elf, dst, LS_UNLOCKED);
+  rwlock_rdlock (elf->lock);
+  result = __elf_getshnum_rdlock (elf, dst);
+  rwlock_unlock (elf->lock);
+
+  return result;
 }
diff --git a/libelf/elf_getshstrndx.c b/libelf/elf_getshstrndx.c
index 9d0d27b..52516a8 100644
--- a/libelf/elf_getshstrndx.c
+++ b/libelf/elf_getshstrndx.c
@@ -79,7 +79,7 @@
       return -1;
     }
 
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   /* We rely here on the fact that the `elf' element is a common prefix
      of `elf32' and `elf64'.  */
@@ -196,7 +196,7 @@
     }
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf_newdata.c b/libelf/elf_newdata.c
index 1f42204..db17ea5 100644
--- a/libelf/elf_newdata.c
+++ b/libelf/elf_newdata.c
@@ -83,7 +83,7 @@
       return NULL;
     }
 
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   if (scn->data_read && scn->data_list_rear == NULL)
     {
@@ -122,7 +122,7 @@
   scn->data_list_rear = result;
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   /* Please note that the following is thread safe and is also defined
      for RESULT == NULL since it still return NULL.  */
diff --git a/libelf/elf_newscn.c b/libelf/elf_newscn.c
index da4ca72..aefab33 100644
--- a/libelf/elf_newscn.c
+++ b/libelf/elf_newscn.c
@@ -80,7 +80,7 @@
   assert (offsetof (Elf, state.elf32.scns)
 	  == offsetof (Elf, state.elf64.scns));
 
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
  again:
   if (elf->state.elf.scns_last->cnt < elf->state.elf.scns_last->max)
@@ -170,7 +170,7 @@
   result->flags |= ELF_F_DIRTY;
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf_nextscn.c b/libelf/elf_nextscn.c
index 0a21c76..a1842bb 100644
--- a/libelf/elf_nextscn.c
+++ b/libelf/elf_nextscn.c
@@ -69,7 +69,7 @@
   if (elf == NULL)
     return NULL;
 
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   if (scn == NULL)
     {
@@ -103,7 +103,7 @@
 	}
     }
 
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf_rawdata.c b/libelf/elf_rawdata.c
index b8be27c..b8bb0f4 100644
--- a/libelf/elf_rawdata.c
+++ b/libelf/elf_rawdata.c
@@ -88,7 +88,7 @@
       /* First thing we do is to read the data from the file.  There is
 	 always a file (or memory region) associated with this descriptor
 	 since otherwise the `data_read' flag would be set.  */
-      if (__libelf_set_rawdata (scn, LS_UNLOCKED) != 0)
+      if (__libelf_set_rawdata (scn) != 0)
 	/* Something went wrong.  The error value is already set.  */
 	return NULL;
     }
diff --git a/libelf/elf_readall.c b/libelf/elf_readall.c
index 71daea4..8f171b2 100644
--- a/libelf/elf_readall.c
+++ b/libelf/elf_readall.c
@@ -90,12 +90,12 @@
      Elf *elf;
 {
   /* Get the file.  */
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   if (elf->map_address == NULL && unlikely (elf->fildes == -1))
     {
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
-      RWLOCK_UNLOCK (elf->lock);
+      rwlock_unlock (elf->lock);
       return NULL;
     }
 
@@ -147,7 +147,7 @@
       libelf_release_all (elf);
     }
 
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return (char *) elf->map_address;
 }
diff --git a/libelf/elf_strptr.c b/libelf/elf_strptr.c
index 6fb0c50..35a0e9b 100644
--- a/libelf/elf_strptr.c
+++ b/libelf/elf_strptr.c
@@ -73,7 +73,7 @@
       return NULL;
     }
 
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   char *result = NULL;
   Elf_Scn *strscn;
@@ -140,10 +140,15 @@
 	}
     }
 
-  if (strscn->rawdata_base == NULL && ! strscn->data_read
-      /* Read the section data.  */
-      && __libelf_set_rawdata (strscn, LS_RDLOCKED) != 0)
-    goto out;
+  if (strscn->rawdata_base == NULL && ! strscn->data_read)
+    {
+      rwlock_unlock (elf->lock);
+      rwlock_wrlock (elf->lock);
+      if (strscn->rawdata_base == NULL && ! strscn->data_read
+	/* Read the section data.  */
+	  && __libelf_set_rawdata_wrlock (strscn) != 0)
+	goto out;
+    }
 
   if (likely (strscn->rawdata_base != NULL))
     result = &strscn->rawdata_base[offset];
@@ -166,7 +171,7 @@
     }
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/elf_update.c b/libelf/elf_update.c
index 8d605e0..5daece4 100644
--- a/libelf/elf_update.c
+++ b/libelf/elf_update.c
@@ -174,7 +174,7 @@
       return -1;
     }
 
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   /* Make sure we have an ELF header.  */
   if (elf->state.elf.ehdr == NULL)
@@ -193,8 +193,8 @@
      will come right after the ELF header.  The count the size of all
      sections and finally place the section table.  */
   size = (elf->class == ELFCLASS32
-	  ? __elf32_updatenull (elf, &change_bo, shnum, LS_WRLOCKED)
-	  : __elf64_updatenull (elf, &change_bo, shnum, LS_WRLOCKED));
+	  ? __elf32_updatenull_wrlock (elf, &change_bo, shnum)
+	  : __elf64_updatenull_wrlock (elf, &change_bo, shnum));
   if (likely (size != -1)
       /* See whether we actually have to write out the data.  */
       && (cmd == ELF_C_WRITE || cmd == ELF_C_WRITE_MMAP))
@@ -218,7 +218,7 @@
     }
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return size;
 }
diff --git a/libelf/gelf_getauxv.c b/libelf/gelf_getauxv.c
index 450e81b..036bdcc 100644
--- a/libelf/gelf_getauxv.c
+++ b/libelf/gelf_getauxv.c
@@ -79,7 +79,7 @@
 
   elf = data_scn->s->elf;
 
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   /* This is the one place where we have to take advantage of the fact
      that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
@@ -124,7 +124,7 @@
   result = dst;
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getdyn.c b/libelf/gelf_getdyn.c
index c032e5b..82f8e32 100644
--- a/libelf/gelf_getdyn.c
+++ b/libelf/gelf_getdyn.c
@@ -80,7 +80,7 @@
 
   elf = data_scn->s->elf;
 
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   /* This is the one place where we have to take advantage of the fact
      that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
@@ -126,7 +126,7 @@
   result = dst;
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getehdr.c b/libelf/gelf_getehdr.c
index ddc7707..34b5db3 100644
--- a/libelf/gelf_getehdr.c
+++ b/libelf/gelf_getehdr.c
@@ -61,10 +61,9 @@
 
 
 GElf_Ehdr *
-__gelf_getehdr_internal (elf, dest, locked)
+__gelf_getehdr_rdlock (elf, dest)
      Elf *elf;
      GElf_Ehdr *dest;
-     lockstat_t locked;
 {
   GElf_Ehdr *result = NULL;
 
@@ -77,9 +76,6 @@
       return NULL;
     }
 
-  if (locked == LS_UNLOCKED)
-    RWLOCK_RDLOCK (elf->lock);
-
   /* The following is an optimization: the ehdr element is at the same
      position in both the elf32 and elf64 structure.  */
   if (offsetof (struct Elf, state.elf32.ehdr)
@@ -116,9 +112,6 @@
   else
     result = memcpy (dest, elf->state.elf64.ehdr, sizeof (*dest));
 
-  if (locked == LS_UNLOCKED)
-    RWLOCK_UNLOCK (elf->lock);
-
   return result;
 }
 
@@ -127,8 +120,13 @@
      Elf *elf;
      GElf_Ehdr *dest;
 {
+  GElf_Ehdr *result;
   if (elf == NULL)
     return NULL;
 
-  return __gelf_getehdr_internal (elf, dest, LS_UNLOCKED);
+  rwlock_rdlock (elf->lock);
+  result = __gelf_getehdr_rdlock (elf, dest);
+  rwlock_unlock (elf->lock);
+
+  return result;
 }
diff --git a/libelf/gelf_getlib.c b/libelf/gelf_getlib.c
index 75a4a4a..aa91a73 100644
--- a/libelf/gelf_getlib.c
+++ b/libelf/gelf_getlib.c
@@ -76,7 +76,7 @@
 
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
 
-  RWLOCK_RDLOCK (data_scn->s->elf->lock);
+  rwlock_rdlock (data_scn->s->elf->lock);
 
   /* The on disk format of Elf32_Lib and Elf64_Lib is identical.  So
      we can simplify things significantly.  */
@@ -95,7 +95,7 @@
       result = dst;
     }
 
-  RWLOCK_UNLOCK (data_scn->s->elf->lock);
+  rwlock_unlock (data_scn->s->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getmove.c b/libelf/gelf_getmove.c
index 728c54e..6e76948 100644
--- a/libelf/gelf_getmove.c
+++ b/libelf/gelf_getmove.c
@@ -90,11 +90,11 @@
     }
 
   elf = ((Elf_Data_Scn *) data)->s->elf;
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   *dst = ((GElf_Move *) data->d_buf)[ndx];
 
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   result = dst;
 
diff --git a/libelf/gelf_getnote.c b/libelf/gelf_getnote.c
index 6867aeb..e490018 100644
--- a/libelf/gelf_getnote.c
+++ b/libelf/gelf_getnote.c
@@ -79,7 +79,7 @@
   assert (sizeof (GElf_Nhdr) == sizeof (Elf32_Nhdr));
   assert (sizeof (GElf_Nhdr) == sizeof (Elf64_Nhdr));
 
-  RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   /* The data is already in the correct form.  Just make sure the
      offset is OK.  */
@@ -113,7 +113,7 @@
 	}
     }
 
-  RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   return offset;
 }
diff --git a/libelf/gelf_getphdr.c b/libelf/gelf_getphdr.c
index 286b253..66cd143 100644
--- a/libelf/gelf_getphdr.c
+++ b/libelf/gelf_getphdr.c
@@ -54,6 +54,7 @@
 
 #include <gelf.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include "libelfP.h"
 
@@ -81,7 +82,7 @@
       return NULL;
     }
 
-  RWLOCK_RDLOCK (elf->lock);
+  rwlock_rdlock (elf->lock);
 
   if (elf->class == ELFCLASS32)
     {
@@ -90,10 +91,12 @@
 
       if (phdr == NULL)
 	{
-	  phdr = __elf32_getphdr_internal (elf, LS_RDLOCKED);
+	  rwlock_unlock (elf->lock);
+	  phdr = INTUSE(elf32_getphdr) (elf);
 	  if (phdr == NULL)
 	    /* The error number is already set.  */
-	    goto out;
+	    return NULL;
+	  rwlock_rdlock (elf->lock);
 	}
 
       /* Test whether the index is ok.  */
@@ -126,10 +129,12 @@
 
       if (phdr == NULL)
 	{
-	  phdr = __elf64_getphdr_internal (elf, LS_RDLOCKED);
+	  rwlock_unlock (elf->lock);
+	  phdr = INTUSE(elf64_getphdr) (elf);
 	  if (phdr == NULL)
 	    /* The error number is already set.  */
-	    goto out;
+	    return NULL;
+	  rwlock_rdlock (elf->lock);
 	}
 
       /* Test whether the index is ok.  */
@@ -144,7 +149,7 @@
     }
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getrel.c b/libelf/gelf_getrel.c
index 74a5595..f3775bd 100644
--- a/libelf/gelf_getrel.c
+++ b/libelf/gelf_getrel.c
@@ -88,7 +88,7 @@
      The interface is broken so that it requires this hack.  */
   scn = data_scn->s;
 
-  RWLOCK_RDLOCK (scn->elf->lock);
+  rwlock_rdlock (scn->elf->lock);
 
   if (scn->elf->class == ELFCLASS32)
     {
@@ -123,7 +123,7 @@
 			 sizeof (Elf64_Rel));
     }
 
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getrela.c b/libelf/gelf_getrela.c
index f62bd28..0275c37 100644
--- a/libelf/gelf_getrela.c
+++ b/libelf/gelf_getrela.c
@@ -88,7 +88,7 @@
      The interface is broken so that it requires this hack.  */
   scn = data_scn->s;
 
-  RWLOCK_RDLOCK (scn->elf->lock);
+  rwlock_rdlock (scn->elf->lock);
 
   if (scn->elf->class == ELFCLASS32)
     {
@@ -124,7 +124,7 @@
 			 sizeof (Elf64_Rela));
     }
 
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getshdr.c b/libelf/gelf_getshdr.c
index f5457a7..8b46dfa 100644
--- a/libelf/gelf_getshdr.c
+++ b/libelf/gelf_getshdr.c
@@ -74,13 +74,13 @@
       return NULL;
     }
 
-  RWLOCK_RDLOCK (scn->elf->lock);
+  rwlock_rdlock (scn->elf->lock);
 
   if (scn->elf->class == ELFCLASS32)
     {
       /* Copy the elements one-by-one.  */
       Elf32_Shdr *shdr
-	= scn->shdr.e32 ?: __elf32_getshdr_internal (scn, LS_RDLOCKED);
+	= scn->shdr.e32 ?: __elf32_getshdr_rdlock (scn);
 
       if (shdr == NULL)
 	{
@@ -106,7 +106,7 @@
   else
     {
       Elf64_Shdr *shdr
-	= scn->shdr.e64 ?: __elf64_getshdr_internal (scn, LS_RDLOCKED);
+	= scn->shdr.e64 ?: __elf64_getshdr_rdlock (scn);
 
       if (shdr == NULL)
 	{
@@ -119,7 +119,7 @@
     }
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getsym.c b/libelf/gelf_getsym.c
index 4f2a2be..162061f 100644
--- a/libelf/gelf_getsym.c
+++ b/libelf/gelf_getsym.c
@@ -77,7 +77,7 @@
       return NULL;
     }
 
-  RWLOCK_RDLOCK (data_scn->s->elf->lock);
+  rwlock_rdlock (data_scn->s->elf->lock);
 
   /* This is the one place where we have to take advantage of the fact
      that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
@@ -131,7 +131,7 @@
   result = dst;
 
  out:
-  RWLOCK_UNLOCK (data_scn->s->elf->lock);
+  rwlock_unlock (data_scn->s->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getsyminfo.c b/libelf/gelf_getsyminfo.c
index c5a312c..2c07526 100644
--- a/libelf/gelf_getsyminfo.c
+++ b/libelf/gelf_getsyminfo.c
@@ -80,7 +80,7 @@
   assert (sizeof (GElf_Syminfo) == sizeof (Elf32_Syminfo));
   assert (sizeof (GElf_Syminfo) == sizeof (Elf64_Syminfo));
 
-  RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
@@ -95,7 +95,7 @@
   result = dst;
 
  out:
-  RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getsymshndx.c b/libelf/gelf_getsymshndx.c
index 7fa0968..a9cfc18 100644
--- a/libelf/gelf_getsymshndx.c
+++ b/libelf/gelf_getsymshndx.c
@@ -84,7 +84,7 @@
       return NULL;
     }
 
-  RWLOCK_RDLOCK (symdata_scn->s->elf->lock);
+  rwlock_rdlock (symdata_scn->s->elf->lock);
 
   /* The user is not required to pass a data descriptor for an extended
      section index table.  */
@@ -155,7 +155,7 @@
   result = dst;
 
  out:
-  RWLOCK_UNLOCK (symdata_scn->s->elf->lock);
+  rwlock_unlock (symdata_scn->s->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getverdaux.c b/libelf/gelf_getverdaux.c
index df796a8..b8bcf14 100644
--- a/libelf/gelf_getverdaux.c
+++ b/libelf/gelf_getverdaux.c
@@ -81,7 +81,7 @@
   assert (sizeof (GElf_Verdaux) == sizeof (Elf32_Verdaux));
   assert (sizeof (GElf_Verdaux) == sizeof (Elf64_Verdaux));
 
-  RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
@@ -97,7 +97,7 @@
 				      sizeof (GElf_Verdaux));
 
 
-  RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getverdef.c b/libelf/gelf_getverdef.c
index babfa99..05cc2e8 100644
--- a/libelf/gelf_getverdef.c
+++ b/libelf/gelf_getverdef.c
@@ -81,7 +81,7 @@
   assert (sizeof (GElf_Verdef) == sizeof (Elf32_Verdef));
   assert (sizeof (GElf_Verdef) == sizeof (Elf64_Verdef));
 
-  RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
@@ -96,7 +96,7 @@
     result = (GElf_Verdef *) memcpy (dst, (char *) data->d_buf + offset,
 				     sizeof (GElf_Verdef));
 
-  RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getvernaux.c b/libelf/gelf_getvernaux.c
index 070e97e..45d3300 100644
--- a/libelf/gelf_getvernaux.c
+++ b/libelf/gelf_getvernaux.c
@@ -84,7 +84,7 @@
   assert (sizeof (GElf_Vernaux) == sizeof (Elf32_Vernaux));
   assert (sizeof (GElf_Vernaux) == sizeof (Elf64_Vernaux));
 
-  RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
@@ -99,7 +99,7 @@
     result = (GElf_Vernaux *) memcpy (dst, (char *) data->d_buf + offset,
 				      sizeof (GElf_Verneed));
 
-  RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getverneed.c b/libelf/gelf_getverneed.c
index 3e2f148..e82a055 100644
--- a/libelf/gelf_getverneed.c
+++ b/libelf/gelf_getverneed.c
@@ -84,7 +84,7 @@
   assert (sizeof (GElf_Verneed) == sizeof (Elf32_Vernaux));
   assert (sizeof (GElf_Verneed) == sizeof (Elf64_Vernaux));
 
-  RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
@@ -99,7 +99,7 @@
     result = (GElf_Verneed *) memcpy (dst, (char *) data->d_buf + offset,
 				      sizeof (GElf_Verneed));
 
-  RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+  rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_getversym.c b/libelf/gelf_getversym.c
index 011a54a..397b7ed 100644
--- a/libelf/gelf_getversym.c
+++ b/libelf/gelf_getversym.c
@@ -88,7 +88,7 @@
   assert (sizeof (GElf_Versym) == sizeof (Elf32_Versym));
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
 
-  RWLOCK_RDLOCK (scn->elf->lock);
+  rwlock_rdlock (scn->elf->lock);
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
@@ -104,7 +104,7 @@
       result = dst;
     }
 
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_auxv.c b/libelf/gelf_update_auxv.c
index dbcde82..b294ead 100644
--- a/libelf/gelf_update_auxv.c
+++ b/libelf/gelf_update_auxv.c
@@ -84,7 +84,7 @@
     }
 
   scn = data_scn->s;
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   if (scn->elf->class == ELFCLASS32)
     {
@@ -129,7 +129,7 @@
   scn->flags |= ELF_F_DIRTY;
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_dyn.c b/libelf/gelf_update_dyn.c
index fce00bb..10bfb6a 100644
--- a/libelf/gelf_update_dyn.c
+++ b/libelf/gelf_update_dyn.c
@@ -85,7 +85,7 @@
     }
 
   scn = data_scn->s;
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   if (scn->elf->class == ELFCLASS32)
     {
@@ -131,7 +131,7 @@
   scn->flags |= ELF_F_DIRTY;
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_ehdr.c b/libelf/gelf_update_ehdr.c
index 2da292c..4d5c2b6 100644
--- a/libelf/gelf_update_ehdr.c
+++ b/libelf/gelf_update_ehdr.c
@@ -72,7 +72,7 @@
       return 0;
     }
 
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   if (elf->class == ELFCLASS32)
     {
@@ -130,7 +130,7 @@
   result = 1;
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_lib.c b/libelf/gelf_update_lib.c
index 46409f3..9571016 100644
--- a/libelf/gelf_update_lib.c
+++ b/libelf/gelf_update_lib.c
@@ -83,7 +83,7 @@
     }
 
   Elf_Scn *scn = data_scn->s;
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   /* Check whether we have to resize the data buffer.  */
   int result = 0;
@@ -99,7 +99,7 @@
       scn->flags |= ELF_F_DIRTY;
     }
 
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_move.c b/libelf/gelf_update_move.c
index 11bc834..fd67be1 100644
--- a/libelf/gelf_update_move.c
+++ b/libelf/gelf_update_move.c
@@ -89,14 +89,14 @@
       return 0;
     }
 
-  RWLOCK_WRLOCK (data_scn->s->elf->lock);
+  rwlock_wrlock (data_scn->s->elf->lock);
 
   ((GElf_Move *) data_scn->d.d_buf)[ndx] = *src;
 
   /* Mark the section as modified.  */
   data_scn->s->flags |= ELF_F_DIRTY;
 
-  RWLOCK_UNLOCK (data_scn->s->elf->lock);
+  rwlock_unlock (data_scn->s->elf->lock);
 
   return 1;
 }
diff --git a/libelf/gelf_update_phdr.c b/libelf/gelf_update_phdr.c
index 3305acb..e8b7f78 100644
--- a/libelf/gelf_update_phdr.c
+++ b/libelf/gelf_update_phdr.c
@@ -72,7 +72,7 @@
       return 0;
     }
 
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   if (elf->class == ELFCLASS32)
     {
@@ -94,7 +94,7 @@
 
       if (phdr == NULL)
 	{
-	  phdr = __elf32_getphdr_internal (elf, LS_WRLOCKED);
+	  phdr = __elf32_getphdr_wrlock (elf);
 	  if (phdr == NULL)
 	    /* The error number is already set.  */
 	    goto out;
@@ -127,7 +127,7 @@
 
       if (phdr == NULL)
 	{
-	  phdr = __elf64_getphdr_internal (elf, LS_WRLOCKED);
+	  phdr = __elf64_getphdr_wrlock (elf);
 	  if (phdr == NULL)
 	    /* The error number is already set.  */
 	    goto out;
@@ -147,7 +147,7 @@
   result = 1;
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_rel.c b/libelf/gelf_update_rel.c
index b40ba27..049b71a 100644
--- a/libelf/gelf_update_rel.c
+++ b/libelf/gelf_update_rel.c
@@ -82,7 +82,7 @@
     }
 
   scn = data_scn->s;
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   if (scn->elf->class == ELFCLASS32)
     {
@@ -129,7 +129,7 @@
   scn->flags |= ELF_F_DIRTY;
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_rela.c b/libelf/gelf_update_rela.c
index 86553bd..dc99c68 100644
--- a/libelf/gelf_update_rela.c
+++ b/libelf/gelf_update_rela.c
@@ -82,7 +82,7 @@
     }
 
   scn = data_scn->s;
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   if (scn->elf->class == ELFCLASS32)
     {
@@ -132,7 +132,7 @@
   scn->flags |= ELF_F_DIRTY;
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_shdr.c b/libelf/gelf_update_shdr.c
index dccf6ad..c57eab8 100644
--- a/libelf/gelf_update_shdr.c
+++ b/libelf/gelf_update_shdr.c
@@ -68,12 +68,12 @@
     return 0;
 
   elf = scn->elf;
-  RWLOCK_WRLOCK (elf->lock);
+  rwlock_wrlock (elf->lock);
 
   if (elf->class == ELFCLASS32)
     {
       Elf32_Shdr *shdr
-	= scn->shdr.e32 ?: __elf32_getshdr_internal (scn, LS_WRLOCKED);
+	= scn->shdr.e32 ?: __elf32_getshdr_wrlock (scn);
 
       if (shdr == NULL)
 	{
@@ -108,7 +108,7 @@
   else
     {
       Elf64_Shdr *shdr
-	= scn->shdr.e64 ?: __elf64_getshdr_internal (scn, LS_WRLOCKED);
+	= scn->shdr.e64 ?: __elf64_getshdr_wrlock (scn);
 
       if (shdr == NULL)
 	{
@@ -123,7 +123,7 @@
   result = 1;
 
  out:
-  RWLOCK_UNLOCK (elf->lock);
+  rwlock_unlock (elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_sym.c b/libelf/gelf_update_sym.c
index d484e03..fff45fd 100644
--- a/libelf/gelf_update_sym.c
+++ b/libelf/gelf_update_sym.c
@@ -86,7 +86,7 @@
     }
 
   scn = data_scn->s;
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   if (scn->elf->class == ELFCLASS32)
     {
@@ -140,7 +140,7 @@
   scn->flags |= ELF_F_DIRTY;
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_syminfo.c b/libelf/gelf_update_syminfo.c
index 83e92ce..a0e176d 100644
--- a/libelf/gelf_update_syminfo.c
+++ b/libelf/gelf_update_syminfo.c
@@ -90,7 +90,7 @@
   assert (sizeof (GElf_Syminfo) == sizeof (Elf64_Syminfo));
 
   scn = data_scn->s;
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   /* Check whether we have to resize the data buffer.  */
   if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
@@ -107,7 +107,7 @@
   scn->flags |= ELF_F_DIRTY;
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_symshndx.c b/libelf/gelf_update_symshndx.c
index 5931b6b..2d393c1 100644
--- a/libelf/gelf_update_symshndx.c
+++ b/libelf/gelf_update_symshndx.c
@@ -93,7 +93,7 @@
   scn = symdata_scn->s;
   /* We simply have to believe the user that the two sections belong to
      the same ELF file.  */
-  RWLOCK_WRLOCK (scn->elf->lock);
+  rwlock_wrlock (scn->elf->lock);
 
   /* The user is not required to pass a data descriptor for an extended
      section index table.  */
@@ -170,7 +170,7 @@
   scn->flags |= ELF_F_DIRTY;
 
  out:
-  RWLOCK_UNLOCK (scn->elf->lock);
+  rwlock_unlock (scn->elf->lock);
 
   return result;
 }
diff --git a/libelf/gelf_update_verdaux.c b/libelf/gelf_update_verdaux.c
index f1875c2..17866ef 100644
--- a/libelf/gelf_update_verdaux.c
+++ b/libelf/gelf_update_verdaux.c
@@ -89,14 +89,14 @@
       return 0;
     }
 
-  RWLOCK_WRLOCK (data_scn->s->elf->lock);
+  rwlock_wrlock (data_scn->s->elf->lock);
 
   memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verdaux));
 
   /* Mark the section as modified.  */
   data_scn->s->flags |= ELF_F_DIRTY;
 
-  RWLOCK_UNLOCK (data_scn->s->elf->lock);
+  rwlock_unlock (data_scn->s->elf->lock);
 
   return 1;
 }
diff --git a/libelf/gelf_update_verdef.c b/libelf/gelf_update_verdef.c
index 94c805b..cd19cb7 100644
--- a/libelf/gelf_update_verdef.c
+++ b/libelf/gelf_update_verdef.c
@@ -89,14 +89,14 @@
       return 0;
     }
 
-  RWLOCK_WRLOCK (data_scn->s->elf->lock);
+  rwlock_wrlock (data_scn->s->elf->lock);
 
   memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verdef));
 
   /* Mark the section as modified.  */
   data_scn->s->flags |= ELF_F_DIRTY;
 
-  RWLOCK_UNLOCK (data_scn->s->elf->lock);
+  rwlock_unlock (data_scn->s->elf->lock);
 
   return 1;
 }
diff --git a/libelf/gelf_update_vernaux.c b/libelf/gelf_update_vernaux.c
index c7a341a..2be69e1 100644
--- a/libelf/gelf_update_vernaux.c
+++ b/libelf/gelf_update_vernaux.c
@@ -89,14 +89,14 @@
       return 0;
     }
 
-  RWLOCK_WRLOCK (data_scn->s->elf->lock);
+  rwlock_wrlock (data_scn->s->elf->lock);
 
   memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Vernaux));
 
   /* Mark the section as modified.  */
   data_scn->s->flags |= ELF_F_DIRTY;
 
-  RWLOCK_UNLOCK (data_scn->s->elf->lock);
+  rwlock_unlock (data_scn->s->elf->lock);
 
   return 1;
 }
diff --git a/libelf/gelf_update_verneed.c b/libelf/gelf_update_verneed.c
index ae23fdb..95f2d18 100644
--- a/libelf/gelf_update_verneed.c
+++ b/libelf/gelf_update_verneed.c
@@ -89,14 +89,14 @@
       return 0;
     }
 
-  RWLOCK_WRLOCK (data_scn->s->elf->lock);
+  rwlock_wrlock (data_scn->s->elf->lock);
 
   memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verneed));
 
   /* Mark the section as modified.  */
   data_scn->s->flags |= ELF_F_DIRTY;
 
-  RWLOCK_UNLOCK (data_scn->s->elf->lock);
+  rwlock_unlock (data_scn->s->elf->lock);
 
   return 1;
 }
diff --git a/libelf/gelf_update_versym.c b/libelf/gelf_update_versym.c
index c2ec6e9..905c8f7 100644
--- a/libelf/gelf_update_versym.c
+++ b/libelf/gelf_update_versym.c
@@ -89,14 +89,14 @@
       return 0;
     }
 
-  RWLOCK_WRLOCK (data_scn->s->elf->lock);
+  rwlock_wrlock (data_scn->s->elf->lock);
 
   ((GElf_Versym *) data_scn->d.d_buf)[ndx] = *src;
 
   /* Mark the section as modified.  */
   data_scn->s->flags |= ELF_F_DIRTY;
 
-  RWLOCK_UNLOCK (data_scn->s->elf->lock);
+  rwlock_unlock (data_scn->s->elf->lock);
 
   return 1;
 }
diff --git a/libelf/libelfP.h b/libelf/libelfP.h
index c90e895..1dc331f 100644
--- a/libelf/libelfP.h
+++ b/libelf/libelfP.h
@@ -418,16 +418,6 @@
   /* There absolutely never must be anything following the union.  */
 };
 
-/* For _locked calls.  This gives the callee insight into how is the
-   object locked.  Some functions, e.g. elf32_getshdr, are called from
-   both callers of both rwlock_wrlock and rwlock_rdlock. */
-typedef enum
-{
-  LS_UNLOCKED = 0,
-  LS_RDLOCKED,
-  LS_WRLOCKED
-} lockstat_t;
-
 /* Type of the conversion functions.  These functions will convert the
    byte order.  */
 typedef void (*xfct_t) (void *, const void *, size_t, int);
@@ -505,17 +495,15 @@
 extern int __libelf_readsections (Elf *elf) internal_function;
 
 /* Store the information for the raw data in the `rawdata_list' element.  */
-extern int __libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
-  internal_function;
+extern int __libelf_set_rawdata (Elf_Scn *scn) internal_function;
+extern int __libelf_set_rawdata_wrlock (Elf_Scn *scn) internal_function;
 
 
 /* Helper functions for elf_update.  */
-extern off_t __elf32_updatenull (Elf *elf, int *change_bop, size_t shnum,
-				 lockstat_t locked)
-     internal_function;
-extern off_t __elf64_updatenull (Elf *elf, int *change_bop, size_t shnum,
-				 lockstat_t locked)
-     internal_function;
+extern off_t __elf32_updatenull_wrlock (Elf *elf, int *change_bop,
+					size_t shnum) internal_function;
+extern off_t __elf64_updatenull_wrlock (Elf *elf, int *change_bop,
+					size_t shnum) internal_function;
 
 extern int __elf32_updatemmap (Elf *elf, int change_bo, size_t shnum)
      internal_function;
@@ -527,20 +515,19 @@
      internal_function;
 
 
-/* Alias for exported functions to avoid PLT entries.  */
-extern int __elf_end_internal (Elf *__elf);
+/* Alias for exported functions to avoid PLT entries, and
+   rdlock/wrlock variants of these functions.  */
+extern int __elf_end_internal (Elf *__elf) attribute_hidden;
 extern Elf *__elf_begin_internal (int __fildes, Elf_Cmd __cmd, Elf *__ref)
      attribute_hidden;
-extern Elf32_Ehdr *__elf32_getehdr_internal (Elf *__elf, lockstat_t locked)
-  attribute_hidden;
-extern Elf64_Ehdr *__elf64_getehdr_internal (Elf *__elf, lockstat_t locked)
-  attribute_hidden;
+extern Elf32_Ehdr *__elf32_getehdr_rdlock (Elf *__elf) internal_function;
+extern Elf64_Ehdr *__elf64_getehdr_rdlock (Elf *__elf) internal_function;
 extern Elf32_Ehdr *__elf32_newehdr_internal (Elf *__elf) attribute_hidden;
 extern Elf64_Ehdr *__elf64_newehdr_internal (Elf *__elf) attribute_hidden;
-extern Elf32_Phdr *__elf32_getphdr_internal (Elf *__elf, lockstat_t locked)
-     attribute_hidden;
-extern Elf64_Phdr *__elf64_getphdr_internal (Elf *__elf, lockstat_t locked)
-     attribute_hidden;
+extern Elf32_Phdr *__elf32_getphdr_internal (Elf *__elf) attribute_hidden;
+extern Elf64_Phdr *__elf64_getphdr_internal (Elf *__elf) attribute_hidden;
+extern Elf32_Phdr *__elf32_getphdr_wrlock (Elf *__elf) attribute_hidden;
+extern Elf64_Phdr *__elf64_getphdr_wrlock (Elf *__elf) attribute_hidden;
 extern Elf32_Phdr *__elf32_newphdr_internal (Elf *__elf, size_t __cnt)
      attribute_hidden;
 extern Elf64_Phdr *__elf64_newphdr_internal (Elf *__elf, size_t __cnt)
@@ -549,23 +536,22 @@
   attribute_hidden;
 extern Elf_Scn *__elf64_offscn_internal (Elf *__elf, Elf64_Off __offset)
   attribute_hidden;
-extern int __elf_getshnum_internal (Elf *__elf, size_t *__dst,
-				    lockstat_t locked)
-     attribute_hidden;
+extern int __elf_getshnum_rdlock (Elf *__elf, size_t *__dst) internal_function;
 extern int __elf_getshstrndx_internal (Elf *__elf, size_t *__dst)
      attribute_hidden;
-extern Elf32_Shdr *__elf32_getshdr_internal (Elf_Scn *__scn, lockstat_t locked)
-     attribute_hidden;
-extern Elf64_Shdr *__elf64_getshdr_internal (Elf_Scn *__scn, lockstat_t locked)
-     attribute_hidden;
+extern Elf32_Shdr *__elf32_getshdr_rdlock (Elf_Scn *__scn) internal_function;
+extern Elf64_Shdr *__elf64_getshdr_rdlock (Elf_Scn *__scn) internal_function;
+extern Elf32_Shdr *__elf32_getshdr_wrlock (Elf_Scn *__scn) internal_function;
+extern Elf64_Shdr *__elf64_getshdr_wrlock (Elf_Scn *__scn) internal_function;
 extern Elf_Scn *__elf_getscn_internal (Elf *__elf, size_t __index)
      attribute_hidden;
 extern Elf_Scn *__elf_nextscn_internal (Elf *__elf, Elf_Scn *__scn)
      attribute_hidden;
 extern int __elf_scnshndx_internal (Elf_Scn *__scn) attribute_hidden;
-extern Elf_Data *__elf_getdata_internal (Elf_Scn *__scn, Elf_Data *__data,
-					 lockstat_t locked)
+extern Elf_Data *__elf_getdata_internal (Elf_Scn *__scn, Elf_Data *__data)
      attribute_hidden;
+extern Elf_Data *__elf_getdata_rdlock (Elf_Scn *__scn, Elf_Data *__data)
+     internal_function;
 extern Elf_Data *__elf_rawdata_internal (Elf_Scn *__scn, Elf_Data *__data)
      attribute_hidden;
 extern char *__elf_strptr_internal (Elf *__elf, size_t __index,
@@ -594,9 +580,8 @@
 extern long int __elf64_checksum_internal (Elf *__elf) attribute_hidden;
 
 
-extern GElf_Ehdr *__gelf_getehdr_internal (Elf *__elf, GElf_Ehdr *__dest,
-					   lockstat_t locked)
-     attribute_hidden;
+extern GElf_Ehdr *__gelf_getehdr_rdlock (Elf *__elf, GElf_Ehdr *__dest)
+     internal_function;
 extern size_t __gelf_fsize_internal (Elf *__elf, Elf_Type __type,
 				     size_t __count, unsigned int __version)
      attribute_hidden;
@@ -626,34 +611,4 @@
 /* Align offset to 4 bytes as needed for note name and descriptor data.  */
 #define NOTE_ALIGN(n)	(((n) + 3) & -4U)
 
-#if defined NDEBUG || !defined USE_TLS
-# define LIBELF_CHECKED_LOCK(V, S) ((void)(V))
-#else
-/* Checked locking primitives.  Prints out an error to stderr if the
-   locking or unlocking function returns an error code.  A development
-   aid similar to assert, the user is not supposed to ever see any of
-   these.  */
-# if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
-#  error The XSI-compliant version of strerror_r() is provided, but in following, \
-  the GNU version is assumed.  Please rewrite.
-# endif
-
-# define LIBELF_CHECKED_LOCK(V, S)				\
-  do {								\
-    int err = (V);						\
-    if (err != 0)						\
-      {								\
-	char __buf[128];					\
-	char *__ptr = strerror_r (err, __buf, sizeof __buf);	\
-	fprintf (stderr, "%s:%d: %s: %s\n",			\
-		 __FILE__, __LINE__, (S), __ptr);		\
-      }								\
-  } while (0)
-
-#endif
-
-#define RWLOCK_RDLOCK(LOCK) LIBELF_CHECKED_LOCK (rwlock_rdlock (LOCK), "rwlock_rdlock")
-#define RWLOCK_WRLOCK(LOCK) LIBELF_CHECKED_LOCK (rwlock_wrlock (LOCK), "rwlock_wrlock")
-#define RWLOCK_UNLOCK(LOCK) LIBELF_CHECKED_LOCK (rwlock_unlock (LOCK), "rwlock_unlock")
-
 #endif  /* libelfP.h */
diff --git a/libelf/nlist.c b/libelf/nlist.c
index de18b8f..f1fe176 100644
--- a/libelf/nlist.c
+++ b/libelf/nlist.c
@@ -141,7 +141,7 @@
   /* SHDR->SH_LINK now contains the index of the string section.  */
 
   /* Get the data for the symbol section.  */
-  data = __elf_getdata_internal (symscn, NULL, LS_UNLOCKED);
+  data = INTUSE(elf_getdata) (symscn, NULL);
   if (data == NULL)
     goto fail_close;