Skip to contentSkip to conversation

Clean up the mips dynamic linker

PATCH H . J . Lu 5 participants 12 messages Jul 13, 2001 – Jul 16, 2001 MIPS

lore
Share & tools
lore

H . J . Lu

author
#0 lore raw

Here is a patch to cleanup the mips dynamic linker. It fixes 2 things:

1. We have been handling MAP_BASE_ADDR right for a long time. In the MIPS SVR4 ABI, DT_MIPS_MAP_BASE_ADDR holds the virtual address of the segment. According to the ABI, it is only used for

The dynamic linker relocates the global offset table by first adding the difference between the base where the shared object is loaded and the value of dyamic tag DT_MIPS_MAP_BASE_ADDR to all local global offset table entries.

In glibc, this difference is stored in the l_addr field in link_map. We have adjusted it in elf_machine_got_rel ():

/* Relocate GOT. */
static inline void
elf_machine_got_rel (struct link_map *map, int lazy) {
ElfW(Addr) *got;
ElfW(Sym) *sym;
int i, n, symidx;

  got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);

n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
/* The dynamic linker's local got entries have already been relocated. */ if (map != &_dl_rtld_map)

    {
      /* got[0] is reserved. got[1] is also reserved for the dynamic object
	 generated by gnu ld. Skip these reserved entries from relocation.  */
      i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1;

      /* Add the run-time displacement to all local got entries if
         needed.  */
      if (__builtin_expect (map->l_addr != 0, 0))
	{
	  while (i < n)
	    got[i++] += map->l_addr;
	}
    }

In fact, DT_MIPS_MAP_BASE_ADDR is the same as the p_addr field of the first loadable segment in the program header. I think it is included in the MIPS ABI to give the dynamic linker easy access to it.

I have tested DSOs with none-zero DT_MIPS_MAP_BASE_ADDR. It works fine. I think it is safe to remove MAP_BASE_ADDR and old binaries will work with the new glibc. If someone thinks I am wrong, please send me a testcase to show it.

2. The existing mips dynamic linker resolves GOT at the wrong time. It does it in elf_machine_got_rel called from elf_machine_runtime_setup. It doesn't work with Jakub's prelinking work. I think it should be done in elf_dynamic_do_rel. I introduced a new macro, ELF_MACHINE_RESOLVE_GOT, to do it.

I also clean up a little bit to follow lazy binding.

Any comments?

Thanks.

H.J.
-----
2001-07-12 H.J. Lu <[email protected]>

* elf/do-rel.h (ELF_MACHINE_RESOLVE_GOT): New. (elf_dynamic_do_rel): Call ELF_MACHINE_RESOLVE_GOT.

* sysdeps/mips/dl-machine.h (MAP_BASE_ADDR): Removed. (ELF_MACHINE_RESOLVE_GOT): Defined as elf_machine_resolve_got. (elf_machine_resolve_got): New fucntion to resolve GOT. (elf_machine_got_rel): Only do GOT relocation.

* sysdeps/mips/rtld-ldscript.in: Removed.
* sysdeps/mips/rtld-parms: Likewise.
* sysdeps/mips/mips64/rtld-parms: Likewise.
* sysdeps/mips/mipsel/rtld-parms: Likewise.

elf/do-rel.h renamed from elf/do-rel.h.mips +8 -0
@@ -30,6 +30,12 @@
 # define VERSYMIDX(sym)	(DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
 #endif
 
