~ubuntu-branches/ubuntu/hoary/binutils/hoary

« back to all changes in this revision

Viewing changes to bfd/elf-strtab.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-03-18 13:07:52 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050318130752-j4i37zgqclj53b94
Tags: 2.15-5ubuntu2
debian/rules: Call pkgstriptranslations if present (the package does not
use debhelper, thus it does not happen automatically).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* ELF strtab with GC and suffix merging support.
2
 
   Copyright 2001, 2002 Free Software Foundation, Inc.
 
2
   Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3
3
   Written by Jakub Jelinek <jakub@redhat.com>.
4
4
 
5
5
   This file is part of BFD, the Binary File Descriptor library.
30
30
struct elf_strtab_hash_entry
31
31
{
32
32
  struct bfd_hash_entry root;
33
 
  /* Length of this entry.  */
34
 
  unsigned int len;
 
33
  /* Length of this entry.  This includes the zero terminator.  */
 
34
  int len;
35
35
  unsigned int refcount;
36
36
  union {
37
37
    /* Index within the merged section.  */
38
38
    bfd_size_type index;
39
 
    /* Entry this is a suffix of (if len is 0).  */
 
39
    /* Entry this is a suffix of (if len < 0).  */
40
40
    struct elf_strtab_hash_entry *suffix;
41
 
    struct elf_strtab_hash_entry *next;
42
41
  } u;
43
42
};
44
43
 
158
157
  if (entry->len == 0)
