Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/libctf/ctf-string.c
Line
Count
Source (jump to first uncovered line)
1
/* CTF string table management.
2
   Copyright (C) 2019-2024 Free Software Foundation, Inc.
3
4
   This file is part of libctf.
5
6
   libctf is free software; you can redistribute it and/or modify it under
7
   the terms of the GNU General Public License as published by the Free
8
   Software Foundation; either version 3, or (at your option) any later
9
   version.
10
11
   This program is distributed in the hope that it will be useful, but
12
   WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
   See the GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; see the file COPYING.  If not see
18
   <http://www.gnu.org/licenses/>.  */
19
20
#include <assert.h>
21
#include <ctf-impl.h>
22
#include <string.h>
23
24
static ctf_str_atom_t *
25
ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str,
26
        int flags, uint32_t *ref);
27
28
/* Convert an encoded CTF string name into a pointer to a C string, possibly
29
  using an explicit internal provisional strtab rather than the fp-based
30
  one.  */
31
const char *
32
ctf_strraw_explicit (ctf_dict_t *fp, uint32_t name, ctf_strs_t *strtab)
33
0
{
34
0
  ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID (name)];
35
36
0
  if ((CTF_NAME_STID (name) == CTF_STRTAB_0) && (strtab != NULL))
37
0
    ctsp = strtab;
38
39
  /* If this name is in the external strtab, and there is a synthetic
40
     strtab, use it in preference.  (This is used to add the set of strings
41
     -- symbol names, etc -- the linker knows about before the strtab is
42
     written out.)  */
43
44
0
  if (CTF_NAME_STID (name) == CTF_STRTAB_1
45
0
      && fp->ctf_syn_ext_strtab != NULL)
46
0
    return ctf_dynhash_lookup (fp->ctf_syn_ext_strtab,
47
0
             (void *) (uintptr_t) name);
48
49
  /* If the name is in the internal strtab, and the name offset is beyond
50
     the end of the ctsp->cts_len but below the ctf_str_prov_offset, this is
51
     a provisional string added by ctf_str_add*() but not yet built into a
52
     real strtab: get the value out of the ctf_prov_strtab.  */
53
54
0
  if (CTF_NAME_STID (name) == CTF_STRTAB_0
55
0
      && name >= ctsp->cts_len && name < fp->ctf_str_prov_offset)
56
0
      return ctf_dynhash_lookup (fp->ctf_prov_strtab,
57
0
         (void *) (uintptr_t) name);
58
59
0
  if (ctsp->cts_strs != NULL && CTF_NAME_OFFSET (name) < ctsp->cts_len)
60
0
    return (ctsp->cts_strs + CTF_NAME_OFFSET (name));
61
62
  /* String table not loaded or corrupt offset.  */
63
0
  return NULL;
64
0
}
65
66
/* Convert an encoded CTF string name into a pointer to a C string by looking
67
  up the appropriate string table buffer and then adding the offset.  */
68
const char *
69
ctf_strraw (ctf_dict_t *fp, uint32_t name)
70
0
{
71
0
  return ctf_strraw_explicit (fp, name, NULL);
72
0
}
73
74
/* Return a guaranteed-non-NULL pointer to the string with the given CTF
75
   name.  */
76
const char *
77
ctf_strptr (ctf_dict_t *fp, uint32_t name)
78
0
{
79
0
  const char *s = ctf_strraw (fp, name);
80
0
  return (s != NULL ? s : "(?)");
81
0
}
82
83
/* As above, but return info on what is wrong in more detail.
84
   (Used for type lookups.) */