+/* Some machine, like mips, needs to resolve GOT. */
+
+#ifndef ELF_MACHINE_RESOLVE_GOT
+#define ELF_MACHINE_RESOLVE_GOT(map,lazy)
+#endif
+
 /* Perform the relocations in MAP on the running program image as specified
    by RELTAG, SZTAG.  If LAZY is nonzero, this is the first pass on PLT
    relocations; they should be set up to call _dl_runtime_resolve, rather
@@ -42,6 +48,8 @@ elf_dynamic_do_rel (struct link_map *map
 {
   const ElfW(Rel) *r = (const void *) reladdr;
   const ElfW(Rel) *end = (const void *) (reladdr + relsize);
+
+  ELF_MACHINE_RESOLVE_GOT (map, lazy);
 
   if (lazy)
     {
sysdeps/mips/dl-machine.h renamed from sysdeps/mips/dl-machine.h.mips +58 -89

191 lines not on this page · raw message

sysdeps/mips/mips64/rtld-parms renamed from sysdeps/mips/mips64/rtld-parms.mips +0 -3
@@ -1,3 +0,0 @@
-ifndef rtld-wordsize
-rtld-wordsize = 64
-endif
sysdeps/mips/mipsel/rtld-parms renamed from sysdeps/mips/mipsel/rtld-parms.mips +0 -3
@@ -1,3 +0,0 @@
-ifndef rtld-oformat
-rtld-oformat = elf32-littlemips
-endif
sysdeps/mips/rtld-ldscript.in renamed from sysdeps/mips/rtld-ldscript.in.mips +0 -105

105 lines not on this page · raw message

sysdeps/mips/rtld-parms renamed from sysdeps/mips/rtld-parms.mips +0 -15
@@ -1,15 +0,0 @@
-ifndef rtld-wordsize
-rtld-wordsize = 32
-endif
-ifndef rtld-oformat
-rtld-oformat = elf$(rtld-wordsize)-bigmips
-endif
-ifndef rtld-arch
-rtld-arch = mips
-endif
-ifndef rtld-entry
-rtld-entry = __start
-endif
-ifndef rtld-base
-rtld-base = 0x0fb60000 + SIZEOF_HEADERS
-endif
#1 lore raw
Quoted 3 lines · H . J . Lu

In fact, DT_MIPS_MAP_BASE_ADDR is the same as the p_addr field of the first loadable segment in the program header. I think it is included in the MIPS ABI to give the dynamic linker easy access to it.

Afair there is no requirement for loadable segments to be sorted so you'd have to go through all the program header table to find the one with the lowest address which isn't necessarily the first segment.

As the ABI doesn't give any guarantee that the lowest address in the segment table is the value of DT_MIPS_BASE_ADDR I just tried to find a binary on my IRIX boxen that violates this rule but I didn't find any. So please, go ahead.

Quoted 4 lines

I have tested DSOs with none-zero DT_MIPS_MAP_BASE_ADDR. It works fine. I think it is safe to remove MAP_BASE_ADDR and old binaries will work with the new glibc. If someone thinks I am wrong, please send me a testcase to show it.

Ralf

#2 lore raw
Quoted 1 line · Ralf Baechle

So please, go ahead.

So you say the patch is OK from your POV?

Signature · 3 lines
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------
#3 lore raw
Quoted 5 lines · Ulrich Drepper
Quoted 1 line · Ralf Baechle

So please, go ahead.

So you say the patch is OK from your POV?

Just for the record, I also think it's ok - but will not apply it since it depends on patches for generic code.

Andreas

Signature · 4 lines
 Andreas Jaeger
  SuSE Labs [email protected]
   private [email protected]
    http://www.suse.de/~aj
author
#4 lore raw
Quoted 10 lines · Andreas Jaeger
Quoted 5 lines · Ulrich Drepper
Quoted 1 line · Ralf Baechle

So please, go ahead.

So you say the patch is OK from your POV?

Just for the record, I also think it's ok - but will not apply it since it depends on patches for generic code.

Please ignore my first patch, which is wrong. The second one is ok which doesn't touch generic code.

H.J.

author
#5 lore raw
In reply to Ulrich Drepper
Quoted 6 lines · Ulrich Drepper
Quoted 1 line · Ralf Baechle

So please, go ahead.

So you say the patch is OK from your POV?

My last patch was not ok :-(. Somehow, make didn't rebuild. In this patch, I rewrote RESOLVE_GOTSYM with RESOLVE to help prelink.

H.J.
-----
2001-07-13 H.J. Lu <[email protected]>

* sysdeps/mips/dl-machine.h (MAP_BASE_ADDR): Removed. (elf_machine_got_rel): Defined only if RTLD_BOOTSTRAP is not defined.
(RESOLVE_GOTSYM): Rewrite to use RESOLVE.

* sysdeps/mips/rtld-ldscript.in: Removed.
* sysdeps/mips/rtld-parms: Likewise.
* sysdeps/mips/mips64/rtld-parms: Likewise.
* sysdeps/mips/mipsel/rtld-parms: Likewise.

sysdeps/mips/dl-machine.h renamed from sysdeps/mips/dl-machine.h.mips +16 -55
@@ -61,23 +61,6 @@
    in l_info array.  */
 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
 
