This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: PATCH: Generic function descriptor
On Thu, Apr 03, 2003 at 04:02:21PM -0800, Ulrich Drepper wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> H. J. Lu wrote:
>
> > I am not sure how feasible it is. That piece of code can be called from
> > anywhere. I don't know if it can use any library functions since they
> > may depend on it themselves. It can only use system calls.
>
> I don't suggest adding more function calls. The cmpxchg is an inline
> operation.
>
>
> > It needs the lock for updating some data structure.
>
> There is no need for locking to synchronize updating the function
> descriptor data structures. If more than one thread at the same time
> finds no more descriptor available let them both allocate more memory
> and use both memory blocks or discard one. This situation will be so
> infrequent that the additional overhead is completely neglectable.
>
>
> You don't have to do it, I might look at it soon. But your unification
> patch won't go in as is. The improved function descriptor handling
> might very well require enough changes to make it impossible to share it
> with HPPA and its pathetic instruction set.
>
Here is my first try to get rid of lock. I haven't figured out a way
to remove duplicated function descriptors.
H.J.
---
#include <unistd.h>
#include <string.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <link.h>
#include <ldsodefs.h>
#include <elf/dynamic-link.h>
#include <dl-fptr.h>
#include <atomic.h>
#ifndef ELF_MACHINE_BOOT_FPTR_TABLE_LEN
/* ELF_MACHINE_BOOT_FPTR_TABLE_LEN should be greater than the number of
dynamic symbols in ld.so. */
#define ELF_MACHINE_BOOT_FPTR_TABLE_LEN 256
#endif
#ifndef ELF_MACHINE_LOAD_ADDRESS
# error "ELF_MACHINE_LOAD_ADDRESS is not defined."
#endif
#ifndef COMPARE_AND_SWAP
#define COMPARE_AND_SWAP(ptr,old,new) \
atomic_compare_and_exchange_bool_acq ((ptr), (old), (new))
#endif
ElfW(Addr) _dl_boot_fptr_table [ELF_MACHINE_BOOT_FPTR_TABLE_LEN];
static struct local
{
struct fdesc_table *root;
struct fdesc *free_list;
unsigned int npages; /* # of pages to allocate */
/* the next to members MUST be consecutive! */
struct fdesc_table boot_table;
struct fdesc boot_fdescs[1024];
}
local =
{
root: &local.boot_table,
npages: 2,
boot_table:
{
len: sizeof (local.boot_fdescs) / sizeof (local.boot_fdescs[0]),
first_unused: 0
}
};
/* Create a new fdesc table and return a pointer to the first fdesc
entry. The fdesc lock must have been acquired already. */
static struct fdesc_table *
new_fdesc_table (struct local *l, size_t *size)
{
size_t old_npages = l->npages;
size_t new_npages = old_npages + old_npages;
struct fdesc_table *new_table;
/* If someone has just created a new table, we return NULL to tell
the caller to try again. */
if (!COMPARE_AND_SWAP (&l->npages, old_npages, new_npages))
return (struct fdesc_table *) NULL;
*size = old_npages * GL(dl_pagesize);
new_table = __mmap (0, *size, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0);
if (new_table == MAP_FAILED)
_dl_signal_error (errno, NULL, NULL, "cannot map pages for fdesc table");
new_table->len = (*size - sizeof (*new_table)) / sizeof (struct fdesc);
new_table->first_unused = 1;
return new_table;
}
static ElfW(Addr)
make_fdesc (ElfW(Addr) ip, ElfW(Addr) gp)
{
struct fdesc *fdesc = NULL;
struct fdesc_table *t;
unsigned int old;
struct local *l;
ELF_MACHINE_LOAD_ADDRESS (l, local);
t = l->root;
while (1)
{
old = t->first_unused;
if (old >= t->len)
break;
else if (COMPARE_AND_SWAP (&t->first_unused, old, old + 1))
{
fdesc = &t->fdesc[old];
goto install;
}
else
{
/* Someone has just used an unused one to create a new
function descriptor. We return NULL to tell the caller
to check it again. */
goto out;
}
}
if (l->free_list)
{
fdesc = l->free_list; /* get it from free-list */
if (!COMPARE_AND_SWAP ((ElfW(Addr) *) &l->free_list,
(ElfW(Addr)) fdesc,
(ElfW(Addr)) fdesc->ip))
{
/* Someone has just taken one from free-list to create a new
function descriptor. We return NULL to tell the caller
to check it again. */
return (ElfW(Addr)) NULL;
}
}
else
{
/* Create a new fdesc table */
size_t size;
struct fdesc_table *new_table = new_fdesc_table (l, &size);
struct fdesc_table *root;
if (!new_table)
goto out;
new_table->next = l->root;
root = l->root;
if (!COMPARE_AND_SWAP ((ElfW(Addr) *) &l->root,
(ElfW(Addr)) root,
(ElfW(Addr)) new_table))
{
/* Someone has just installed a new table. Return NULL to
tell the caller to try again. */
__munmap (new_table, size);
goto out;
}
fdesc = &new_table->fdesc[0];
}
install:
fdesc->ip = ip;
fdesc->gp = gp;
out:
return (ElfW(Addr)) fdesc;
}
static inline ElfW(Addr) *
make_fptr_table (struct link_map *map)
{
const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
ElfW(Addr) *fptr_table;
size_t size;
size_t len;
/* XXX Apparently the only way to find out the size of the dynamic
symbol section is to assume that the string table follows right
afterwards... */
len = ((strtab - (char *) symtab) / map->l_info[DT_SYMENT]->d_un.d_val);
size = ((len * sizeof (fptr_table[0]) + GL(dl_pagesize) - 1)
& -GL(dl_pagesize));
/* XXX We don't support here in the moment systems without MAP_ANON.
There probably are none for IA-64. In case this is proven wrong
we will have to open /dev/null here and use the file descriptor
instead of the hard-coded -1. */
fptr_table = __mmap (NULL, size, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0);
if (fptr_table == MAP_FAILED)
_dl_signal_error (errno, NULL, NULL, "cannot map pages for fptr table");
if (COMPARE_AND_SWAP ((ElfW(Addr) *) &map->l_mach.fptr_table,
(ElfW(Addr)) NULL, (ElfW(Addr)) fptr_table))
map->l_mach.fptr_table_len = len;
else
__munmap (fptr_table, len * sizeof (fptr_table[0]));
return map->l_mach.fptr_table;
}
ElfW(Addr)
_dl_make_fptr (struct link_map *map, const ElfW(Sym) *sym,
ElfW(Addr) ip)
{
ElfW(Addr) *ftab = map->l_mach.fptr_table;
const ElfW(Sym) *symtab;
Elf_Symndx symidx;
if (__builtin_expect (!ftab, 0))
ftab = make_fptr_table (map);
symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
symidx = sym - symtab;
if (symidx >= map->l_mach.fptr_table_len)
_dl_signal_error (0, NULL, NULL,
"internal error: symidx out of range of fptr table");
while (!ftab[symidx])
{
/* GOT has already been relocated in elf_get_dynamic_info -
don't try to relocate it again. */
ElfW(Addr) fdesc
= make_fdesc (ip, map->l_info[DT_PLTGOT]->d_un.d_ptr);
/* Continue if we failed to create a function descriptor. */
if (!fdesc)
continue;
if (COMPARE_AND_SWAP (&ftab[symidx], (ElfW(Addr)) NULL, fdesc))
{
/* Noone has updated the entry and the new function
descriptor has been installed. */
#if 0
const char *strtab
= (const void *) D_PTR (map, l_info[DT_STRTAB]);
struct local *l;
ELF_MACHINE_LOAD_ADDRESS (l, local);
if (l->root != &l->boot_table
|| l->boot_table.first_unused > 20)
_dl_debug_printf ("created fdesc symbol `%s' at %lx\n",
strtab + sym->st_name, ftab[symidx]);
#endif
break;
}
else
{
/* We create a duplicated function descriptor. We need to
free it. FIXME: How? */
}
}
return ftab[symidx];
}
void
_dl_unmap (struct link_map *map)
{
ElfW(Addr) *ftab = map->l_mach.fptr_table;
struct fdesc *head = NULL, *tail = NULL;
size_t i;
__munmap ((void *) map->l_map_start, map->l_map_end - map->l_map_start);
if (!ftab)
return;
/* String together the fdesc structures that are being freed. */
for (i = 0; i < map->l_mach.fptr_table_len; ++i)
{
if (ftab[i])
{
*(struct fdesc **) ftab[i] = head;
head = (struct fdesc *) ftab[i];
if (!tail)
tail = head;
}
}
/* Prepend the new list to the free_list: */
if (tail)
do
{
*(struct fdesc **) tail = local.free_list;
}
while (!COMPARE_AND_SWAP ((ElfW(Addr) *) &local.free_list,
(ElfW(Addr)) tail, (ElfW(Addr)) head));
__munmap (ftab,
map->l_mach.fptr_table_len * sizeof (map->l_mach.fptr_table[0]));
map->l_mach.fptr_table = NULL;
}
ElfW(Addr)
_dl_lookup_address (const void *address)
{
ElfW(Addr) addr = (ElfW(Addr)) address;
struct fdesc_table *t;
unsigned long int i;
for (t = local.root; t != NULL; t = t->next)
{
i = (struct fdesc *) addr - &t->fdesc[0];
if (i < t->first_unused && addr == (ElfW(Addr)) &t->fdesc[i])
{
addr = t->fdesc[i].ip;
break;
}
}
return addr;
}