85
86
const char *
87
ctf_strptr_validate (ctf_dict_t *fp, uint32_t name)
88
0
{
89
0
  const char *str = ctf_strraw (fp, name);
90
91
0
  if (str == NULL)
92
0
    {
93
0
      if (CTF_NAME_STID (name) == CTF_STRTAB_1
94
0
    && fp->ctf_syn_ext_strtab == NULL
95
0
    && fp->ctf_str[CTF_NAME_STID (name)].cts_strs == NULL)
96
0
  {
97
0
    ctf_set_errno (fp, ECTF_STRTAB);
98
0
    return NULL;
99
0
  }
100
101
0
      ctf_set_errno (fp, ECTF_BADNAME);
102
0
      return NULL;
103
0
    }
104
0
  return str;
105
0
}
106
107
/* Remove all refs to a given atom.  */
108
static void
109
ctf_str_purge_atom_refs (ctf_str_atom_t *atom)
110
0
{
111
0
  ctf_str_atom_ref_t *ref, *next;
112
113
0
  for (ref = ctf_list_next (&atom->csa_refs); ref != NULL; ref = next)
114
0
    {
115
0
      next = ctf_list_next (ref);
116
0
      ctf_list_delete (&atom->csa_refs, ref);
117
0
      if (atom->csa_flags & CTF_STR_ATOM_MOVABLE)
118
0
  {
119
0
    ctf_str_atom_ref_movable_t *movref;
120
0
    movref = (ctf_str_atom_ref_movable_t *) ref;
121
0
    ctf_dynhash_remove (movref->caf_movable_refs, ref);
122
0
  }
123
124
0
      free (ref);
125
0
    }
126
0
}
127
128
/* Free an atom.  */
129
static void
130
ctf_str_free_atom (void *a)
131
0
{
132
0
  ctf_str_atom_t *atom = a;
133
134
0
  ctf_str_purge_atom_refs (atom);
135
136
0
  if (atom->csa_flags & CTF_STR_ATOM_FREEABLE)
137
0
    free (atom->csa_str);
138
139
0
  free (atom);
140
0
}
141
142
/* Create the atoms table.  There is always at least one atom in it, the null
143
   string: but also pull in atoms from the internal strtab.  (We rely on
144
   calls to ctf_str_add_external to populate external strtab entries, since
145
   these are often not quite the same as what appears in any external
146
   strtab, and the external strtab is often huge and best not aggressively
147
   pulled in.)  */
148
int
149
ctf_str_create_atoms (ctf_dict_t *fp)
150
0
{
151
0
  size_t i;
152
153
0
  fp->ctf_str_atoms = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
154
0
            NULL, ctf_str_free_atom);
155
0
  if (!fp->ctf_str_atoms)
156
0
    return -ENOMEM;
157
158
0
  if (!fp->ctf_prov_strtab)
159
0
    fp->ctf_prov_strtab = ctf_dynhash_create (ctf_hash_integer,
160
0
                ctf_hash_eq_integer,
161
0
                NULL, NULL);
162
0
  if (!fp->ctf_prov_strtab)
163
0
    goto oom_prov_strtab;
164
165
0
  fp->ctf_str_movable_refs = ctf_dynhash_create (ctf_hash_integer,
166
0
             ctf_hash_eq_integer,
167
0
             NULL, NULL);
168
0
  if (!fp->ctf_str_movable_refs)
169
0
    goto oom_movable_refs;
170
171
0
  errno = 0;
172
0
  ctf_str_add (fp, "");
173
0
  if (errno == ENOMEM)
174
0
    goto oom_str_add;
175
176
  /* Pull in all the strings in the strtab as new atoms.  The provisional
177
     strtab must be empty at this point, so there is no need to populate
178
     atoms from it as well.  Types in this subset are frozen and readonly,
179
     so the refs list and movable refs list need not be populated.  */
180
181
0
  for (i = 0; i < fp->ctf_str[CTF_STRTAB_0].cts_len;
182
0
       i += strlen (&fp->ctf_str[CTF_STRTAB_0].cts_strs[i]) + 1)
183
0
    {
184
0
      ctf_str_atom_t *atom;
185
186
0
      if (fp->ctf_str[CTF_STRTAB_0].cts_strs[i] == 0)
187
0
  continue;
188
189
0
      atom = ctf_str_add_ref_internal (fp, &fp->ctf_str[CTF_STRTAB_0].cts_strs[i],
190
0
               0, 0);
191
192
0
      if (!atom)
193
0
  goto oom_str_add;
194
195
0
      atom->csa_offset = i;
196
0
    }
197
198
0
  return 0;
199
200
0
 oom_str_add:
201
0
  ctf_dynhash_destroy (fp->ctf_str_movable_refs);
