2 Copyright (C) 2019-2020 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 /* Write out a CTF archive to the start of the file referenced by the passed-in
48 fd. The entries in CTF_FILES are referenced by name: the names are passed in
49 the names array, which must have CTF_FILES entries.
51 Returns 0 on success, or an errno, or an ECTF_* value. */
53 ctf_arc_write_fd (int fd
, ctf_file_t
**ctf_files
, size_t ctf_file_cnt
,
54 const char **names
, size_t threshold
)
57 struct ctf_archive
*archdr
;
62 size_t ctf_startoffs
; /* Start of the section we are working over. */
63 char *nametbl
= NULL
; /* The name table. */
66 struct ctf_archive_modent
*modent
;
68 ctf_dprintf ("Writing CTF archive with %lu files\n",
69 (unsigned long) ctf_file_cnt
);
71 /* Figure out the size of the mmap()ed header, including the
72 ctf_archive_modent array. We assume that all of this needs no
73 padding: a likely assumption, given that it's all made up of
75 headersz
= sizeof (struct ctf_archive
)
76 + (ctf_file_cnt
* sizeof (uint64_t) * 2);
77 ctf_dprintf ("headersz is %lu\n", (unsigned long) headersz
);
79 /* From now on we work in two pieces: an mmap()ed region from zero up to the
80 headersz, and a region updated via write() starting after that, containing
81 all the tables. Platforms that do not support mmap() just use write(). */
82 ctf_startoffs
= headersz
;
83 if (lseek (fd
, ctf_startoffs
- 1, SEEK_SET
) < 0)
85 errmsg
= "ctf_arc_write(): cannot extend file while writing: %s\n";
89 if (write (fd
, &dummy
, 1) < 0)
91 errmsg
= "ctf_arc_write(): cannot extend file while writing: %s\n";
95 if ((archdr
= arc_mmap_header (fd
, headersz
)) == NULL
)
97 errmsg
= "ctf_arc_write(): Cannot mmap(): %s\n";
101 /* Fill in everything we can, which is everything other than the name
103 archdr
->ctfa_magic
= htole64 (CTFA_MAGIC
);
104 archdr
->ctfa_nfiles
= htole64 (ctf_file_cnt
);
105 archdr
->ctfa_ctfs
= htole64 (ctf_startoffs
);
107 /* We could validate that all CTF files have the same data model, but
108 since any reasonable construction process will be building things of
109 only one bitness anyway, this is pretty pointless, so just use the
110 model of the first CTF file for all of them. (It *is* valid to
111 create an empty archive: the value of ctfa_model is irrelevant in
112 this case, but we must be sure not to dereference uninitialized
115 if (ctf_file_cnt
> 0)
116 archdr
->ctfa_model
= htole64 (ctf_getmodel (ctf_files
[0]));
118 /* Now write out the CTFs: ctf_archive_modent array via the mapping,
119 ctfs via write(). The names themselves have not been written yet: we
120 track them in a local strtab until the time is right, and sort the
121 modents array after construction.
123 The name table is not sorted. */
125 for (i
= 0, namesz
= 0; i
< le64toh (archdr
->ctfa_nfiles
); i
++)
126 namesz
+= strlen (names
[i
]) + 1;
128 nametbl
= malloc (namesz
);
131 errmsg
= "Error writing named CTF to archive: %s\n";
135 for (i
= 0, namesz
= 0,
136 modent
= (ctf_archive_modent_t
*) ((char *) archdr
137 + sizeof (struct ctf_archive
));
138 i
< le64toh (archdr
->ctfa_nfiles
); i
++)
142 strcpy (&nametbl
[namesz
], names
[i
]);
144 off
= arc_write_one_ctf (ctf_files
[i
], fd
, threshold
);
145 if ((off
< 0) && (off
> -ECTF_BASE
))
147 errmsg
= "ctf_arc_write(): Cannot determine file "
148 "position while writing to archive: %s";
153 errmsg
= "ctf_arc_write(): Cannot write CTF file to archive: %s\n";
158 modent
->name_offset
= htole64 (namesz
);
159 modent
->ctf_offset
= htole64 (off
- ctf_startoffs
);
160 namesz
+= strlen (names
[i
]) + 1;
164 ctf_qsort_r ((ctf_archive_modent_t
*) ((char *) archdr
165 + sizeof (struct ctf_archive
)),
166 le64toh (archdr
->ctfa_nfiles
),
167 sizeof (struct ctf_archive_modent
), sort_modent_by_name
,
170 /* Now the name table. */
172 if ((nameoffs
= lseek (fd
, 0, SEEK_CUR
)) < 0)
174 errmsg
= "ctf_arc_write(): Cannot get current file position "
178 archdr
->ctfa_names
= htole64 (nameoffs
);
183 if ((len
= write (fd
, np
, namesz
)) < 0)
185 errmsg
= "ctf_arc_write(): Cannot write name table to archive: %s\n";
193 if (arc_mmap_writeout (fd
, archdr
, headersz
, &errmsg
) < 0)
195 if (arc_mmap_unmap (archdr
, headersz
, &errmsg
) < 0)
202 arc_mmap_unmap (archdr
, headersz
, NULL
);
204 ctf_dprintf (errmsg
, errno
< ECTF_BASE
? strerror (errno
) :
209 /* Write out a CTF archive. The entries in CTF_FILES are referenced by name:
210 the names are passed in the names array, which must have CTF_FILES entries.
212 If the filename is NULL, create a temporary file and return a pointer to it.
214 Returns 0 on success, or an errno, or an ECTF_* value. */
216 ctf_arc_write (const char *file
, ctf_file_t
** ctf_files
, size_t ctf_file_cnt
,
217 const char **names
, size_t threshold
)
222 if ((fd
= open (file
, O_RDWR
| O_CREAT
| O_TRUNC
| O_CLOEXEC
, 0666)) < 0)
224 ctf_dprintf ("ctf_arc_write(): cannot create %s: %s\n", file
,
229 err
= ctf_arc_write_fd (fd
, ctf_files
, ctf_file_cnt
, names
, threshold
);
233 if ((err
= close (fd
)) < 0)
234 ctf_dprintf ("ctf_arc_write(): Cannot close after writing to archive: "
235 "%s\n", strerror (errno
));
247 /* Write one CTF file out. Return the file position of the written file (or
248 rather, of the file-size uint64_t that precedes it): negative return is a
249 negative errno or ctf_errno value. On error, the file position may no longer
250 be at the end of the file. */
252 arc_write_one_ctf (ctf_file_t
* f
, int fd
, size_t threshold
)
258 int (*writefn
) (ctf_file_t
* fp
, int fd
);
260 if (ctf_serialize (f
) < 0)
261 return f
->ctf_errno
* -1;
263 if ((off
= lseek (fd
, 0, SEEK_CUR
)) < 0)
266 if (f
->ctf_size
> threshold
)
267 writefn
= ctf_compress_write
;
271 /* This zero-write turns into the size in a moment. */
272 ctfsz_len
= sizeof (ctfsz
);
273 ctfszp
= (char *) &ctfsz
;
274 while (ctfsz_len
> 0)
276 ssize_t writelen
= write (fd
, ctfszp
, ctfsz_len
);
279 ctfsz_len
-= writelen
;
283 if (writefn (f
, fd
) != 0)
284 return f
->ctf_errno
* -1;
286 if ((end_off
= lseek (fd
, 0, SEEK_CUR
)) < 0)
288 ctfsz
= htole64 (end_off
- off
);
290 if ((lseek (fd
, off
, SEEK_SET
)) < 0)
294 ctfsz_len
= sizeof (ctfsz
);
295 ctfszp
= (char *) &ctfsz
;
296 while (ctfsz_len
> 0)
298 ssize_t writelen
= write (fd
, ctfszp
, ctfsz_len
);
301 ctfsz_len
-= writelen
;
305 end_off
= LCTF_ALIGN_OFFS (end_off
, 8);
306 if ((lseek (fd
, end_off
, SEEK_SET
)) < 0)
312 /* qsort() function to sort the array of struct ctf_archive_modents into
313 ascending name order. */
315 sort_modent_by_name (const void *one
, const void *two
, void *n
)
317 const struct ctf_archive_modent
*a
= one
;
318 const struct ctf_archive_modent
*b
= two
;
321 return strcmp (&nametbl
[le64toh (a
->name_offset
)],
322 &nametbl
[le64toh (b
->name_offset
)]);
325 /* bsearch_r() function to search for a given name in the sorted array of struct
326 ctf_archive_modents. */
328 search_modent_by_name (const void *key
, const void *ent
, void *arg
)
331 const struct ctf_archive_modent
*v
= ent
;
332 const char *search_nametbl
= arg
;
334 return strcmp (k
, &search_nametbl
[le64toh (v
->name_offset
)]);
337 /* Make a new struct ctf_archive_internal wrapper for a ctf_archive or a
338 ctf_file. Closes ARC and/or FP on error. Arrange to free the SYMSECT or
339 STRSECT, as needed, on close. Possibly do not unmap on close. */
341 struct ctf_archive_internal
*
342 ctf_new_archive_internal (int is_archive
, int unmap_on_close
,
343 struct ctf_archive
*arc
,
344 ctf_file_t
*fp
, const ctf_sect_t
*symsect
,
345 const ctf_sect_t
*strsect
,
348 struct ctf_archive_internal
*arci
;
350 if ((arci
= calloc (1, sizeof (struct ctf_archive_internal
))) == NULL
)
355 ctf_arc_close_internal (arc
);
359 return (ctf_set_open_errno (errp
, errno
));
361 arci
->ctfi_is_archive
= is_archive
;
363 arci
->ctfi_archive
= arc
;
365 arci
->ctfi_file
= fp
;
367 memcpy (&arci
->ctfi_symsect
, symsect
, sizeof (struct ctf_sect
));
369 memcpy (&arci
->ctfi_strsect
, strsect
, sizeof (struct ctf_sect
));
370 arci
->ctfi_free_symsect
= 0;
371 arci
->ctfi_free_strsect
= 0;
372 arci
->ctfi_unmap_on_close
= unmap_on_close
;
377 /* Open a CTF archive or dictionary from data in a buffer (which the caller must
378 preserve until ctf_arc_close() time). Returns the archive, or NULL and an
379 error in *err (if not NULL). */
381 ctf_arc_bufopen (const ctf_sect_t
*ctfsect
, const ctf_sect_t
*symsect
,
382 const ctf_sect_t
*strsect
, int *errp
)
384 struct ctf_archive
*arc
= NULL
;
386 ctf_file_t
*fp
= NULL
;
388 if (ctfsect
->cts_size
> sizeof (uint64_t) &&
389 ((*(uint64_t *) ctfsect
->cts_data
) == CTFA_MAGIC
))
391 /* The archive is mmappable, so this operation is trivial.
393 This buffer is nonmodifiable, so the trick involving mmapping only part
394 of it and storing the length in the magic number is not applicable: so
395 record this fact in the archive-wrapper header. (We cannot record it
396 in the archive, because the archive may very well be a read-only
400 arc
= (struct ctf_archive
*) ctfsect
->cts_data
;
405 if ((fp
= ctf_bufopen (ctfsect
, symsect
, strsect
, errp
)) == NULL
)
407 ctf_dprintf ("ctf_arc_bufopen(): cannot open CTF: %s\n",
412 return ctf_new_archive_internal (is_archive
, 0, arc
, fp
, symsect
, strsect
,
416 /* Open a CTF archive. Returns the archive, or NULL and an error in *err (if
419 ctf_arc_open_internal (const char *filename
, int *errp
)
424 struct ctf_archive
*arc
; /* (Actually the whole file.) */
427 if ((fd
= open (filename
, O_RDONLY
)) < 0)
429 errmsg
= "ctf_arc_open(): cannot open %s: %s\n";
432 if (fstat (fd
, &s
) < 0)
434 errmsg
= "ctf_arc_open(): cannot stat %s: %s\n";
438 if ((arc
= arc_mmap_file (fd
, s
.st_size
)) == NULL
)
440 errmsg
= "ctf_arc_open(): Cannot read in %s: %s\n";
444 if (le64toh (arc
->ctfa_magic
) != CTFA_MAGIC
)
446 errmsg
= "ctf_arc_open(): Invalid magic number";
451 /* This horrible hack lets us know how much to unmap when the file is
452 closed. (We no longer need the magic number, and the mapping
454 arc
->ctfa_magic
= s
.st_size
;
459 arc_mmap_unmap (arc
, s
.st_size
, NULL
);
465 ctf_dprintf (errmsg
, filename
, errno
< ECTF_BASE
? strerror (errno
) :
470 /* Close an archive. */
472 ctf_arc_close_internal (struct ctf_archive
*arc
)
477 /* See the comment in ctf_arc_open(). */
478 arc_mmap_unmap (arc
, arc
->ctfa_magic
, NULL
);
481 /* Public entry point: close an archive, or CTF file. */
483 ctf_arc_close (ctf_archive_t
*arc
)
488 if (arc
->ctfi_is_archive
)
490 if (arc
->ctfi_unmap_on_close
)
491 ctf_arc_close_internal (arc
->ctfi_archive
);
494 ctf_file_close (arc
->ctfi_file
);
495 if (arc
->ctfi_free_symsect
)
496 free ((void *) arc
->ctfi_symsect
.cts_data
);
497 if (arc
->ctfi_free_strsect
)
498 free ((void *) arc
->ctfi_strsect
.cts_data
);
499 free (arc
->ctfi_data
);
500 if (arc
->ctfi_bfd_close
)
501 arc
->ctfi_bfd_close (arc
);
505 /* Return the ctf_file_t with the given name, or NULL if none, setting 'err' if
506 non-NULL. A name of NULL means to open the default file. */
508 ctf_arc_open_by_name_internal (const struct ctf_archive
*arc
,
509 const ctf_sect_t
*symsect
,
510 const ctf_sect_t
*strsect
,
511 const char *name
, int *errp
)
513 struct ctf_archive_modent
*modent
;
514 const char *search_nametbl
;
517 name
= _CTF_SECTION
; /* The default name. */
519 ctf_dprintf ("ctf_arc_open_by_name(%s): opening\n", name
);
521 modent
= (ctf_archive_modent_t
*) ((char *) arc
522 + sizeof (struct ctf_archive
));
524 search_nametbl
= (const char *) arc
+ le64toh (arc
->ctfa_names
);
525 modent
= bsearch_r (name
, modent
, le64toh (arc
->ctfa_nfiles
),
526 sizeof (struct ctf_archive_modent
),
527 search_modent_by_name
, (void *) search_nametbl
);
529 /* This is actually a common case and normal operation: no error
534 *errp
= ECTF_ARNNAME
;
538 return ctf_arc_open_by_offset (arc
, symsect
, strsect
,
539 le64toh (modent
->ctf_offset
), errp
);
542 /* Return the ctf_file_t with the given name, or NULL if none, setting 'err' if
543 non-NULL. A name of NULL means to open the default file.
545 Use the specified string and symbol table sections.
547 Public entry point. */
549 ctf_arc_open_by_name_sections (const ctf_archive_t
*arc
,
550 const ctf_sect_t
*symsect
,
551 const ctf_sect_t
*strsect
,
555 if (arc
->ctfi_is_archive
)
558 ret
= ctf_arc_open_by_name_internal (arc
->ctfi_archive
, symsect
, strsect
,
561 ret
->ctf_archive
= (ctf_archive_t
*) arc
;
565 if ((name
!= NULL
) && (strcmp (name
, _CTF_SECTION
) != 0))
568 *errp
= ECTF_ARNNAME
;
571 arc
->ctfi_file
->ctf_archive
= (ctf_archive_t
*) arc
;
573 /* Bump the refcount so that the user can ctf_file_close() it. */
574 arc
->ctfi_file
->ctf_refcnt
++;
575 return arc
->ctfi_file
;
578 /* Return the ctf_file_t with the given name, or NULL if none, setting 'err' if
579 non-NULL. A name of NULL means to open the default file.
581 Public entry point. */
583 ctf_arc_open_by_name (const ctf_archive_t
*arc
, const char *name
, int *errp
)
585 const ctf_sect_t
*symsect
= &arc
->ctfi_symsect
;
586 const ctf_sect_t
*strsect
= &arc
->ctfi_strsect
;
588 if (symsect
->cts_name
== NULL
)
590 if (strsect
->cts_name
== NULL
)
593 return ctf_arc_open_by_name_sections (arc
, symsect
, strsect
, name
, errp
);
596 /* Return the ctf_file_t at the given ctfa_ctfs-relative offset, or NULL if
597 none, setting 'err' if non-NULL. */
599 ctf_arc_open_by_offset (const struct ctf_archive
*arc
,
600 const ctf_sect_t
*symsect
,
601 const ctf_sect_t
*strsect
, size_t offset
,
607 ctf_dprintf ("ctf_arc_open_by_offset(%lu): opening\n", (unsigned long) offset
);
609 memset (&ctfsect
, 0, sizeof (ctf_sect_t
));
611 offset
+= le64toh (arc
->ctfa_ctfs
);
613 ctfsect
.cts_name
= _CTF_SECTION
;
614 ctfsect
.cts_size
= le64toh (*((uint64_t *) ((char *) arc
+ offset
)));
615 ctfsect
.cts_entsize
= 1;
616 ctfsect
.cts_data
= (void *) ((char *) arc
+ offset
+ sizeof (uint64_t));
617 fp
= ctf_bufopen (&ctfsect
, symsect
, strsect
, errp
);
619 ctf_setmodel (fp
, le64toh (arc
->ctfa_model
));
623 /* Return the number of members in an archive. */
625 ctf_archive_count (const ctf_archive_t
*wrapper
)
627 if (!wrapper
->ctfi_is_archive
)
630 return wrapper
->ctfi_archive
->ctfa_nfiles
;
633 /* Raw iteration over all CTF files in an archive. We pass the raw data for all
634 CTF files in turn to the specified callback function. */
636 ctf_archive_raw_iter_internal (const struct ctf_archive
*arc
,
637 ctf_archive_raw_member_f
*func
, void *data
)
641 struct ctf_archive_modent
*modent
;
644 modent
= (ctf_archive_modent_t
*) ((char *) arc
645 + sizeof (struct ctf_archive
));
646 nametbl
= (((const char *) arc
) + le64toh (arc
->ctfa_names
));
648 for (i
= 0; i
< le64toh (arc
->ctfa_nfiles
); i
++)
653 name
= &nametbl
[le64toh (modent
[i
].name_offset
)];
654 fp
= ((char *) arc
+ le64toh (arc
->ctfa_ctfs
)
655 + le64toh (modent
[i
].ctf_offset
));
657 if ((rc
= func (name
, (void *) (fp
+ sizeof (uint64_t)),
658 le64toh (*((uint64_t *) fp
)), data
)) != 0)
664 /* Raw iteration over all CTF files in an archive: public entry point.
666 Returns -EINVAL if not supported for this sort of archive. */
668 ctf_archive_raw_iter (const ctf_archive_t
*arc
,
669 ctf_archive_raw_member_f
* func
, void *data
)
671 if (arc
->ctfi_is_archive
)
672 return ctf_archive_raw_iter_internal (arc
->ctfi_archive
, func
, data
);
674 return -EINVAL
; /* Not supported. */
677 /* Iterate over all CTF files in an archive. We pass all CTF files in turn to
678 the specified callback function. */
680 ctf_archive_iter_internal (const ctf_archive_t
*wrapper
,
681 const struct ctf_archive
*arc
,
682 const ctf_sect_t
*symsect
,
683 const ctf_sect_t
*strsect
,
684 ctf_archive_member_f
*func
, void *data
)
689 struct ctf_archive_modent
*modent
;
692 modent
= (ctf_archive_modent_t
*) ((char *) arc
693 + sizeof (struct ctf_archive
));
694 nametbl
= (((const char *) arc
) + le64toh (arc
->ctfa_names
));
696 for (i
= 0; i
< le64toh (arc
->ctfa_nfiles
); i
++)
700 name
= &nametbl
[le64toh (modent
[i
].name_offset
)];
701 if ((f
= ctf_arc_open_by_name_internal (arc
, symsect
, strsect
,
705 f
->ctf_archive
= (ctf_archive_t
*) wrapper
;
706 if ((rc
= func (f
, name
, data
)) != 0)
717 /* Iterate over all CTF files in an archive: public entry point. We pass all
718 CTF files in turn to the specified callback function. */
720 ctf_archive_iter (const ctf_archive_t
*arc
, ctf_archive_member_f
*func
,
723 const ctf_sect_t
*symsect
= &arc
->ctfi_symsect
;
724 const ctf_sect_t
*strsect
= &arc
->ctfi_strsect
;
726 if (symsect
->cts_name
== NULL
)
728 if (strsect
->cts_name
== NULL
)
731 if (arc
->ctfi_is_archive
)
732 return ctf_archive_iter_internal (arc
, arc
->ctfi_archive
, symsect
, strsect
,
735 return func (arc
->ctfi_file
, _CTF_SECTION
, data
);
738 /* Iterate over all CTF files in an archive, returning each dict in turn as a
739 ctf_file_t, and NULL on error or end of iteration. It is the caller's
740 responsibility to close it. Parent dicts may be skipped. Regardless of
741 whether they are skipped or not, the caller must ctf_import the parent if
744 We identify parents by name rather than by flag value: for now, with the
745 linker only emitting parents named _CTF_SECTION, this works well enough. */
748 ctf_archive_next (const ctf_archive_t
*wrapper
, ctf_next_t
**it
, const char **name
,
749 int skip_parent
, int *errp
)
753 struct ctf_archive
*arc
;
754 struct ctf_archive_modent
*modent
;
760 if ((i
= ctf_next_create()) == NULL
)
766 i
->cu
.ctn_arc
= wrapper
;
767 i
->ctn_iter_fun
= (void (*) (void)) ctf_archive_next
;
771 if ((void (*) (void)) ctf_archive_next
!= i
->ctn_iter_fun
)
774 *errp
= ECTF_NEXT_WRONGFUN
;
778 if (wrapper
!= i
->cu
.ctn_arc
)
781 *errp
= ECTF_NEXT_WRONGFP
;
785 /* Iteration is made a bit more complex by the need to handle ctf_file_t's
786 transparently wrapped in a single-member archive. These are parents: if
787 skip_parent is on, they are skipped and the iterator terminates
790 if (!wrapper
->ctfi_is_archive
&& i
->ctn_n
== 0)
795 wrapper
->ctfi_file
->ctf_refcnt
++;
796 return wrapper
->ctfi_file
;
800 arc
= wrapper
->ctfi_archive
;
802 /* The loop keeps going when skip_parent is on as long as the member we find
803 is the parent (i.e. at most two iterations, but possibly an early return if
804 *all* we have is a parent). */
806 const ctf_sect_t
*symsect
;
807 const ctf_sect_t
*strsect
;
811 if ((!wrapper
->ctfi_is_archive
) || (i
->ctn_n
>= le64toh (arc
->ctfa_nfiles
)))
813 ctf_next_destroy (i
);
816 *errp
= ECTF_NEXT_END
;
820 symsect
= &wrapper
->ctfi_symsect
;
821 strsect
= &wrapper
->ctfi_strsect
;
823 if (symsect
->cts_name
== NULL
)
825 if (strsect
->cts_name
== NULL
)
828 modent
= (ctf_archive_modent_t
*) ((char *) arc
829 + sizeof (struct ctf_archive
));
830 nametbl
= (((const char *) arc
) + le64toh (arc
->ctfa_names
));
832 name_
= &nametbl
[le64toh (modent
[i
->ctn_n
].name_offset
)];
834 } while (skip_parent
&& strcmp (name_
, _CTF_SECTION
) == 0);
839 f
= ctf_arc_open_by_name_internal (arc
, symsect
, strsect
,
841 f
->ctf_archive
= (ctf_archive_t
*) wrapper
;
846 /* Map the header in. Only used on new, empty files. */
847 static void *arc_mmap_header (int fd
, size_t headersz
)
850 if ((hdr
= mmap (NULL
, headersz
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
,
856 /* mmap() the whole file, for reading only. (Map it writably, but privately: we
857 need to modify the region, but don't need anyone else to see the
859 static void *arc_mmap_file (int fd
, size_t size
)
862 if ((arc
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
863 fd
, 0)) == MAP_FAILED
)
868 /* Persist the header to disk. */
869 static int arc_mmap_writeout (int fd _libctf_unused_
, void *header
,
870 size_t headersz
, const char **errmsg
)
872 if (msync (header
, headersz
, MS_ASYNC
) < 0)
875 *errmsg
= "arc_mmap_writeout(): Cannot sync after writing to %s: %s\n";
881 /* Unmap the region. */
882 static int arc_mmap_unmap (void *header
, size_t headersz
, const char **errmsg
)
884 if (munmap (header
, headersz
) < 0)
887 *errmsg
= "arc_mmap_munmap(): Cannot unmap after writing to %s: %s\n";
893 /* Map the header in. Only used on new, empty files. */
894 static void *arc_mmap_header (int fd _libctf_unused_
, size_t headersz
)
897 if ((hdr
= malloc (headersz
)) == NULL
)
902 /* Pull in the whole file, for reading only. We assume the current file
903 position is at the start of the file. */
904 static void *arc_mmap_file (int fd
, size_t size
)
908 if ((data
= malloc (size
)) == NULL
)
911 if (ctf_pread (fd
, data
, size
, 0) < 0)
919 /* Persist the header to disk. */
920 static int arc_mmap_writeout (int fd
, void *header
, size_t headersz
,
925 char *data
= (char *) header
;
926 ssize_t count
= headersz
;
928 if ((lseek (fd
, 0, SEEK_SET
)) < 0)
931 *errmsg
= "arc_mmap_writeout(): Cannot seek while writing header to "
938 if ((len
= write (fd
, data
, count
)) < 0)
941 *errmsg
= "arc_mmap_writeout(): Cannot write header to %s: %s\n";
948 if (len
== 0) /* EOF. */
957 /* Unmap the region. */
958 static int arc_mmap_unmap (void *header
, size_t headersz _libctf_unused_
,
959 const char **errmsg _libctf_unused_
)
This page took 0.049759 seconds and 4 git commands to generate.