2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
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
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.
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/>. */
21 #include <sys/types.h>
24 #include "ctf-endian.h"
35 static off_t
arc_write_one_ctf (ctf_file_t
* f
, int fd
, size_t threshold
);
36 static ctf_file_t
*ctf_arc_open_by_offset (const struct ctf_archive
*arc
,
37 const ctf_sect_t
*symsect
,
38 const ctf_sect_t
*strsect
,
39 size_t offset
, int *errp
);
40 static int sort_modent_by_name (const void *one
, const void *two
, void *n
);
41 static void *arc_mmap_header (int fd
, size_t headersz
);
42 static void *arc_mmap_file (int fd
, size_t size
);
43 static int arc_mmap_writeout (int fd
, void *header
, size_t headersz
,
45 static int arc_mmap_unmap (void *header
, size_t headersz
, const char **errmsg
);
47 /* bsearch() internal state. */
48 static __thread
char *search_nametbl
;
50 /* Write out a CTF archive. The entries in CTF_FILES are referenced by name:
51 the names are passed in the names array, which must have CTF_FILES entries.
53 Returns 0 on success, or an errno, or an ECTF_* value. */
55 ctf_arc_write (const char *file
, ctf_file_t
** ctf_files
, size_t ctf_file_cnt
,
56 const char **names
, size_t threshold
)
59 struct ctf_archive
*archdr
;
65 size_t ctf_startoffs
; /* Start of the section we are working over. */
66 char *nametbl
= NULL
; /* The name table. */
69 struct ctf_archive_modent
*modent
;
71 ctf_dprintf ("Writing archive %s with %zi files\n", file
, ctf_file_cnt
);
73 if ((fd
= open (file
, O_RDWR
| O_CREAT
| O_TRUNC
| O_CLOEXEC
, 0666)) < 0)
75 errmsg
= "ctf_arc_write(): cannot create %s: %s\n";
79 /* Figure out the size of the mmap()ed header, including the
80 ctf_archive_modent array. We assume that all of this needs no
81 padding: a likely assumption, given that it's all made up of
83 headersz
= sizeof (struct ctf_archive
)
84 + (ctf_file_cnt
* sizeof (uint64_t) * 2);
85 ctf_dprintf ("headersz is %zi\n", headersz
);
87 /* From now on we work in two pieces: an mmap()ed region from zero up to the
88 headersz, and a region updated via write() starting after that, containing
89 all the tables. Platforms that do not support mmap() just use write(). */
90 ctf_startoffs
= headersz
;
91 if (lseek (fd
, ctf_startoffs
- 1, SEEK_SET
) < 0)
93 errmsg
= "ctf_arc_write(): cannot extend file while writing %s: %s\n";
97 if (write (fd
, &dummy
, 1) < 0)
99 errmsg
= "ctf_arc_write(): cannot extend file while writing %s: %s\n";
103 if ((archdr
= arc_mmap_header (fd
, headersz
)) == NULL
)
105 errmsg
= "ctf_arc_write(): Cannot mmap() %s: %s\n";
109 /* Fill in everything we can, which is everything other than the name
111 archdr
->ctfa_magic
= htole64 (CTFA_MAGIC
);
112 archdr
->ctfa_nfiles
= htole64 (ctf_file_cnt
);
113 archdr
->ctfa_ctfs
= htole64 (ctf_startoffs
);
115 /* We could validate that all CTF files have the same data model, but
116 since any reasonable construction process will be building things of
117 only one bitness anyway, this is pretty pointless, so just use the
118 model of the first CTF file for all of them. (It *is* valid to
119 create an empty archive: the value of ctfa_model is irrelevant in
120 this case, but we must be sure not to dereference uninitialized
123 if (ctf_file_cnt
> 0)
124 archdr
->ctfa_model
= htole64 (ctf_getmodel (ctf_files
[0]));
126 /* Now write out the CTFs: ctf_archive_modent array via the mapping,
127 ctfs via write(). The names themselves have not been written yet: we
128 track them in a local strtab until the time is right, and sort the
129 modents array after construction.
131 The name table is not sorted. */
133 for (i
= 0, namesz
= 0; i
< le64toh (archdr
->ctfa_nfiles
); i
++)
134 namesz
+= strlen (names
[i
]) + 1;
136 nametbl
= malloc (namesz
);
139 errmsg
= "Error writing named CTF to %s: %s\n";
143 for (i
= 0, namesz
= 0,
144 modent
= (ctf_archive_modent_t
*) ((char *) archdr
145 + sizeof (struct ctf_archive
));
146 i
< le64toh (archdr
->ctfa_nfiles
); i
++)
150 strcpy (&nametbl
[namesz
], names
[i
]);
152 off
= arc_write_one_ctf (ctf_files
[i
], fd
, threshold
);
153 if ((off
< 0) && (off
> -ECTF_BASE
))
155 errmsg
= "ctf_arc_write(): Cannot determine file "
156 "position while writing %s: %s";
161 errmsg
= "ctf_arc_write(): Cannot write CTF file to %s: %s\n";
166 modent
->name_offset
= htole64 (namesz
);
167 modent
->ctf_offset
= htole64 (off
- ctf_startoffs
);
168 namesz
+= strlen (names
[i
]) + 1;
172 qsort_r ((ctf_archive_modent_t
*) ((char *) archdr
173 + sizeof (struct ctf_archive
)),
174 le64toh (archdr
->ctfa_nfiles
),
175 sizeof (struct ctf_archive_modent
), sort_modent_by_name
, nametbl
);
177 /* Now the name table. */
179 if ((nameoffs
= lseek (fd
, 0, SEEK_CUR
)) < 0)
181 errmsg
= "ctf_arc_write(): Cannot get current file position "
185 archdr
->ctfa_names
= htole64 (nameoffs
);
190 if ((len
= write (fd
, np
, namesz
)) < 0)
192 errmsg
= "ctf_arc_write(): Cannot write name table in %s: %s\n";
200 if (arc_mmap_writeout (fd
, archdr
, headersz
, &errmsg
) < 0)
202 if (arc_mmap_unmap (archdr
, headersz
, &errmsg
) < 0)
206 errmsg
= "ctf_arc_write(): Cannot close after writing to %s: %s\n";
215 arc_mmap_unmap (archdr
, headersz
, NULL
);
221 ctf_dprintf (errmsg
, file
, errno
< ECTF_BASE
? strerror (errno
) :
226 /* Write one CTF file out. Return the file position of the written file (or
227 rather, of the file-size uint64_t that precedes it): negative return is a
228 negative errno or ctf_errno value. On error, the file position may no longer
229 be at the end of the file. */
231 arc_write_one_ctf (ctf_file_t
* f
, int fd
, size_t threshold
)
237 int (*writefn
) (ctf_file_t
* fp
, int fd
);
239 if ((off
= lseek (fd
, 0, SEEK_CUR
)) < 0)
242 if (f
->ctf_size
> threshold
)
243 writefn
= ctf_compress_write
;
247 /* This zero-write turns into the size in a moment. */
248 ctfsz_len
= sizeof (ctfsz
);
249 ctfszp
= (char *) &ctfsz
;
250 while (ctfsz_len
> 0)
252 ssize_t writelen
= write (fd
, ctfszp
, ctfsz_len
);
255 ctfsz_len
-= writelen
;
259 if (writefn (f
, fd
) != 0)
260 return f
->ctf_errno
* -1;
262 if ((end_off
= lseek (fd
, 0, SEEK_CUR
)) < 0)
264 ctfsz
= htole64 (end_off
- off
);
266 if ((lseek (fd
, off
, SEEK_SET
)) < 0)
270 ctfsz_len
= sizeof (ctfsz
);
271 ctfszp
= (char *) &ctfsz
;
272 while (ctfsz_len
> 0)
274 ssize_t writelen
= write (fd
, ctfszp
, ctfsz_len
);
277 ctfsz_len
-= writelen
;
281 end_off
= LCTF_ALIGN_OFFS (end_off
, 8);
282 if ((lseek (fd
, end_off
, SEEK_SET
)) < 0)
288 /* qsort() function to sort the array of struct ctf_archive_modents into
289 ascending name order. */
291 sort_modent_by_name (const void *one
, const void *two
, void *n
)
293 const struct ctf_archive_modent
*a
= one
;
294 const struct ctf_archive_modent
*b
= two
;
297 return strcmp (&nametbl
[le64toh (a
->name_offset
)],
298 &nametbl
[le64toh (b
->name_offset
)]);
301 /* bsearch() function to search for a given name in the sorted array of struct
302 ctf_archive_modents. */
304 search_modent_by_name (const void *key
, const void *ent
)
307 const struct ctf_archive_modent
*v
= ent
;
309 return strcmp (k
, &search_nametbl
[le64toh (v
->name_offset
)]);
312 /* A trivial wrapper: open a CTF archive, from data in a buffer (which the
313 caller must preserve until ctf_arc_close() time). Returns the archive, or
314 NULL and an error in *err (if not NULL). */
316 ctf_arc_bufopen (const void *buf
, size_t size _libctf_unused_
, int *errp
)
318 struct ctf_archive
*arc
= (struct ctf_archive
*) buf
;
320 if (le64toh (arc
->ctfa_magic
) != CTFA_MAGIC
)
329 /* Open a CTF archive. Returns the archive, or NULL and an error in *err (if
332 ctf_arc_open_internal (const char *filename
, int *errp
)
337 struct ctf_archive
*arc
; /* (Actually the whole file.) */
340 if ((fd
= open (filename
, O_RDONLY
)) < 0)
342 errmsg
= "ctf_arc_open(): cannot open %s: %s\n";
345 if (fstat (fd
, &s
) < 0)
347 errmsg
= "ctf_arc_open(): cannot stat %s: %s\n";
351 if ((arc
= arc_mmap_file (fd
, s
.st_size
)) == NULL
)
353 errmsg
= "ctf_arc_open(): Cannot read in %s: %s\n";
357 if (le64toh (arc
->ctfa_magic
) != CTFA_MAGIC
)
359 errmsg
= "ctf_arc_open(): Invalid magic number";
364 /* This horrible hack lets us know how much to unmap when the file is
365 closed. (We no longer need the magic number, and the mapping
367 arc
->ctfa_magic
= s
.st_size
;
372 arc_mmap_unmap (arc
, s
.st_size
, NULL
);
378 ctf_dprintf (errmsg
, filename
, errno
< ECTF_BASE
? strerror (errno
) :
383 /* Close an archive. */
385 ctf_arc_close_internal (struct ctf_archive
*arc
)
390 /* See the comment in ctf_arc_open(). */
391 arc_mmap_unmap (arc
, arc
->ctfa_magic
, NULL
);
394 /* Public entry point: close an archive, or CTF file. */
396 ctf_arc_close (ctf_archive_t
*arc
)
401 if (arc
->ctfi_is_archive
)
402 ctf_arc_close_internal (arc
->ctfi_archive
);
404 ctf_file_close (arc
->ctfi_file
);
405 free ((void *) arc
->ctfi_symsect
.cts_data
);
406 free ((void *) arc
->ctfi_strsect
.cts_data
);
407 free (arc
->ctfi_data
);
411 /* Return the ctf_file_t with the given name, or NULL if none, setting 'err' if
412 non-NULL. A name of NULL means to open the default file. */
414 ctf_arc_open_by_name_internal (const struct ctf_archive
*arc
,
415 const ctf_sect_t
*symsect
,
416 const ctf_sect_t
*strsect
,
417 const char *name
, int *errp
)
419 struct ctf_archive_modent
*modent
;
422 name
= _CTF_SECTION
; /* The default name. */
424 ctf_dprintf ("ctf_arc_open_by_name(%s): opening\n", name
);
426 modent
= (ctf_archive_modent_t
*) ((char *) arc
427 + sizeof (struct ctf_archive
));
429 search_nametbl
= (char *) arc
+ le64toh (arc
->ctfa_names
);
430 modent
= bsearch (name
, modent
, le64toh (arc
->ctfa_nfiles
),
431 sizeof (struct ctf_archive_modent
),
432 search_modent_by_name
);
434 /* This is actually a common case and normal operation: no error
439 *errp
= ECTF_ARNNAME
;
443 return ctf_arc_open_by_offset (arc
, symsect
, strsect
,
444 le64toh (modent
->ctf_offset
), errp
);
447 /* Return the ctf_file_t with the given name, or NULL if none, setting 'err' if
448 non-NULL. A name of NULL means to open the default file.
450 Use the specified string and symbol table sections.
452 Public entry point. */
454 ctf_arc_open_by_name_sections (const ctf_archive_t
*arc
,
455 const ctf_sect_t
*symsect
,
456 const ctf_sect_t
*strsect
,
460 if (arc
->ctfi_is_archive
)
463 ret
= ctf_arc_open_by_name_internal (arc
->ctfi_archive
, symsect
, strsect
,
466 ret
->ctf_archive
= (ctf_archive_t
*) arc
;
470 if ((name
!= NULL
) && (strcmp (name
, _CTF_SECTION
) != 0))
473 *errp
= ECTF_ARNNAME
;
476 arc
->ctfi_file
->ctf_archive
= (ctf_archive_t
*) arc
;
478 /* Bump the refcount so that the user can ctf_file_close() it. */
479 arc
->ctfi_file
->ctf_refcnt
++;
480 return arc
->ctfi_file
;
483 /* Return the ctf_file_t with the given name, or NULL if none, setting 'err' if
484 non-NULL. A name of NULL means to open the default file.
486 Public entry point. */
488 ctf_arc_open_by_name (const ctf_archive_t
*arc
, const char *name
, int *errp
)
490 const ctf_sect_t
*symsect
= &arc
->ctfi_symsect
;
491 const ctf_sect_t
*strsect
= &arc
->ctfi_strsect
;
493 if (symsect
->cts_name
== NULL
)
495 if (strsect
->cts_name
== NULL
)
498 return ctf_arc_open_by_name_sections (arc
, symsect
, strsect
, name
, errp
);
501 /* Return the ctf_file_t at the given ctfa_ctfs-relative offset, or NULL if
502 none, setting 'err' if non-NULL. */
504 ctf_arc_open_by_offset (const struct ctf_archive
*arc
,
505 const ctf_sect_t
*symsect
,
506 const ctf_sect_t
*strsect
, size_t offset
,
512 ctf_dprintf ("ctf_arc_open_by_offset(%zi): opening\n", offset
);
514 memset (&ctfsect
, 0, sizeof (ctf_sect_t
));
516 offset
+= le64toh (arc
->ctfa_ctfs
);
518 ctfsect
.cts_name
= _CTF_SECTION
;
519 ctfsect
.cts_size
= le64toh (*((uint64_t *) ((char *) arc
+ offset
)));
520 ctfsect
.cts_entsize
= 1;
521 ctfsect
.cts_data
= (void *) ((char *) arc
+ offset
+ sizeof (uint64_t));
522 fp
= ctf_bufopen (&ctfsect
, symsect
, strsect
, errp
);
524 ctf_setmodel (fp
, le64toh (arc
->ctfa_model
));
528 /* Raw iteration over all CTF files in an archive. We pass the raw data for all
529 CTF files in turn to the specified callback function. */
531 ctf_archive_raw_iter_internal (const struct ctf_archive
*arc
,
532 ctf_archive_raw_member_f
*func
, void *data
)
536 struct ctf_archive_modent
*modent
;
539 modent
= (ctf_archive_modent_t
*) ((char *) arc
540 + sizeof (struct ctf_archive
));
541 nametbl
= (((const char *) arc
) + le64toh (arc
->ctfa_names
));
543 for (i
= 0; i
< le64toh (arc
->ctfa_nfiles
); i
++)
548 name
= &nametbl
[le64toh (modent
[i
].name_offset
)];
549 fp
= ((char *) arc
+ le64toh (arc
->ctfa_ctfs
)
550 + le64toh (modent
[i
].ctf_offset
));
552 if ((rc
= func (name
, (void *) (fp
+ sizeof (uint64_t)),
553 le64toh (*((uint64_t *) fp
)), data
)) != 0)
559 /* Raw iteration over all CTF files in an archive: public entry point.
561 Returns -EINVAL if not supported for this sort of archive. */
563 ctf_archive_raw_iter (const ctf_archive_t
*arc
,
564 ctf_archive_raw_member_f
* func
, void *data
)
566 if (arc
->ctfi_is_archive
)
567 return ctf_archive_raw_iter_internal (arc
->ctfi_archive
, func
, data
);
569 return -EINVAL
; /* Not supported. */
572 /* Iterate over all CTF files in an archive. We pass all CTF files in turn to
573 the specified callback function. */
575 ctf_archive_iter_internal (const ctf_archive_t
*wrapper
,
576 const struct ctf_archive
*arc
,
577 const ctf_sect_t
*symsect
,
578 const ctf_sect_t
*strsect
,
579 ctf_archive_member_f
*func
, void *data
)
584 struct ctf_archive_modent
*modent
;
587 modent
= (ctf_archive_modent_t
*) ((char *) arc
588 + sizeof (struct ctf_archive
));
589 nametbl
= (((const char *) arc
) + le64toh (arc
->ctfa_names
));
591 for (i
= 0; i
< le64toh (arc
->ctfa_nfiles
); i
++)
595 name
= &nametbl
[le64toh (modent
[i
].name_offset
)];
596 if ((f
= ctf_arc_open_by_name_internal (arc
, symsect
, strsect
,
600 f
->ctf_archive
= (ctf_archive_t
*) wrapper
;
601 if ((rc
= func (f
, name
, data
)) != 0)
612 /* Iterate over all CTF files in an archive: public entry point. We pass all
613 CTF files in turn to the specified callback function. */
615 ctf_archive_iter (const ctf_archive_t
*arc
, ctf_archive_member_f
*func
,
618 const ctf_sect_t
*symsect
= &arc
->ctfi_symsect
;
619 const ctf_sect_t
*strsect
= &arc
->ctfi_strsect
;
621 if (symsect
->cts_name
== NULL
)
623 if (strsect
->cts_name
== NULL
)
626 if (arc
->ctfi_is_archive
)
627 return ctf_archive_iter_internal (arc
, arc
->ctfi_archive
, symsect
, strsect
,
630 return func (arc
->ctfi_file
, _CTF_SECTION
, data
);
634 /* Map the header in. Only used on new, empty files. */
635 static void *arc_mmap_header (int fd
, size_t headersz
)
638 if ((hdr
= mmap (NULL
, headersz
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
,
644 /* mmap() the whole file, for reading only. (Map it writably, but privately: we
645 need to modify the region, but don't need anyone else to see the
647 static void *arc_mmap_file (int fd
, size_t size
)
650 if ((arc
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
651 fd
, 0)) == MAP_FAILED
)
656 /* Persist the header to disk. */
657 static int arc_mmap_writeout (int fd _libctf_unused_
, void *header
,
658 size_t headersz
, const char **errmsg
)
660 if (msync (header
, headersz
, MS_ASYNC
) < 0)
663 *errmsg
= "arc_mmap_writeout(): Cannot sync after writing to %s: %s\n";
669 /* Unmap the region. */
670 static int arc_mmap_unmap (void *header
, size_t headersz
, const char **errmsg
)
672 if (munmap (header
, headersz
) < 0)
675 *errmsg
= "arc_mmap_munmap(): Cannot unmap after writing to %s: %s\n";
681 /* Map the header in. Only used on new, empty files. */
682 static void *arc_mmap_header (int fd
, size_t headersz
)
685 if ((hdr
= malloc (headersz
)) == NULL
)
690 /* Pull in the whole file, for reading only. We assume the current file
691 position is at the start of the file. */
692 static void *arc_mmap_file (int fd
, size_t size
)
696 if ((data
= malloc (size
)) == NULL
)
699 if (ctf_pread (fd
, data
, size
, 0) < 0)
707 /* Persist the header to disk. */
708 static int arc_mmap_writeout (int fd
, void *header
, size_t headersz
,
713 char *data
= (char *) header
;
714 ssize_t count
= headersz
;
716 if ((lseek (fd
, 0, SEEK_SET
)) < 0)
719 *errmsg
= "arc_mmap_writeout(): Cannot seek while writing header to "
726 if ((len
= write (fd
, data
, count
)) < 0)
729 *errmsg
= "arc_mmap_writeout(): Cannot write header to %s: %s\n";
736 if (len
== 0) /* EOF. */
745 /* Unmap the region. */
746 static int arc_mmap_unmap (void *header
, size_t headersz _libctf_unused_
,
747 const char **errmsg _libctf_unused_
)
This page took 0.060752 seconds and 5 git commands to generate.