202
0
  fp->ctf_str_movable_refs = NULL;
203
0
 oom_movable_refs:
204
0
  ctf_dynhash_destroy (fp->ctf_prov_strtab);
205
0
  fp->ctf_prov_strtab = NULL;
206
0
 oom_prov_strtab:
207
0
  ctf_dynhash_destroy (fp->ctf_str_atoms);
208
0
  fp->ctf_str_atoms = NULL;
209
0
  return -ENOMEM;
210
0
}
211
212
/* Destroy the atoms table and associated refs.  */
213
void
214
ctf_str_free_atoms (ctf_dict_t *fp)
215
0
{
216
0
  ctf_dynhash_destroy (fp->ctf_prov_strtab);
217
0
  ctf_dynhash_destroy (fp->ctf_str_atoms);
218
0
  ctf_dynhash_destroy (fp->ctf_str_movable_refs);
219
0
  if (fp->ctf_dynstrtab)
220
0
    {
221
0
      free (fp->ctf_dynstrtab->cts_strs);
222
0
      free (fp->ctf_dynstrtab);
223
0
    }
224
0
}
225
226
0
#define CTF_STR_ADD_REF 0x1
227
0
#define CTF_STR_PROVISIONAL 0x2
228
0
#define CTF_STR_MOVABLE 0x4
229
230
/* Allocate a ref and bind it into a ref list.  */
231
232
static ctf_str_atom_ref_t *
233
aref_create (ctf_dict_t *fp, ctf_str_atom_t *atom, uint32_t *ref, int flags)
234
0
{
235
0
  ctf_str_atom_ref_t *aref;
236
0
  size_t s = sizeof (struct ctf_str_atom_ref);
237
238
0
  if (flags & CTF_STR_MOVABLE)
239
0
    s = sizeof (struct ctf_str_atom_ref_movable);
240
241
0
  aref = malloc (s);
242
243
0
  if (!aref)
244
0
    return NULL;
245
246
0
  aref->caf_ref = ref;
247
248
  /* Movable refs get a backpointer to them in ctf_str_movable_refs, and a
249
     pointer to ctf_str_movable_refs itself in the ref, for use when freeing
250
     refs: they can be moved later in batches via a call to
251
     ctf_str_move_refs.  */
252
253
0
  if (flags & CTF_STR_MOVABLE)
254
0
    {
255
0
      ctf_str_atom_ref_movable_t *movref = (ctf_str_atom_ref_movable_t *) aref;
256
257
0
      movref->caf_movable_refs = fp->ctf_str_movable_refs;
258
259
0
      if (ctf_dynhash_insert (fp->ctf_str_movable_refs, ref, aref) < 0)
260
0
  {
261
0
    free (aref);
262
0
    return NULL;
263
0
  }
264
0
    }
265
266
0
  ctf_list_append (&atom->csa_refs, aref);
267
268
0
  return aref;
269
0
}
270
271
/* Add a string to the atoms table, copying the passed-in string if
272
   necessary.  Return the atom added. Return NULL only when out of memory
273
   (and do not touch the passed-in string in that case).
274
275
   Possibly add a provisional entry for this string to the provisional
276
   strtab.  If the string is in the provisional strtab, update its ref list
277
   with the passed-in ref, causing the ref to be updated when the strtab is
278
   written out.  */
279
280
static ctf_str_atom_t *
281
ctf_str_add_ref_internal (ctf_dict_t *fp, const char *str,
282
        int flags, uint32_t *ref)
283
0
{
284
0
  char *newstr = NULL;
285
0
  ctf_str_atom_t *atom = NULL;
286
0
  int added = 0;
287
288
0
  atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str);
289
290
  /* Existing atoms get refs added only if they are provisional:
291
     non-provisional strings already have a fixed strtab offset, and just
292
     get their ref updated immediately, since its value cannot change.  */
293
294
0
  if (atom)