159
158
    {
160
159
      entry->len = strlen (str) + 1;
 
160
      /* 2G strings lose.  */
 
161
      BFD_ASSERT (entry->len > 0);
161
162
      if (tab->size == tab->alloced)
162
163
        {
163
164
          bfd_size_type amt = sizeof (struct elf_strtab_hash_entry *);
235
236
  for (i = 1; i < tab->size; ++i)
236
237
    {
237
238
      register const char *str;
238
 
      register size_t len;
 
239
      register unsigned int len;
239
240
 
240
 
      str = tab->array[i]->root.string;
 
241
      BFD_ASSERT (tab->array[i]->refcount == 0);
241
242
      len = tab->array[i]->len;
242
 
      BFD_ASSERT (tab->array[i]->refcount == 0);
243
 
      if (len == 0)
 
243
      if ((int) len < 0)
244
244
        continue;
245
245
 
 
246
      str = tab->array[i]->root.string;
246
247
      if (bfd_bwrite (str, len, abfd) != len)
247
248
        return FALSE;
248
249
 
253
254
  return TRUE;
254
255
}
255
256
 
256
 
/* Compare two elf_strtab_hash_entry structures.  This is called via qsort.  */
 
257
/* Compare two elf_strtab_hash_entry structures.  Called via qsort.  */
257
258
 
258
259
static int
259
 
cmplengthentry (const void *a, const void *b)
 
260
strrevcmp (const void *a, const void *b)
260
261
{
261
262
  struct elf_strtab_hash_entry *A = *(struct elf_strtab_hash_entry **) a;
262
263
  struct elf_strtab_hash_entry *B = *(struct elf_strtab_hash_entry **) b;
263
 
 
264
 
  if (A->len < B->len)
265
 
    return 1;
266
 
  else if (A->len > B->len)
267
 
    return -1;
268
 
 
269
 
  return memcmp (A->root.string, B->root.string, A->len);
 
264
  unsigned int lenA = A->len;
 
265
  unsigned int lenB = B->len;
 
266
  const unsigned char *s = A->root.string + lenA - 1;
 
267
  const unsigned char *t = B->root.string + lenB - 1;
 
268
  int l = lenA < lenB ? lenA : lenB;
 
269
 
 
270
  while (l)
 
271
    {
 
272
      if (*s != *t)
 
273
        return (int) *s - (int) *t;
 
274
      s--;
 
275
      t--;
 
276
      l--;
 
277
    }
 
278
  return lenA - lenB;
270
279
}
271
280
 
272
 
static int
273
 
last4_eq (const void *a, const void *b)
 
281
static inline int
 
282
is_suffix (const struct elf_strtab_hash_entry *A,
 
283
           const struct elf_strtab_hash_entry *B)
274
284
{
275
 
  const struct elf_strtab_hash_entry *A = a;
276
 
  const struct elf_strtab_hash_entry *B = b;
277
 
 
278
 
  if (memcmp (A->root.string + A->len - 5, B->root.string + B->len - 5, 4)
279
 
      != 0)
280
 
    /* This was a hashtable collision.  */
281
 
    return 0;
282
 
 
283
285
  if (A->len <= B->len)
284
286
    /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
285
287
       not to be equal by the hash table.  */
286
288
    return 0;
287
289
 
288
290
  return memcmp (A->root.string + (A->len - B->len),
289
 
                 B->root.string, B->len - 5) == 0;
 
291
                 B->root.string, B->len - 1) == 0;
290
292
}
291
293
 
292
294
/* This function assigns final string table offsets for used strings,
295
297
void
296
298
_bfd_elf_strtab_finalize (struct elf_strtab_hash *tab)
297
299
{
298
 
  struct elf_strtab_hash_entry **array, **a, **end, *e;
299
 
  htab_t last4tab = NULL;
 
300
  struct elf_strtab_hash_entry **array, **a, *e;
300
301
  bfd_size_type size, amt;
301
 
  struct elf_strtab_hash_entry *last[256], **last_ptr[256];
302
302
 
303
303
  /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
304
304
     a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
306
306
     cycles.  */
307
307
  size_t i;
308
308
 
309
 
  /* Now sort the strings by length, longest first.  */
310
 
  array = NULL;
 
309
  /* Sort the strings by suffix and length.  */
311
310
  amt = tab->size * sizeof (struct elf_strtab_hash_entry *);
312
311
  array = bfd_malloc (amt);
313
312
  if (array == NULL)
314
313
    goto alloc_failure;
315
314
 
316
 
  memset (last, 0, sizeof (last));
317
 
  for (i = 0; i < 256; ++i)
318
 
    last_ptr[i] = &last[i];
319
315
  for (i = 1, a = array; i < tab->size; ++i)
320
 
    if (tab->array[i]->refcount)
321
 
      *a++ = tab->array[i];
322
 
    else
323
 
      tab->array[i]->len = 0;
 
316
    {
 
317
      e = tab->array[i];
 
318
      if (e->refcount)
 
319
        {
 
320
          *a++ = e;
 
321
          /* Adjust the length to not include the zero terminator.  */
 
322
          e->len -= 1;
 
323
        }
 
324
      else
 
325
        e->len = 0;
 
326
    }
324
327
 
325
328
  size = a - array;
326
 
 
327
 
  qsort (array, size, sizeof (struct elf_strtab_hash_entry *), cmplengthentry);
328
 
 
329
 
  last4tab = htab_create_alloc (size * 4, NULL, last4_eq, NULL, calloc, free);
330
 
  if (last4tab == NULL)
331
 
    goto alloc_failure;
332
 
 
333
 
  /* Now insert the strings into hash tables (strings with last 4 characters
334
 
     and strings with last character equal), look for longer strings which
335
 
     we're suffix of.  */
336
 
  for (a = array, end = array + size; a < end; a++)
 
329
  if (size != 0)
337
330
    {
338
 
      register hashval_t hash;
339
 
      unsigned int c;
340
 
      unsigned int j;
341
 
      const unsigned char *s;
342
 
      void **p;
343
 
 
344
 
      e = *a;
345
 
      if (e->len > 4)
 
331
      qsort (array, size, sizeof (struct elf_strtab_hash_entry *), strrevcmp);
 
332
 
 
333
      /* Loop over the sorted array and merge suffixes.  Start from the
 
334
         end because we want eg.
 
335
 
 
336
         s1 -> "d"
 
337
         s2 -> "bcd"
 
338
         s3 -> "abcd"
 
339
 
 
340
         to end up as
 
341
 
 
342
         s3 -> "abcd"
 
343
         s2 _____^
 
344
         s1 _______^
 
345
 
 
346
         ie. we don't want s1 pointing into the old s2.  */
 
347
      e = *--a;
 
348
      e->len += 1;
 
349
      while (--a >= array)
346
350
        {
347
 
          s = e->root.string + e->len - 1;
348
 
          hash = 0;
349
 
          for (j = 0; j < 4; j++)
350
 
            {
351
 
              c = *--s;
352
 
              hash += c + (c << 17);
353
 
              hash ^= hash >> 2;
354
 
            }
355
 
          p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
356
 
          if (p == NULL)
357
 
            goto alloc_failure;
358
 
          if (*p)
359
 
            {
360
 
              struct elf_strtab_hash_entry *ent;
 
351
          struct elf_strtab_hash_entry *cmp = *a;
361
352
 
362
 
              ent = *p;
363
 
              e->u.suffix = ent;
364
 
              e->len = 0;
365
 
              continue;
 
353
          cmp->len += 1;
 
354
          if (is_suffix (e, cmp))
 
355
            {
 
356
              cmp->u.suffix = e;
 
357
              cmp->len = -cmp->len;
366
358
            }
367
359
          else
368
 
            *p = e;
369
 
        }
370
 
      else
371
 
        {
372
 
          struct elf_strtab_hash_entry *tem;
373
 
 
374
 
          c = e->root.string[e->len - 2] & 0xff;
375
 
 
376
 
          for (tem = last[c]; tem; tem = tem->u.next)
377
 
            if (tem->len > e->len
378
 
                && memcmp (tem->root.string + (tem->len - e->len),
379
 
                           e->root.string, e->len - 1) == 0)
380
 
              break;
381
 
          if (tem)
382
 
            {
383
 
              e->u.suffix = tem;
384
 
              e->len = 0;
385
 
              continue;
386
 
            }
387
 
        }
388
 
 
389
 
      c = e->root.string[e->len - 2] & 0xff;
390
 
      /* Put longest strings first.  */
391
 
      *last_ptr[c] = e;
392
 
      last_ptr[c] = &e->u.next;
393
 
      e->u.next = NULL;
 
360
            e = cmp;
 
361
        }
394
362
    }
395
363
 
396
364
alloc_failure:
397
365
  if (array)
398
366
    free (array);
399
 
  if (last4tab)
400
 
    htab_delete (last4tab);
401
367
 
402
 
  /* Now assign positions to the strings we want to keep.  */
 
368
  /* Assign positions to the strings we want to keep.  */
403
369
  size = 1;
404
370
  for (i = 1; i < tab->size; ++i)
405
371
    {
406
372
      e = tab->array[i];
407
 
      if (e->refcount && e->len)
 
373
      if (e->refcount && e->len > 0)
408
374
        {
409
375
          e->u.index = size;
410
376
          size += e->len;
413
379
 
414
380
  tab->sec_size = size;
415
381
 
416
 
  /* And now adjust the rest.  */
 
382
  /* Adjust the rest.  */
417
383
  for (i = 1; i < tab->size; ++i)
418
384
    {
419
385
      e = tab->array[i];
420
 
      if (e->refcount && ! e->len)
421
 
        e->u.index = e->u.suffix->u.index
422
 
                     + (e->u.suffix->len - strlen (e->root.string) - 1);
 
386
      if (e->refcount && e->len < 0)
 
387
        e->u.index = e->u.suffix->u.index + (e->u.suffix->len + e->len);
423
388
    }
424
389
}