-/*
- * MIPS libraries are usually linked to a non-zero base address.  We
- * subtract the base address from the address where we map the object
- * to.  This results in more efficient address space usage.
- *
- * FIXME: By the time when MAP_BASE_ADDR is called we don't have the
- * DYNAMIC section read.  Until this is fixed make the assumption that
- * libraries have their base address at 0x5ffe0000.  This needs to be
- * fixed before we can safely get rid of this MIPSism.
- */
-#if 0
-#define MAP_BASE_ADDR(l) ((l)->l_info[DT_MIPS(BASE_ADDRESS)] ? \
-			  (l)->l_info[DT_MIPS(BASE_ADDRESS)]->d_un.d_ptr : 0)
-#else
-#define MAP_BASE_ADDR(l) 0x5ffe0000
-#endif
-
 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
    with the run-time address of the r_debug structure  */
 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
@@ -557,51 +540,30 @@ elf_machine_lazy_rel (struct link_map *m
   /* Do nothing.  */
 }
 
+#ifndef RTLD_BOOTSTRAP
 /* Relocate GOT. */
 static inline void
 elf_machine_got_rel (struct link_map *map, int lazy)
 {
   ElfW(Addr) *got;
   ElfW(Sym) *sym;
+  const ElfW(Half) *vernum;
   int i, n, symidx;
-  /*  This function is loaded in dl-reloc as a nested function and can
-      therefore access the variables scope and strtab from
-      _dl_relocate_object.  */
-#ifdef RTLD_BOOTSTRAP
-# define RESOLVE_GOTSYM(sym,sym_index) 0
-#else
-# define RESOLVE_GOTSYM(sym,sym_index)					  \
+
+#define RESOLVE_GOTSYM(sym,vernum,sym_index)				  \
     ({									  \
       const ElfW(Sym) *ref = sym;					  \
+      const struct r_found_version *version				  \
+        = vernum ? &map->l_versions [vernum [sym_index]] : NULL;	  \
       ElfW(Addr) value;							  \
-									  \
-      switch (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)		  \
-	{								  \
-	default:							  \
-	  {								  \
-	    const ElfW(Half) *vernum =					  \
-	      (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);  \
-	    ElfW(Half) ndx = vernum[sym_index];				  \
-	    const struct r_found_version *version = &l->l_versions[ndx];  \
-									  \
-	    if (version->hash != 0)					  \
-	      {								  \
-		value = _dl_lookup_versioned_symbol(strtab + sym->st_name,\
-						    map,		  \
-						    &ref, scope, version, \
-						    R_MIPS_REL32, 0);	  \
-		break;							  \
-	      }								  \
-	    /* Fall through.  */					  \
-	  }								  \
-	case 0:								  \
-	  value = _dl_lookup_symbol (strtab + sym->st_name, map, &ref,	  \
-				     scope, R_MIPS_REL32, 0);		  \
-	}								  \
-									  \
+      value = RESOLVE (&ref, version, R_MIPS_REL32);			  \
       (ref)? value + ref->st_value: 0;					  \
     })
-#endif /* RTLD_BOOTSTRAP */
+
+  if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
+    vernum = (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
+  else
+    vernum = NULL;
 
   got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
 
@@ -639,10 +601,10 @@ elf_machine_got_rel (struct link_map *ma
 	      && sym->st_value && lazy)
 	    *got = sym->st_value + map->l_addr;
 	  else
-	    *got = RESOLVE_GOTSYM (sym, symidx);
+	    *got = RESOLVE_GOTSYM (sym, vernum, symidx);
 	}
       else if (sym->st_shndx == SHN_COMMON)
-	*got = RESOLVE_GOTSYM (sym, symidx);
+	*got = RESOLVE_GOTSYM (sym, vernum, symidx);
       else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
 	       && *got != sym->st_value
 	       && lazy)
@@ -653,7 +615,7 @@ elf_machine_got_rel (struct link_map *ma
 	    *got += map->l_addr;
 	}
       else
-	*got = RESOLVE_GOTSYM (sym, symidx);
+	*got = RESOLVE_GOTSYM (sym, vernum, symidx);
 
       ++got;
       ++sym;
@@ -661,9 +623,8 @@ elf_machine_got_rel (struct link_map *ma
     }
 
 #undef RESOLVE_GOTSYM
-
-  return;
 }
+#endif
 
 /* Set up the loaded object described by L so its stub function
    will jump to the on-demand fixup code __dl_runtime_resolve.  */