295
0
    {
296
0
      if (!ctf_dynhash_lookup (fp->ctf_prov_strtab, (void *) (uintptr_t)
297
0
             atom->csa_offset))
298
0
  {
299
0
    if (flags & CTF_STR_ADD_REF)
300
0
      {
301
0
        if (atom->csa_external_offset)
302
0
    *ref = atom->csa_external_offset;
303
0
        else
304
0
    *ref = atom->csa_offset;
305
0
      }
306
0
    return atom;
307
0
  }
308
309
0
      if (flags & CTF_STR_ADD_REF)
310
0
  {
311
0
    if (!aref_create (fp, atom, ref, flags))
312
0
      {
313
0
        ctf_set_errno (fp, ENOMEM);
314
0
        return NULL;
315
0
      }
316
0
  }
317
318
0
      return atom;
319
0
    }
320
321
  /* New atom.  */
322
323
0
  if ((atom = malloc (sizeof (struct ctf_str_atom))) == NULL)
324
0
    goto oom;
325
0
  memset (atom, 0, sizeof (struct ctf_str_atom));
326
327
  /* Don't allocate new strings if this string is within an mmapped
328
     strtab.  */
329
330
0
  if ((unsigned char *) str < (unsigned char *) fp->ctf_data_mmapped
331
0
      || (unsigned char *) str > (unsigned char *) fp->ctf_data_mmapped + fp->ctf_data_mmapped_len)
332
0
    {
333
0
      if ((newstr = strdup (str)) == NULL)
334
0
  goto oom;
335
0
      atom->csa_flags |= CTF_STR_ATOM_FREEABLE;
336
0
      atom->csa_str = newstr;
337
0
    }
338
0
  else
339
0
    atom->csa_str = (char *) str;
340
341
0
  if (ctf_dynhash_insert (fp->ctf_str_atoms, atom->csa_str, atom) < 0)
342
0
    goto oom;
343
0
  added = 1;
344
345
0
  atom->csa_snapshot_id = fp->ctf_snapshots;
346
347
  /* New atoms marked provisional go into the provisional strtab, and get a
348
     ref added.  */
349
350
0
  if (flags & CTF_STR_PROVISIONAL)
351
0
    {
352
0
      atom->csa_offset = fp->ctf_str_prov_offset;
353
354
0
      if (ctf_dynhash_insert (fp->ctf_prov_strtab, (void *) (uintptr_t)
355
0
            atom->csa_offset, (void *) atom->csa_str) < 0)
356
0
  goto oom;
357
358
0
      fp->ctf_str_prov_offset += strlen (atom->csa_str) + 1;
359
360
0
      if (flags & CTF_STR_ADD_REF)
361
0
      {
362
0
  if (!aref_create (fp, atom, ref, flags))
363
0
    goto oom;
364
0
      }
365
0
    }
366
367
0
  return atom;
368
369
0
 oom:
370
0
  if (added)
371
0
    ctf_dynhash_remove (fp->ctf_str_atoms, atom->csa_str);
372
0
  free (atom);
373
0
  free (newstr);
374
0
  ctf_set_errno (fp, ENOMEM);
375
0
  return NULL;
376
0
}
377
378
/* Add a string to the atoms table, without augmenting the ref list for this
379
   string: return a 'provisional offset' which can be used to return this string
380
   until ctf_str_write_strtab is called, or 0 on failure.  (Everywhere the
381
   provisional offset is assigned to should be added as a ref using
382
   ctf_str_add_ref() as well.) */
383
uint32_t
384
ctf_str_add (ctf_dict_t *fp, const char *str)
385
0
{
386
0
  ctf_str_atom_t *atom;
387
388
0
  if (!str)
389
0
    str = "";
390
391
0
  atom = ctf_str_add_ref_internal (fp, str, CTF_STR_PROVISIONAL, 0);
392
0
  if (!atom)
393
0
    return 0;
394
395
0
  return atom->csa_offset;
396
0
}
397
398
/* Like ctf_str_add(), but additionally augment the atom's refs list with the
399
   passed-in ref, whether or not the string is already present.  There is no
400
   attempt to deduplicate the refs list (but duplicates are harmless).  */
