diff mbox series

elf: Fix elf_get_dynamic_info definition

Message ID 20211011191859.509946-1-adhemerval.zanella@linaro.org
State Committed
Commit 4af6982e4c9fc465ffb7a54b794aaaa134241f05
Headers show
Series elf: Fix elf_get_dynamic_info definition | expand

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Adhemerval Zanella Oct. 11, 2021, 7:18 p.m. UTC
Before 490e6c62aa31a8a ('elf: Avoid nested functions in the loader
[BZ #27220]') elf_get_dynamic_info were defined as 'auto' for
!RESOLVE_MAP (the default case).  Also, the function adds extra asserts
depending on whether is defined or not RTLD_BOOTSTRAP.

With the commit change, the function is now included and defined once
instead of defined as a nested function.  So rtld.c defines it when
including 'dynamic-link.h' without defining RTLD_BOOTSTRAP and it
brokes at least powerpc.

This patch fixes by defining iff get-dynamic-info.h, which then the
caller can corectly set the expected semantic by STATIC_PIE_BOOTSTRAP,
RTLD_BOOTSTRAP, and/or RESOLVE_MAP.

It also requires to enable some asserts only for the loader bootstrap
since the function is now define once.

As a side note, this is another issues with nested functions: it is
not clear from pre-processed output (-E -dD) how the function will
be build and its semantic (since nested function will be local and
extra C defines may change it).

I checked on x86_64-linux-gnu (w/o --enable-static-pie),
i686-linux-gnu, powerpc64-linux-gnu, powerpc-linux-gnu-power4,
aarch64-linux-gnu, arm-linux-gnu, sparc64-linux-gnu, and
s390x-linux-gnu.
---
 elf/dl-conflict.c           |  1 +
 elf/dl-load.c               |  3 ++-
 elf/dl-reloc-static-pie.c   |  3 ++-
 elf/dl-runtime.c            |  1 +
 elf/dynamic-link.h          |  1 -
 elf/get-dynamic-info.h      | 25 ++++++++++++++-----------
 elf/rtld.c                  |  5 +++--
 elf/setup-vdso.h            |  2 +-
 sysdeps/arm/dl-machine.h    |  1 +
 sysdeps/i386/dl-machine.h   |  1 +
 sysdeps/x86_64/dl-machine.h |  1 +
 11 files changed, 27 insertions(+), 17 deletions(-)

Comments

Fangrui Song Oct. 11, 2021, 7:55 p.m. UTC | #1
On 2021-10-11, Adhemerval Zanella wrote:
>Before 490e6c62aa31a8a ('elf: Avoid nested functions in the loader
>[BZ #27220]') elf_get_dynamic_info were defined as 'auto' for
>!RESOLVE_MAP (the default case).  Also, the function adds extra asserts
>depending on whether is defined or not RTLD_BOOTSTRAP.
>
>With the commit change, the function is now included and defined once
>instead of defined as a nested function.  So rtld.c defines it when
>including 'dynamic-link.h' without defining RTLD_BOOTSTRAP and it
>brokes at least powerpc.
>
>This patch fixes by defining iff get-dynamic-info.h, which then the
>caller can corectly set the expected semantic by STATIC_PIE_BOOTSTRAP,
>RTLD_BOOTSTRAP, and/or RESOLVE_MAP.
>
>It also requires to enable some asserts only for the loader bootstrap
>since the function is now define once.
>
>As a side note, this is another issues with nested functions: it is
>not clear from pre-processed output (-E -dD) how the function will
>be build and its semantic (since nested function will be local and
>extra C defines may change it).
>
>I checked on x86_64-linux-gnu (w/o --enable-static-pie),
>i686-linux-gnu, powerpc64-linux-gnu, powerpc-linux-gnu-power4,
>aarch64-linux-gnu, arm-linux-gnu, sparc64-linux-gnu, and
>s390x-linux-gnu.

Thank you for the fix!

hppa and ia64 build as well.

Reviewed-by: Fangrui Song <maskray@google.com>

>---
> elf/dl-conflict.c           |  1 +
> elf/dl-load.c               |  3 ++-
> elf/dl-reloc-static-pie.c   |  3 ++-
> elf/dl-runtime.c            |  1 +
> elf/dynamic-link.h          |  1 -
> elf/get-dynamic-info.h      | 25 ++++++++++++++-----------
> elf/rtld.c                  |  5 +++--
> elf/setup-vdso.h            |  2 +-
> sysdeps/arm/dl-machine.h    |  1 +
> sysdeps/i386/dl-machine.h   |  1 +
> sysdeps/x86_64/dl-machine.h |  1 +
> 11 files changed, 27 insertions(+), 17 deletions(-)
>
>diff --git a/elf/dl-conflict.c b/elf/dl-conflict.c
>index b58929cc5d..fb501e0373 100644
>--- a/elf/dl-conflict.c
>+++ b/elf/dl-conflict.c
>@@ -16,6 +16,7 @@
>    License along with the GNU C Library; see the file COPYING.LIB.  If
>    not, see <https://www.gnu.org/licenses/>.  */
>
>+#include <assert.h>
> #include <errno.h>
> #include <libintl.h>
> #include <stdlib.h>
>diff --git a/elf/dl-load.c b/elf/dl-load.c
>index 99c5ab83b3..335c79fad5 100644
>--- a/elf/dl-load.c
>+++ b/elf/dl-load.c
>@@ -58,6 +58,7 @@ struct filebuf
> };
>
> #include "dynamic-link.h"
>+#include "get-dynamic-info.h"
> #include <abi-tag.h>
> #include <stackinfo.h>
> #include <sysdep.h>
>@@ -1297,7 +1298,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>   else
>     l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
>
>-  elf_get_dynamic_info (l);
>+  elf_get_dynamic_info (l, false);
>
>   /* Make sure we are not dlopen'ing an object that has the
>      DF_1_NOOPEN flag set, or a PIE object.  */
>diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c
>index 4007580453..8a99c4995b 100644
>--- a/elf/dl-reloc-static-pie.c
>+++ b/elf/dl-reloc-static-pie.c
>@@ -28,6 +28,7 @@
> #define STATIC_PIE_BOOTSTRAP
> #define RESOLVE_MAP(map, scope, sym, version, flags) map
> #include "dynamic-link.h"
>+#include "get-dynamic-info.h"
>
> /* Relocate static executable with PIE.  */
>
>@@ -51,7 +52,7 @@ _dl_relocate_static_pie (void)
> 	break;
>       }
>
>-  elf_get_dynamic_info (main_map);
>+  elf_get_dynamic_info (main_map, false);
>
> # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
>   ELF_MACHINE_BEFORE_RTLD_RELOC (main_map, main_map->l_info);
>diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
>index 9d0d941000..61c260ddb8 100644
>--- a/elf/dl-runtime.c
>+++ b/elf/dl-runtime.c
>@@ -19,6 +19,7 @@
> #define IN_DL_RUNTIME 1		/* This can be tested in dl-machine.h.  */
>
> #include <alloca.h>
>+#include <assert.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/param.h>
>diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h
>index 7cc3021164..21cdfc88bb 100644
>--- a/elf/dynamic-link.h
>+++ b/elf/dynamic-link.h
>@@ -93,7 +93,6 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
>
> #include <dl-machine.h>
>
>-#include "get-dynamic-info.h"
>
> #ifdef RESOLVE_MAP
>
>diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h
>index 15c316b38c..d169099fbc 100644
>--- a/elf/get-dynamic-info.h
>+++ b/elf/get-dynamic-info.h
>@@ -25,7 +25,7 @@
> #include <libc-diag.h>
>
> static inline void __attribute__ ((unused, always_inline))
>-elf_get_dynamic_info (struct link_map *l)
>+elf_get_dynamic_info (struct link_map *l, bool check)
> {
> #if __ELF_NATIVE_CLASS == 32
>   typedef Elf32_Word d_tag_utype;
>@@ -112,16 +112,19 @@ elf_get_dynamic_info (struct link_map *l)
>     assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
> #endif
> #ifdef RTLD_BOOTSTRAP
>-  /* Only the bind now flags are allowed.  */
>-  assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
>-	  || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
>-  /* Flags must not be set for ld.so.  */
>-  assert (info[DT_FLAGS] == NULL
>-	  || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
>-#endif
>-#if defined RTLD_BOOTSTRAP || defined STATIC_PIE_BOOTSTRAP
>-  assert (info[DT_RUNPATH] == NULL);
>-  assert (info[DT_RPATH] == NULL);
>+  if (check)
>+    {
>+      /* Only the bind now flags are allowed.  */
>+      assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
>+	      || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
>+      /* Flags must not be set for ld.so.  */
>+      assert (info[DT_FLAGS] == NULL
>+	      || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
>+# ifdef STATIC_PIE_BOOTSTRAP
>+      assert (info[DT_RUNPATH] == NULL);
>+      assert (info[DT_RPATH] == NULL);
>+# endif
>+    }
> #else
>   if (info[DT_FLAGS] != NULL)
>     {
>diff --git a/elf/rtld.c b/elf/rtld.c
>index 5eee9e1091..fe5ee9ec4d 100644
>--- a/elf/rtld.c
>+++ b/elf/rtld.c
>@@ -513,6 +513,7 @@ _dl_start_final (void *arg, struct dl_start_final_info *info)
>      is trivial: always the map of ld.so itself.  */
> #define RTLD_BOOTSTRAP
> #define RESOLVE_MAP(map, scope, sym, version, flags) map
>+#include "get-dynamic-info.h"
> #include "dynamic-link.h"
>
> static ElfW(Addr) __attribute_used__
>@@ -547,7 +548,7 @@ _dl_start (void *arg)
>
>   /* Read our own dynamic section and fill in the info array.  */
>   bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
>-  elf_get_dynamic_info (&bootstrap_map);
>+  elf_get_dynamic_info (&bootstrap_map, true);
>
> #if NO_TLS_OFFSET != 0
>   bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
>@@ -1615,7 +1616,7 @@ dl_main (const ElfW(Phdr) *phdr,
>   if (! rtld_is_main)
>     {
>       /* Extract the contents of the dynamic section for easy access.  */
>-      elf_get_dynamic_info (main_map);
>+      elf_get_dynamic_info (main_map, false);
>
>       /* If the main map is libc.so, update the base namespace to
> 	 refer to this map.  If libc.so is loaded later, this happens
>diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h
>index f44748bc98..6fdffafcca 100644
>--- a/elf/setup-vdso.h
>+++ b/elf/setup-vdso.h
>@@ -64,7 +64,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)),
>       l->l_map_end += l->l_addr;
>       l->l_text_end += l->l_addr;
>       l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
>-      elf_get_dynamic_info (l);
>+      elf_get_dynamic_info (l, false);
>       _dl_setup_hash (l);
>       l->l_relocated = 1;
>
>diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h
>index 68a0b4b476..dfa05eee44 100644
>--- a/sysdeps/arm/dl-machine.h
>+++ b/sysdeps/arm/dl-machine.h
>@@ -21,6 +21,7 @@
>
> #define ELF_MACHINE_NAME "ARM"
>
>+#include <assert.h>
> #include <sys/param.h>
> #include <tls.h>
> #include <dl-tlsdesc.h>
>diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
>index 9f578e536a..78bed03eee 100644
>--- a/sysdeps/i386/dl-machine.h
>+++ b/sysdeps/i386/dl-machine.h
>@@ -21,6 +21,7 @@
>
> #define ELF_MACHINE_NAME "i386"
>
>+#include <assert.h>
> #include <sys/param.h>
> #include <sysdep.h>
> #include <tls.h>
>diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h
>index 9c0182d597..20afb0b4bc 100644
>--- a/sysdeps/x86_64/dl-machine.h
>+++ b/sysdeps/x86_64/dl-machine.h
>@@ -21,6 +21,7 @@
>
> #define ELF_MACHINE_NAME "x86_64"
>
>+#include <assert.h>
> #include <sys/param.h>
> #include <sysdep.h>
> #include <tls.h>
>-- 
>2.30.2
>
Florian Weimer Oct. 12, 2021, 6:35 a.m. UTC | #2
* Adhemerval Zanella via Libc-alpha:

> +  if (check)
> +    {
> +      /* Only the bind now flags are allowed.  */
> +      assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
> +	      || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
> +      /* Flags must not be set for ld.so.  */
> +      assert (info[DT_FLAGS] == NULL
> +	      || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
> +# ifdef STATIC_PIE_BOOTSTRAP
> +      assert (info[DT_RUNPATH] == NULL);
> +      assert (info[DT_RPATH] == NULL);
> +# endif
> +    }

Why is a conditional check required?  Is this an optimization?
Adhemerval Zanella Oct. 12, 2021, 11:34 a.m. UTC | #3
On 12/10/2021 03:35, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
> 
>> +  if (check)
>> +    {
>> +      /* Only the bind now flags are allowed.  */
>> +      assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
>> +	      || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
>> +      /* Flags must not be set for ld.so.  */
>> +      assert (info[DT_FLAGS] == NULL
>> +	      || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
>> +# ifdef STATIC_PIE_BOOTSTRAP
>> +      assert (info[DT_RUNPATH] == NULL);
>> +      assert (info[DT_RPATH] == NULL);
>> +# endif
>> +    }
> 
> Why is a conditional check required?  Is this an optimization?
> 

Because these asserts are only meaningful for the loader itself, but 
this function is used by setup_vdso() as well.  Enabling it regardless
leads to:

--
$ cat test.c
int main (int argc, char *argv[])
{
  return 0;
}
$ gcc -Wall test.c 
$ ./testrun.sh ./a.out 
Inconsistency detected by ld.so: get-dynamic-info.h: 121: elf_get_dynamic_info: Assertion `info[DT_FLAGS] == NULL || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0' failed!
--

Previously to 490e6c62aa31a8a, elf_get_dynamic_info() was defined twice
on rtld.c: first with the "dynamic-link.h" include at line 35 and later
at line 519 within _dl_start.  The former definition did not define
DONT_USE_BOOTSTRAP_MAP and it is used on setup_vdso() (since it is a
global definition), while the former does define DONT_USE_BOOTSTRAP_MAP
and it is used on loader self-relocation.

For rtld.c we must define DONT_USE_BOOTSTRAP_MAP, so I added this test
to handle setup_vdso().

This is another complexity that nested function with 'auto' brings,
which only make the code more convoluted.
Adhemerval Zanella Oct. 12, 2021, 11:35 a.m. UTC | #4
On 11/10/2021 16:55, Fangrui Song wrote:
> On 2021-10-11, Adhemerval Zanella wrote:
>> Before 490e6c62aa31a8a ('elf: Avoid nested functions in the loader
>> [BZ #27220]') elf_get_dynamic_info were defined as 'auto' for
>> !RESOLVE_MAP (the default case).  Also, the function adds extra asserts
>> depending on whether is defined or not RTLD_BOOTSTRAP.
>>
>> With the commit change, the function is now included and defined once
>> instead of defined as a nested function.  So rtld.c defines it when
>> including 'dynamic-link.h' without defining RTLD_BOOTSTRAP and it
>> brokes at least powerpc.
>>
>> This patch fixes by defining iff get-dynamic-info.h, which then the
>> caller can corectly set the expected semantic by STATIC_PIE_BOOTSTRAP,
>> RTLD_BOOTSTRAP, and/or RESOLVE_MAP.
>>
>> It also requires to enable some asserts only for the loader bootstrap
>> since the function is now define once.
>>
>> As a side note, this is another issues with nested functions: it is
>> not clear from pre-processed output (-E -dD) how the function will
>> be build and its semantic (since nested function will be local and
>> extra C defines may change it).
>>
>> I checked on x86_64-linux-gnu (w/o --enable-static-pie),
>> i686-linux-gnu, powerpc64-linux-gnu, powerpc-linux-gnu-power4,
>> aarch64-linux-gnu, arm-linux-gnu, sparc64-linux-gnu, and
>> s390x-linux-gnu.
> 
> Thank you for the fix!
> 
> hppa and ia64 build as well.
> 
> Reviewed-by: Fangrui Song <maskray@google.com>

And I just checked on ia64 and hppa and I saw no regressions.
Florian Weimer Oct. 13, 2021, 3:18 p.m. UTC | #5
* Adhemerval Zanella:

> On 12/10/2021 03:35, Florian Weimer wrote:
>> * Adhemerval Zanella via Libc-alpha:
>> 
>>> +  if (check)
>>> +    {
>>> +      /* Only the bind now flags are allowed.  */
>>> +      assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
>>> +	      || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
>>> +      /* Flags must not be set for ld.so.  */
>>> +      assert (info[DT_FLAGS] == NULL
>>> +	      || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
>>> +# ifdef STATIC_PIE_BOOTSTRAP
>>> +      assert (info[DT_RUNPATH] == NULL);
>>> +      assert (info[DT_RPATH] == NULL);
>>> +# endif
>>> +    }
>> 
>> Why is a conditional check required?  Is this an optimization?
>> 
>
> Because these asserts are only meaningful for the loader itself, but 
> this function is used by setup_vdso() as well.  Enabling it regardless
> leads to:

Please add a comment mentioning this.

In the long run, we should replace these asserts with a build-time
check.  I hesitate to add more tests based on readelf output, though.
diff mbox series

Patch

diff --git a/elf/dl-conflict.c b/elf/dl-conflict.c
index b58929cc5d..fb501e0373 100644
--- a/elf/dl-conflict.c
+++ b/elf/dl-conflict.c
@@ -16,6 +16,7 @@ 
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
+#include <assert.h>
 #include <errno.h>
 #include <libintl.h>
 #include <stdlib.h>
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 99c5ab83b3..335c79fad5 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -58,6 +58,7 @@  struct filebuf
 };
 
 #include "dynamic-link.h"
+#include "get-dynamic-info.h"
 #include <abi-tag.h>
 #include <stackinfo.h>
 #include <sysdep.h>
@@ -1297,7 +1298,7 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
   else
     l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
 
-  elf_get_dynamic_info (l);
+  elf_get_dynamic_info (l, false);
 
   /* Make sure we are not dlopen'ing an object that has the
      DF_1_NOOPEN flag set, or a PIE object.  */
diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c
index 4007580453..8a99c4995b 100644
--- a/elf/dl-reloc-static-pie.c
+++ b/elf/dl-reloc-static-pie.c
@@ -28,6 +28,7 @@ 
 #define STATIC_PIE_BOOTSTRAP
 #define RESOLVE_MAP(map, scope, sym, version, flags) map
 #include "dynamic-link.h"
+#include "get-dynamic-info.h"
 
 /* Relocate static executable with PIE.  */
 
@@ -51,7 +52,7 @@  _dl_relocate_static_pie (void)
 	break;
       }
 
-  elf_get_dynamic_info (main_map);
+  elf_get_dynamic_info (main_map, false);
 
 # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
   ELF_MACHINE_BEFORE_RTLD_RELOC (main_map, main_map->l_info);
diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
index 9d0d941000..61c260ddb8 100644
--- a/elf/dl-runtime.c
+++ b/elf/dl-runtime.c
@@ -19,6 +19,7 @@ 
 #define IN_DL_RUNTIME 1		/* This can be tested in dl-machine.h.  */
 
 #include <alloca.h>
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/param.h>
diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h
index 7cc3021164..21cdfc88bb 100644
--- a/elf/dynamic-link.h
+++ b/elf/dynamic-link.h
@@ -93,7 +93,6 @@  elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
 
 #include <dl-machine.h>
 
-#include "get-dynamic-info.h"
 
 #ifdef RESOLVE_MAP
 
diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h
index 15c316b38c..d169099fbc 100644
--- a/elf/get-dynamic-info.h
+++ b/elf/get-dynamic-info.h
@@ -25,7 +25,7 @@ 
 #include <libc-diag.h>
 
 static inline void __attribute__ ((unused, always_inline))
-elf_get_dynamic_info (struct link_map *l)
+elf_get_dynamic_info (struct link_map *l, bool check)
 {
 #if __ELF_NATIVE_CLASS == 32
   typedef Elf32_Word d_tag_utype;
@@ -112,16 +112,19 @@  elf_get_dynamic_info (struct link_map *l)
     assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
 #endif
 #ifdef RTLD_BOOTSTRAP
-  /* Only the bind now flags are allowed.  */
-  assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
-	  || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
-  /* Flags must not be set for ld.so.  */
-  assert (info[DT_FLAGS] == NULL
-	  || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
-#endif
-#if defined RTLD_BOOTSTRAP || defined STATIC_PIE_BOOTSTRAP
-  assert (info[DT_RUNPATH] == NULL);
-  assert (info[DT_RPATH] == NULL);
+  if (check)
+    {
+      /* Only the bind now flags are allowed.  */
+      assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
+	      || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
+      /* Flags must not be set for ld.so.  */
+      assert (info[DT_FLAGS] == NULL
+	      || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
+# ifdef STATIC_PIE_BOOTSTRAP
+      assert (info[DT_RUNPATH] == NULL);
+      assert (info[DT_RPATH] == NULL);
+# endif
+    }
 #else
   if (info[DT_FLAGS] != NULL)
     {
diff --git a/elf/rtld.c b/elf/rtld.c
index 5eee9e1091..fe5ee9ec4d 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -513,6 +513,7 @@  _dl_start_final (void *arg, struct dl_start_final_info *info)
      is trivial: always the map of ld.so itself.  */
 #define RTLD_BOOTSTRAP
 #define RESOLVE_MAP(map, scope, sym, version, flags) map
+#include "get-dynamic-info.h"
 #include "dynamic-link.h"
 
 static ElfW(Addr) __attribute_used__
@@ -547,7 +548,7 @@  _dl_start (void *arg)
 
   /* Read our own dynamic section and fill in the info array.  */
   bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
-  elf_get_dynamic_info (&bootstrap_map);
+  elf_get_dynamic_info (&bootstrap_map, true);
 
 #if NO_TLS_OFFSET != 0
   bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
@@ -1615,7 +1616,7 @@  dl_main (const ElfW(Phdr) *phdr,
   if (! rtld_is_main)
     {
       /* Extract the contents of the dynamic section for easy access.  */
-      elf_get_dynamic_info (main_map);
+      elf_get_dynamic_info (main_map, false);
 
       /* If the main map is libc.so, update the base namespace to
 	 refer to this map.  If libc.so is loaded later, this happens
diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h
index f44748bc98..6fdffafcca 100644
--- a/elf/setup-vdso.h
+++ b/elf/setup-vdso.h
@@ -64,7 +64,7 @@  setup_vdso (struct link_map *main_map __attribute__ ((unused)),
       l->l_map_end += l->l_addr;
       l->l_text_end += l->l_addr;
       l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
-      elf_get_dynamic_info (l);
+      elf_get_dynamic_info (l, false);
       _dl_setup_hash (l);
       l->l_relocated = 1;
 
diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h
index 68a0b4b476..dfa05eee44 100644
--- a/sysdeps/arm/dl-machine.h
+++ b/sysdeps/arm/dl-machine.h
@@ -21,6 +21,7 @@ 
 
 #define ELF_MACHINE_NAME "ARM"
 
+#include <assert.h>
 #include <sys/param.h>
 #include <tls.h>
 #include <dl-tlsdesc.h>
diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
index 9f578e536a..78bed03eee 100644
--- a/sysdeps/i386/dl-machine.h
+++ b/sysdeps/i386/dl-machine.h
@@ -21,6 +21,7 @@ 
 
 #define ELF_MACHINE_NAME "i386"
 
+#include <assert.h>
 #include <sys/param.h>
 #include <sysdep.h>
 #include <tls.h>
diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h
index 9c0182d597..20afb0b4bc 100644
--- a/sysdeps/x86_64/dl-machine.h
+++ b/sysdeps/x86_64/dl-machine.h
@@ -21,6 +21,7 @@ 
 
 #define ELF_MACHINE_NAME "x86_64"
 
+#include <assert.h>
 #include <sys/param.h>
 #include <sysdep.h>
 #include <tls.h>