sysdeps/mips/mips64/rtld-parms renamed from sysdeps/mips/mips64/rtld-parms.mips +0 -3
@@ -1,3 +0,0 @@
-ifndef rtld-wordsize
-rtld-wordsize = 64
-endif
sysdeps/mips/mipsel/rtld-parms renamed from sysdeps/mips/mipsel/rtld-parms.mips +0 -3
@@ -1,3 +0,0 @@
-ifndef rtld-oformat
-rtld-oformat = elf32-littlemips
-endif
sysdeps/mips/rtld-ldscript.in renamed from sysdeps/mips/rtld-ldscript.in.mips +0 -105

105 lines not on this page · raw message

sysdeps/mips/rtld-parms renamed from sysdeps/mips/rtld-parms.mips +0 -15
@@ -1,15 +0,0 @@
-ifndef rtld-wordsize
-rtld-wordsize = 32
-endif
-ifndef rtld-oformat
-rtld-oformat = elf$(rtld-wordsize)-bigmips
-endif
-ifndef rtld-arch
-rtld-arch = mips
-endif
-ifndef rtld-entry
-rtld-entry = __start
-endif
-ifndef rtld-base
-rtld-base = 0x0fb60000 + SIZEOF_HEADERS
-endif
#6 lore raw
Quoted 11 lines · H . J . Lu

2001-07-13 H.J. Lu <[email protected]>

* sysdeps/mips/dl-machine.h (MAP_BASE_ADDR): Removed. (elf_machine_got_rel): Defined only if RTLD_BOOTSTRAP is not defined.
(RESOLVE_GOTSYM): Rewrite to use RESOLVE.

* sysdeps/mips/rtld-ldscript.in: Removed.
* sysdeps/mips/rtld-parms: Likewise.
* sysdeps/mips/mips64/rtld-parms: Likewise.
* sysdeps/mips/mipsel/rtld-parms: Likewise.

Is this the complete patch? Nothing needed from the other patches?

Signature · 3 lines
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------
author
#7 lore raw
Quoted 9 lines · Ulrich Drepper
Quoted 7 lines · H . J . Lu

2001-07-13 H.J. Lu <[email protected]>

* sysdeps/mips/dl-machine.h (MAP_BASE_ADDR): Removed. (elf_machine_got_rel): Defined only if RTLD_BOOTSTRAP is not defined.
(RESOLVE_GOTSYM): Rewrite to use RESOLVE.

Quoted 7 lines
Quoted 4 lines

* sysdeps/mips/rtld-ldscript.in: Removed.
* sysdeps/mips/rtld-parms: Likewise.
* sysdeps/mips/mips64/rtld-parms: Likewise.
* sysdeps/mips/mipsel/rtld-parms: Likewise.

Is this the complete patch? Nothing needed from the other patches?

Yes, it is complete. Tested on Linux/mipsel.

H.J.

#8 lore raw
In reply to H . J . Lu
Quoted 2 lines · H . J . Lu

My last patch was not ok :-(. Somehow, make didn't rebuild. In this patch, I rewrote RESOLVE_GOTSYM with RESOLVE to help prelink.

Applied now. Thanks,

Signature · 3 lines
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------
author
#9 lore raw
In reply to Ralf Baechle
Quoted 6 lines · Ralf Baechle
Quoted 3 lines · H . J . Lu

In fact, DT_MIPS_MAP_BASE_ADDR is the same as the p_addr field of the first loadable segment in the program header. I think it is included in the MIPS ABI to give the dynamic linker easy access to it.

Quoted 8 lines

Afair there is no requirement for loadable segments to be sorted so you'd have to go through all the program header table to find the one with the lowest address which isn't necessarily the first segment.

As the ABI doesn't give any guarantee that the lowest address in the segment table is the value of DT_MIPS_BASE_ADDR I just tried to find a binary on my IRIX boxen that violates this rule but I didn't find any. So please, go ahead.

It doesn't matter. DT_MIPS_BASE_ADDR is the "Base Address" in the gABI. glibc has to get the "Base Address" right. Otherwise, it won't work correctly for all cases. It is not mips specific.

H.J.

author
#11 lore raw
Quoted 3 lines · Roland McGrath
Quoted 1 line

Afair there is no requirement for loadable segments to be sorted

PT_LOAD entries appear in ascending order, sorted on the p_vaddr member.

Thanks. I missed that one. In any case, glibc has been right on mips for a long time. We just don't need that specical treatmeant for DT_MIPS_BASE_ADDRESS.

H.J.