401
uint32_t
402
ctf_str_add_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
403
0
{
404
0
  ctf_str_atom_t *atom;
405
406
0
  if (!str)
407
0
    str = "";
408
409
0
  atom = ctf_str_add_ref_internal (fp, str, CTF_STR_ADD_REF
410
0
           | CTF_STR_PROVISIONAL, ref);
411
0
  if (!atom)
412
0
    return 0;
413
414
0
  return atom->csa_offset;
415
0
}
416
417
/* Like ctf_str_add_ref(), but note that the ref may be moved later on.  */
418
uint32_t
419
ctf_str_add_movable_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
420
0
{
421
0
  ctf_str_atom_t *atom;
422
423
0
  if (!str)
424
0
    str = "";
425
426
0
  atom = ctf_str_add_ref_internal (fp, str, CTF_STR_ADD_REF
427
0
           | CTF_STR_PROVISIONAL
428
0
           | CTF_STR_MOVABLE, ref);
429
0
  if (!atom)
430
0
    return 0;
431
432
0
  return atom->csa_offset;
433
0
}
434
435
/* Add an external strtab reference at OFFSET.  Returns zero if the addition
436
   failed, nonzero otherwise.  */
437
int
438
ctf_str_add_external (ctf_dict_t *fp, const char *str, uint32_t offset)
439
0
{
440
0
  ctf_str_atom_t *atom;
441
442
0
  if (!str)
443
0
    str = "";
444
445
0
  atom = ctf_str_add_ref_internal (fp, str, 0, 0);
446
0
  if (!atom)
447
0
    return 0;
448
449
0
  atom->csa_external_offset = CTF_SET_STID (offset, CTF_STRTAB_1);
450
451
0
  if (!fp->ctf_syn_ext_strtab)
452
0
    fp->ctf_syn_ext_strtab = ctf_dynhash_create (ctf_hash_integer,
453
0
             ctf_hash_eq_integer,
454
0
             NULL, NULL);
455
0
  if (!fp->ctf_syn_ext_strtab)
456
0
    {
457
0
      ctf_set_errno (fp, ENOMEM);
458
0
      return 0;
459
0
    }
460
461
0
  if (ctf_dynhash_insert (fp->ctf_syn_ext_strtab,
462
0
        (void *) (uintptr_t)
463
0
        atom->csa_external_offset,
464
0
        (void *) atom->csa_str) < 0)
465
0
    {
466
      /* No need to bother freeing the syn_ext_strtab: it will get freed at
467
   ctf_str_write_strtab time if unreferenced.  */
468
0
      ctf_set_errno (fp, ENOMEM);
469
0
      return 0;
470
0
    }
471
472
0
  return 1;
473
0
}
474
475
/* Note that refs have moved from (SRC, LEN) to DEST.  We use the movable
476
   refs backpointer for this, because it is done an amortized-constant
477
   number of times during structure member and enumerand addition, and if we
478
   did a linear search this would turn such addition into an O(n^2)
479
   operation.  Even this is not linear, but it's better than that.  */
480
int
481
ctf_str_move_refs (ctf_dict_t *fp, void *src, size_t len, void *dest)
482
0
{
483
0
  uintptr_t p;
484
485
0
  if (src == dest)
486
0
    return 0;
487
488
0
  for (p = (uintptr_t) src; p - (uintptr_t) src < len; p++)
489
0
    {
490
0
      ctf_str_atom_ref_t *ref;
491
492
0
      if ((ref = ctf_dynhash_lookup (fp->ctf_str_movable_refs,
493
0
             (ctf_str_atom_ref_t *) p)) != NULL)
494
0
  {
495
0
    int out_of_memory;
496
497
0
    ref->caf_ref = (uint32_t *) (((uintptr_t) ref->caf_ref +
498
0
          (uintptr_t) dest - (uintptr_t) src));
499
0
    ctf_dynhash_remove (fp->ctf_str_movable_refs,
500
0
            (ctf_str_atom_ref_t *) p);
501
0
    out_of_memory = ctf_dynhash_insert (fp->ctf_str_movable_refs,
502
0
                ref->caf_ref, ref);
503
0
    assert (out_of_memory == 0);
504
0
  }
505
0
    }
506
507
0
  return 0;
508
0
}
509
510
/* Remove a single ref.  */
511
void
512
ctf_str_remove_ref (ctf_dict_t *fp, const char *str, uint32_t *ref)
513
0
{
514
0
  ctf_str_atom_ref_t *aref, *anext;
515
0
  ctf_str_atom_t *atom = NULL;
516
517
0
  atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str);
518
0
  if (!atom)
519
0
    return;
520
521
0
  for (aref = ctf_list_next (&atom->csa_refs); aref != NULL; aref = anext)
522
0
    {
523
0
      anext = ctf_list_next (aref);
524
0
      if (aref->caf_ref == ref)
525
0
  {
526
0
    ctf_list_delete (&atom->csa_refs, aref);
527
0
    free (aref);
528
0
  }
529
0
    }
530
0
}
531
532
/* A ctf_dynhash_iter_remove() callback that removes atoms later than a given
533
   snapshot ID.  External atoms are never removed, because they came from the
534
   linker string table and are still present even if you roll back type
535
   additions.  */
536
static int
537
ctf_str_rollback_atom (void *key _libctf_unused_, void *value, void *arg)
538
0
{
539
0
  ctf_str_atom_t *atom = (ctf_str_atom_t *) value;
540
0
  ctf_snapshot_id_t *id = (ctf_snapshot_id_t *) arg;
541
542
0
  return (atom->csa_snapshot_id > id->snapshot_id)
543
0
    && (atom->csa_external_offset == 0);
544
0
}
545
546
/* Roll back, deleting all (internal) atoms created after a particular ID.  */
547
void
548
ctf_str_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id)
549
0
{
550
0
  ctf_dynhash_iter_remove (fp->ctf_str_atoms, ctf_str_rollback_atom, &id);
551
0
}
552
553
/* An adaptor around ctf_purge_atom_refs.  */
554
static void
555
ctf_str_purge_one_atom_refs (void *key _libctf_unused_, void *value,
556
           void *arg _libctf_unused_)
557
0
{
558
0
  ctf_str_atom_t *atom = (ctf_str_atom_t *) value;
559
0
  ctf_str_purge_atom_refs (atom);
560
0
}
561
562
/* Remove all the recorded refs from the atoms table.  */
563
void
564
ctf_str_purge_refs (ctf_dict_t *fp)
565
0
{
566
0
  ctf_dynhash_iter (fp->ctf_str_atoms, ctf_str_purge_one_atom_refs, NULL);
567
0
}
568
569
/* Update a list of refs to the specified value. */
570
static void
571
ctf_str_update_refs (ctf_str_atom_t *refs, uint32_t value)
572
0
{
573
0
  ctf_str_atom_ref_t *ref;
574
575
0
  for (ref = ctf_list_next (&refs->csa_refs); ref != NULL;
576
0
       ref = ctf_list_next (ref))
577
0
    *(ref->caf_ref) = value;
578
0
}
579
580
/* Sort the strtab.  */
581
static int
582
ctf_str_sort_strtab (const void *a, const void *b)
583
0
{
584
0
  ctf_str_atom_t **one = (ctf_str_atom_t **) a;
585
0
  ctf_str_atom_t **two = (ctf_str_atom_t **) b;
586
587
0
  return (strcmp ((*one)->csa_str, (*two)->csa_str));
588
0
}
589
590
/* Write out and return a strtab containing all strings with recorded refs,
591
   adjusting the refs to refer to the corresponding string.  The returned
592
   strtab is already assigned to strtab 0 in this dict, is owned by this
593
   dict, and may be NULL on error.  Also populate the synthetic strtab with
594
   mappings from external strtab offsets to names, so we can look them up
595
   with ctf_strptr().  Only external strtab offsets with references are
596
   added.
597
598
   As a side effect, replaces the strtab of the current dict with the newly-
599
   generated strtab.  This is an exception to the general rule that
600
   serialization does not change the dict passed in, because the alternative
601
   is to copy the entire atoms table on every reserialization just to avoid
602
   modifying the original, which is excessively costly for minimal gain.
603
604
   We use the lazy man's approach and double memory costs by always storing
605
   atoms as individually allocated entities whenever they come from anywhere
606
   but a freshly-opened, mmapped dict, even though after serialization there
607
   is another copy in the strtab; this ensures that ctf_strptr()-returned
608
   pointers to them remain valid for the lifetime of the dict.
609
610
   This is all rendered more complex because if a dict is ctf_open()ed it
611
   will have a bunch of strings in its strtab already, and their strtab
612
   offsets can never change (without piles of complexity to rescan the
613
   entire dict just to get all the offsets to all of them into the atoms
614
   table).  Entries below the existing strtab limit are just copied into the
615
   new dict: entries above it are new, and are are sorted first, then
616
   appended to it.  The sorting is purely a compression-efficiency
617
   improvement, and we get nearly as good an improvement from sorting big
618
   chunks like this as we would from sorting the whole thing.  */
619
620
const ctf_strs_writable_t *
621
ctf_str_write_strtab (ctf_dict_t *fp)
622
0
{
623
0
  ctf_strs_writable_t *strtab;
624
0
  size_t strtab_count = 0;
625
0
  uint32_t cur_stroff = 0;
626
0
  ctf_str_atom_t **sorttab;
627
0
  ctf_next_t *it = NULL;
628
0
  size_t i;
629
0
  void *v;
630
0
  int err;
631
0
  int new_strtab = 0;
632
0
  int any_external = 0;
633
634
0
  strtab = calloc (1, sizeof (ctf_strs_writable_t));
635
0
  if (!strtab)
636
0
    return NULL;
637
638
  /* The strtab contains the existing string table at its start: figure out
639
     how many new strings we need to add.  We only need to add new strings
640
     that have no external offset, that have refs, and that are found in the
641
     provisional strtab.  If the existing strtab is empty we also need to
642
     add the null string at its start.  */
643
644
0
  strtab->cts_len = fp->ctf_str[CTF_STRTAB_0].cts_len;
645
646
0
  if (strtab->cts_len == 0)
647
0
    {
648
0
      new_strtab = 1;
649
0
      strtab->cts_len++;      /* For the \0.  */
650
0
    }
651
652
  /* Count new entries in the strtab: i.e. entries in the provisional
653
     strtab.  Ignore any entry for \0, entries which ended up in the
654
     external strtab, and unreferenced entries.  */
655
656
0
  while ((err = ctf_dynhash_next (fp->ctf_prov_strtab, &it, NULL, &v)) == 0)
657
0
    {
658
0
      const char *str = (const char *) v;
659
0
      ctf_str_atom_t *atom;
660
661
0
      atom = ctf_dynhash_lookup (fp->ctf_str_atoms, str);
662
0
      if (!ctf_assert (fp, atom))
663
0
  goto err_strtab;
664
665
0
      if (atom->csa_str[0] == 0 || ctf_list_empty_p (&atom->csa_refs) ||
666
0
    atom->csa_external_offset)
667
0
  continue;
668
669
0
      strtab->cts_len += strlen (atom->csa_str) + 1;
670
0
      strtab_count++;
671
0
    }
672
0
  if (err != ECTF_NEXT_END)
673
0
    {
674
0
      ctf_dprintf ("ctf_str_write_strtab: error counting strtab entries: %s\n",
675
0
       ctf_errmsg (err));
676
0
      goto err_strtab;
677
0
    }
678
679
0
  ctf_dprintf ("%lu bytes of strings in strtab: %lu pre-existing.\n",
680
0
         (unsigned long) strtab->cts_len,
681
0
         (unsigned long) fp->ctf_str[CTF_STRTAB_0].cts_len);
682
683
  /* Sort the new part of the strtab.  */
684
685
0
  sorttab = calloc (strtab_count, sizeof (ctf_str_atom_t *));
686
0
  if (!sorttab)
687
0
    {
688
0
      ctf_set_errno (fp, ENOMEM);
689
0
      goto err_strtab;
690
0
    }
691
692
0
  i = 0;
693
0
  while ((err = ctf_dynhash_next (fp->ctf_prov_strtab, &it, NULL, &v)) == 0)
694
0
    {
695
0
      ctf_str_atom_t *atom;
696
697
0
      atom = ctf_dynhash_lookup (fp->ctf_str_atoms, v);
698
0
      if (!ctf_assert (fp, atom))
699
0
  goto err_sorttab;
700
701
0
      if (atom->csa_str[0] == 0 || ctf_list_empty_p (&atom->csa_refs) ||
702
0
    atom->csa_external_offset)
703
0
  continue;
704
705
0
      sorttab[i++] = atom;
706
0
    }
707
708
0
  qsort (sorttab, strtab_count, sizeof (ctf_str_atom_t *),
709
0
   ctf_str_sort_strtab);
710
711
0
  if ((strtab->cts_strs = malloc (strtab->cts_len)) == NULL)
712
0
    goto err_sorttab;
713
714
0
  cur_stroff = fp->ctf_str[CTF_STRTAB_0].cts_len;
715
716
0
  if (new_strtab)
717
0
    {
718
0
      strtab->cts_strs[0] = 0;
719
0
      cur_stroff++;
720
0
    }
721
0
  else
722
0
    memcpy (strtab->cts_strs, fp->ctf_str[CTF_STRTAB_0].cts_strs,
723
0
      fp->ctf_str[CTF_STRTAB_0].cts_len);
724
725
  /* Work over the sorttab, add its strings to the strtab, and remember
726
     where they are in the csa_offset for the appropriate atom.  No ref
727
     updating is done at this point, because refs might well relate to
728
     already-existing strings, or external strings, which do not need adding
729
     to the strtab and may not be in the sorttab.  */
730
731
0
  for (i = 0; i < strtab_count; i++)
732
0
    {
733
0
      sorttab[i]->csa_offset = cur_stroff;
734
0
      strcpy (&strtab->cts_strs[cur_stroff], sorttab[i]->csa_str);
735
0
      cur_stroff += strlen (sorttab[i]->csa_str) + 1;
736
0
    }
737
0
  free (sorttab);
738
0
  sorttab = NULL;
739
740
  /* Update all refs, then purge them as no longer necessary: also update
741
     the strtab appropriately.  */
742
743
0
  while ((err = ctf_dynhash_next (fp->ctf_str_atoms, &it, NULL, &v)) == 0)
744
0
    {
745
0
      ctf_str_atom_t *atom = (ctf_str_atom_t *) v;
746
0
      uint32_t offset;
747
748
0
      if (ctf_list_empty_p (&atom->csa_refs))
749
0
  continue;
750
751
0
      if (atom->csa_external_offset)
752
0
  {
753
0
    any_external = 1;
754
0
    offset = atom->csa_external_offset;
755
0
  }
756
0
      else
757
0
  offset = atom->csa_offset;
758
0
      ctf_str_update_refs (atom, offset);
759
0
    }
760
0
  if (err != ECTF_NEXT_END)
761
0
    {
762
0
      ctf_dprintf ("ctf_str_write_strtab: error iterating over atoms while updating refs: %s\n",
763
0
       ctf_errmsg (err));
764
0
      goto err_strtab;
765
0
    }
766
0
  ctf_str_purge_refs (fp);
767
768
0
  if (!any_external)
769
0
    {
770
0
      ctf_dynhash_destroy (fp->ctf_syn_ext_strtab);
771
0
      fp->ctf_syn_ext_strtab = NULL;
772
0
    }
773
774
  /* Replace the old strtab with the new one in this dict.  */
775
776
0
  if (fp->ctf_dynstrtab)
777
0
    {
778
0
      free (fp->ctf_dynstrtab->cts_strs);
779
0
      free (fp->ctf_dynstrtab);
780
0
    }
781
782
0
  fp->ctf_dynstrtab = strtab;
783
0
  fp->ctf_str[CTF_STRTAB_0].cts_strs = strtab->cts_strs;
784
0
  fp->ctf_str[CTF_STRTAB_0].cts_len = strtab->cts_len;
785
786
  /* All the provisional strtab entries are now real strtab entries, and
787
     ctf_strptr() will find them there.  The provisional offset now starts right
788
     beyond the new end of the strtab.  */
789
790
0
  ctf_dynhash_empty (fp->ctf_prov_strtab);
791
0
  fp->ctf_str_prov_offset = strtab->cts_len + 1;
792
0
  return strtab;
793
794
0
 err_sorttab:
795
0
  free (sorttab);
796
0
 err_strtab:
797
0
  free (strtab);
798
0
  return NULL;
799
0
}