ctf-archive.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /* CTF archive files.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of libctf.
  4. libctf is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ctf-impl.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <elf.h>
  19. #include "ctf-endian.h"
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #ifdef HAVE_MMAP
  26. #include <sys/mman.h>
  27. #endif
  28. static off_t arc_write_one_ctf (ctf_dict_t * f, int fd, size_t threshold);
  29. static ctf_dict_t *ctf_dict_open_by_offset (const struct ctf_archive *arc,
  30. const ctf_sect_t *symsect,
  31. const ctf_sect_t *strsect,
  32. size_t offset, int little_endian,
  33. int *errp);
  34. static int sort_modent_by_name (const void *one, const void *two, void *n);
  35. static void *arc_mmap_header (int fd, size_t headersz);
  36. static void *arc_mmap_file (int fd, size_t size);
  37. static int arc_mmap_writeout (int fd, void *header, size_t headersz,
  38. const char **errmsg);
  39. static int arc_mmap_unmap (void *header, size_t headersz, const char **errmsg);
  40. static void ctf_arc_import_parent (const ctf_archive_t *arc, ctf_dict_t *fp);
  41. /* Flag to indicate "symbol not present" in ctf_archive_internal.ctfi_symdicts
  42. and ctfi_symnamedicts. Never initialized. */
  43. static ctf_dict_t enosym;
  44. /* Write out a CTF archive to the start of the file referenced by the passed-in
  45. fd. The entries in CTF_DICTS are referenced by name: the names are passed in
  46. the names array, which must have CTF_DICTS entries.
  47. Returns 0 on success, or an errno, or an ECTF_* value. */
  48. int
  49. ctf_arc_write_fd (int fd, ctf_dict_t **ctf_dicts, size_t ctf_dict_cnt,
  50. const char **names, size_t threshold)
  51. {
  52. const char *errmsg;
  53. struct ctf_archive *archdr;
  54. size_t i;
  55. char dummy = 0;
  56. size_t headersz;
  57. ssize_t namesz;
  58. size_t ctf_startoffs; /* Start of the section we are working over. */
  59. char *nametbl = NULL; /* The name table. */
  60. char *np;
  61. off_t nameoffs;
  62. struct ctf_archive_modent *modent;
  63. ctf_dprintf ("Writing CTF archive with %lu files\n",
  64. (unsigned long) ctf_dict_cnt);
  65. /* Figure out the size of the mmap()ed header, including the
  66. ctf_archive_modent array. We assume that all of this needs no
  67. padding: a likely assumption, given that it's all made up of
  68. uint64_t's. */
  69. headersz = sizeof (struct ctf_archive)
  70. + (ctf_dict_cnt * sizeof (uint64_t) * 2);
  71. ctf_dprintf ("headersz is %lu\n", (unsigned long) headersz);
  72. /* From now on we work in two pieces: an mmap()ed region from zero up to the
  73. headersz, and a region updated via write() starting after that, containing
  74. all the tables. Platforms that do not support mmap() just use write(). */
  75. ctf_startoffs = headersz;
  76. if (lseek (fd, ctf_startoffs - 1, SEEK_SET) < 0)
  77. {
  78. errmsg = N_("ctf_arc_write(): cannot extend file while writing");
  79. goto err;
  80. }
  81. if (write (fd, &dummy, 1) < 0)
  82. {
  83. errmsg = N_("ctf_arc_write(): cannot extend file while writing");
  84. goto err;
  85. }
  86. if ((archdr = arc_mmap_header (fd, headersz)) == NULL)
  87. {
  88. errmsg = N_("ctf_arc_write(): cannot mmap");
  89. goto err;
  90. }
  91. /* Fill in everything we can, which is everything other than the name
  92. table offset. */
  93. archdr->ctfa_magic = htole64 (CTFA_MAGIC);
  94. archdr->ctfa_ndicts = htole64 (ctf_dict_cnt);
  95. archdr->ctfa_ctfs = htole64 (ctf_startoffs);
  96. /* We could validate that all CTF files have the same data model, but
  97. since any reasonable construction process will be building things of
  98. only one bitness anyway, this is pretty pointless, so just use the
  99. model of the first CTF file for all of them. (It *is* valid to
  100. create an empty archive: the value of ctfa_model is irrelevant in
  101. this case, but we must be sure not to dereference uninitialized
  102. memory.) */
  103. if (ctf_dict_cnt > 0)
  104. archdr->ctfa_model = htole64 (ctf_getmodel (ctf_dicts[0]));
  105. /* Now write out the CTFs: ctf_archive_modent array via the mapping,
  106. ctfs via write(). The names themselves have not been written yet: we
  107. track them in a local strtab until the time is right, and sort the
  108. modents array after construction.
  109. The name table is not sorted. */
  110. for (i = 0, namesz = 0; i < le64toh (archdr->ctfa_ndicts); i++)
  111. namesz += strlen (names[i]) + 1;
  112. nametbl = malloc (namesz);
  113. if (nametbl == NULL)
  114. {
  115. errmsg = N_("ctf_arc_write(): error writing named CTF to archive");
  116. goto err_unmap;
  117. }
  118. for (i = 0, namesz = 0,
  119. modent = (ctf_archive_modent_t *) ((char *) archdr
  120. + sizeof (struct ctf_archive));
  121. i < le64toh (archdr->ctfa_ndicts); i++)
  122. {
  123. off_t off;
  124. strcpy (&nametbl[namesz], names[i]);
  125. off = arc_write_one_ctf (ctf_dicts[i], fd, threshold);
  126. if ((off < 0) && (off > -ECTF_BASE))
  127. {
  128. errmsg = N_("ctf_arc_write(): cannot determine file "
  129. "position while writing to archive");
  130. goto err_free;
  131. }
  132. if (off < 0)
  133. {
  134. errmsg = N_("ctf_arc_write(): cannot write CTF file to archive");
  135. errno = off * -1;
  136. goto err_free;
  137. }
  138. modent->name_offset = htole64 (namesz);
  139. modent->ctf_offset = htole64 (off - ctf_startoffs);
  140. namesz += strlen (names[i]) + 1;
  141. modent++;
  142. }
  143. ctf_qsort_r ((ctf_archive_modent_t *) ((char *) archdr
  144. + sizeof (struct ctf_archive)),
  145. le64toh (archdr->ctfa_ndicts),
  146. sizeof (struct ctf_archive_modent), sort_modent_by_name,
  147. nametbl);
  148. /* Now the name table. */
  149. if ((nameoffs = lseek (fd, 0, SEEK_CUR)) < 0)
  150. {
  151. errmsg = N_("ctf_arc_write(): cannot get current file position "
  152. "in archive");
  153. goto err_free;
  154. }
  155. archdr->ctfa_names = htole64 (nameoffs);
  156. np = nametbl;
  157. while (namesz > 0)
  158. {
  159. ssize_t len;
  160. if ((len = write (fd, np, namesz)) < 0)
  161. {
  162. errmsg = N_("ctf_arc_write(): cannot write name table to archive");
  163. goto err_free;
  164. }
  165. namesz -= len;
  166. np += len;
  167. }
  168. free (nametbl);
  169. if (arc_mmap_writeout (fd, archdr, headersz, &errmsg) < 0)
  170. goto err_unmap;
  171. if (arc_mmap_unmap (archdr, headersz, &errmsg) < 0)
  172. goto err;
  173. return 0;
  174. err_free:
  175. free (nametbl);
  176. err_unmap:
  177. arc_mmap_unmap (archdr, headersz, NULL);
  178. err:
  179. /* We report errors into the first file in the archive, if any: if this is a
  180. zero-file archive, put it in the open-errors stream for lack of anywhere
  181. else for it to go. */
  182. ctf_err_warn (ctf_dict_cnt > 0 ? ctf_dicts[0] : NULL, 0, errno, "%s",
  183. gettext (errmsg));
  184. return errno;
  185. }
  186. /* Write out a CTF archive. The entries in CTF_DICTS are referenced by name:
  187. the names are passed in the names array, which must have CTF_DICTS entries.
  188. If the filename is NULL, create a temporary file and return a pointer to it.
  189. Returns 0 on success, or an errno, or an ECTF_* value. */
  190. int
  191. ctf_arc_write (const char *file, ctf_dict_t **ctf_dicts, size_t ctf_dict_cnt,
  192. const char **names, size_t threshold)
  193. {
  194. int err;
  195. int fd;
  196. if ((fd = open (file, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0666)) < 0)
  197. {
  198. ctf_err_warn (ctf_dict_cnt > 0 ? ctf_dicts[0] : NULL, 0, errno,
  199. _("ctf_arc_write(): cannot create %s"), file);
  200. return errno;
  201. }
  202. err = ctf_arc_write_fd (fd, ctf_dicts, ctf_dict_cnt, names, threshold);
  203. if (err)
  204. goto err_close;
  205. if ((err = close (fd)) < 0)
  206. ctf_err_warn (ctf_dict_cnt > 0 ? ctf_dicts[0] : NULL, 0, errno,
  207. _("ctf_arc_write(): cannot close after writing to archive"));
  208. goto err;
  209. err_close:
  210. (void) close (fd);
  211. err:
  212. if (err < 0)
  213. unlink (file);
  214. return err;
  215. }
  216. /* Write one CTF file out. Return the file position of the written file (or
  217. rather, of the file-size uint64_t that precedes it): negative return is a
  218. negative errno or ctf_errno value. On error, the file position may no longer
  219. be at the end of the file. */
  220. static off_t
  221. arc_write_one_ctf (ctf_dict_t * f, int fd, size_t threshold)
  222. {
  223. off_t off, end_off;
  224. uint64_t ctfsz = 0;
  225. char *ctfszp;
  226. size_t ctfsz_len;
  227. int (*writefn) (ctf_dict_t * fp, int fd);
  228. if (ctf_serialize (f) < 0)
  229. return f->ctf_errno * -1;
  230. if ((off = lseek (fd, 0, SEEK_CUR)) < 0)
  231. return errno * -1;
  232. if (f->ctf_size > threshold)
  233. writefn = ctf_compress_write;
  234. else
  235. writefn = ctf_write;
  236. /* This zero-write turns into the size in a moment. */
  237. ctfsz_len = sizeof (ctfsz);
  238. ctfszp = (char *) &ctfsz;
  239. while (ctfsz_len > 0)
  240. {
  241. ssize_t writelen = write (fd, ctfszp, ctfsz_len);
  242. if (writelen < 0)
  243. return errno * -1;
  244. ctfsz_len -= writelen;
  245. ctfszp += writelen;
  246. }
  247. if (writefn (f, fd) != 0)
  248. return f->ctf_errno * -1;
  249. if ((end_off = lseek (fd, 0, SEEK_CUR)) < 0)
  250. return errno * -1;
  251. ctfsz = htole64 (end_off - off);
  252. if ((lseek (fd, off, SEEK_SET)) < 0)
  253. return errno * -1;
  254. /* ... here. */
  255. ctfsz_len = sizeof (ctfsz);
  256. ctfszp = (char *) &ctfsz;
  257. while (ctfsz_len > 0)
  258. {
  259. ssize_t writelen = write (fd, ctfszp, ctfsz_len);
  260. if (writelen < 0)
  261. return errno * -1;
  262. ctfsz_len -= writelen;
  263. ctfszp += writelen;
  264. }
  265. end_off = LCTF_ALIGN_OFFS (end_off, 8);
  266. if ((lseek (fd, end_off, SEEK_SET)) < 0)
  267. return errno * -1;
  268. return off;
  269. }
  270. /* qsort() function to sort the array of struct ctf_archive_modents into
  271. ascending name order. */
  272. static int
  273. sort_modent_by_name (const void *one, const void *two, void *n)
  274. {
  275. const struct ctf_archive_modent *a = one;
  276. const struct ctf_archive_modent *b = two;
  277. char *nametbl = n;
  278. return strcmp (&nametbl[le64toh (a->name_offset)],
  279. &nametbl[le64toh (b->name_offset)]);
  280. }
  281. /* bsearch_r() function to search for a given name in the sorted array of struct
  282. ctf_archive_modents. */
  283. static int
  284. search_modent_by_name (const void *key, const void *ent, void *arg)
  285. {
  286. const char *k = key;
  287. const struct ctf_archive_modent *v = ent;
  288. const char *search_nametbl = arg;
  289. return strcmp (k, &search_nametbl[le64toh (v->name_offset)]);
  290. }
  291. /* Make a new struct ctf_archive_internal wrapper for a ctf_archive or a
  292. ctf_dict. Closes ARC and/or FP on error. Arrange to free the SYMSECT or
  293. STRSECT, as needed, on close. Possibly do not unmap on close. */
  294. struct ctf_archive_internal *
  295. ctf_new_archive_internal (int is_archive, int unmap_on_close,
  296. struct ctf_archive *arc,
  297. ctf_dict_t *fp, const ctf_sect_t *symsect,
  298. const ctf_sect_t *strsect,
  299. int *errp)
  300. {
  301. struct ctf_archive_internal *arci;
  302. if ((arci = calloc (1, sizeof (struct ctf_archive_internal))) == NULL)
  303. {
  304. if (is_archive)
  305. {
  306. if (unmap_on_close)
  307. ctf_arc_close_internal (arc);
  308. }
  309. else
  310. ctf_dict_close (fp);
  311. return (ctf_set_open_errno (errp, errno));
  312. }
  313. arci->ctfi_is_archive = is_archive;
  314. if (is_archive)
  315. arci->ctfi_archive = arc;
  316. else
  317. arci->ctfi_dict = fp;
  318. if (symsect)
  319. memcpy (&arci->ctfi_symsect, symsect, sizeof (struct ctf_sect));
  320. if (strsect)
  321. memcpy (&arci->ctfi_strsect, strsect, sizeof (struct ctf_sect));
  322. arci->ctfi_free_symsect = 0;
  323. arci->ctfi_free_strsect = 0;
  324. arci->ctfi_unmap_on_close = unmap_on_close;
  325. arci->ctfi_symsect_little_endian = -1;
  326. return arci;
  327. }
  328. /* Set the symbol-table endianness of an archive (defaulting the symtab
  329. endianness of all ctf_file_t's opened from that archive). */
  330. void
  331. ctf_arc_symsect_endianness (ctf_archive_t *arc, int little_endian)
  332. {
  333. arc->ctfi_symsect_little_endian = !!little_endian;
  334. if (!arc->ctfi_is_archive)
  335. ctf_symsect_endianness (arc->ctfi_dict, arc->ctfi_symsect_little_endian);
  336. }
  337. /* Get the CTF preamble from data in a buffer, which may be either an archive or
  338. a CTF dict. If multiple dicts are present in an archive, the preamble comes
  339. from an arbitrary dict. The preamble is a pointer into the ctfsect passed
  340. in. */
  341. const ctf_preamble_t *
  342. ctf_arc_bufpreamble (const ctf_sect_t *ctfsect)
  343. {
  344. if (ctfsect->cts_size > sizeof (uint64_t) &&
  345. (le64toh ((*(uint64_t *) ctfsect->cts_data)) == CTFA_MAGIC))
  346. {
  347. struct ctf_archive *arc = (struct ctf_archive *) ctfsect->cts_data;
  348. return (const ctf_preamble_t *) ((char *) arc + le64toh (arc->ctfa_ctfs)
  349. + sizeof (uint64_t));
  350. }
  351. else
  352. return (const ctf_preamble_t *) ctfsect->cts_data;
  353. }
  354. /* Open a CTF archive or dictionary from data in a buffer (which the caller must
  355. preserve until ctf_arc_close() time). Returns the archive, or NULL and an
  356. error in *err (if not NULL). */
  357. ctf_archive_t *
  358. ctf_arc_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
  359. const ctf_sect_t *strsect, int *errp)
  360. {
  361. struct ctf_archive *arc = NULL;
  362. int is_archive;
  363. ctf_dict_t *fp = NULL;
  364. if (ctfsect->cts_size > sizeof (uint64_t) &&
  365. (le64toh ((*(uint64_t *) ctfsect->cts_data)) == CTFA_MAGIC))
  366. {
  367. /* The archive is mmappable, so this operation is trivial.
  368. This buffer is nonmodifiable, so the trick involving mmapping only part
  369. of it and storing the length in the magic number is not applicable: so
  370. record this fact in the archive-wrapper header. (We cannot record it
  371. in the archive, because the archive may very well be a read-only
  372. mapping.) */
  373. is_archive = 1;
  374. arc = (struct ctf_archive *) ctfsect->cts_data;
  375. }
  376. else
  377. {
  378. is_archive = 0;
  379. if ((fp = ctf_bufopen (ctfsect, symsect, strsect, errp)) == NULL)
  380. {
  381. ctf_err_warn (NULL, 0, *errp, _("ctf_arc_bufopen(): cannot open CTF"));
  382. return NULL;
  383. }
  384. }
  385. return ctf_new_archive_internal (is_archive, 0, arc, fp, symsect, strsect,
  386. errp);
  387. }
  388. /* Open a CTF archive. Returns the archive, or NULL and an error in *err (if
  389. not NULL). */
  390. struct ctf_archive *
  391. ctf_arc_open_internal (const char *filename, int *errp)
  392. {
  393. const char *errmsg;
  394. int fd;
  395. struct stat s;
  396. struct ctf_archive *arc; /* (Actually the whole file.) */
  397. libctf_init_debug();
  398. if ((fd = open (filename, O_RDONLY)) < 0)
  399. {
  400. errmsg = N_("ctf_arc_open(): cannot open %s");
  401. goto err;
  402. }
  403. if (fstat (fd, &s) < 0)
  404. {
  405. errmsg = N_("ctf_arc_open(): cannot stat %s");
  406. goto err_close;
  407. }
  408. if ((arc = arc_mmap_file (fd, s.st_size)) == NULL)
  409. {
  410. errmsg = N_("ctf_arc_open(): cannot read in %s");
  411. goto err_close;
  412. }
  413. if (le64toh (arc->ctfa_magic) != CTFA_MAGIC)
  414. {
  415. errmsg = N_("ctf_arc_open(): %s: invalid magic number");
  416. errno = ECTF_FMT;
  417. goto err_unmap;
  418. }
  419. /* This horrible hack lets us know how much to unmap when the file is
  420. closed. (We no longer need the magic number, and the mapping
  421. is private.) */
  422. arc->ctfa_magic = s.st_size;
  423. close (fd);
  424. return arc;
  425. err_unmap:
  426. arc_mmap_unmap (arc, s.st_size, NULL);
  427. err_close:
  428. close (fd);
  429. err:
  430. if (errp)
  431. *errp = errno;
  432. ctf_err_warn (NULL, 0, errno, gettext (errmsg), filename);
  433. return NULL;
  434. }
  435. /* Close an archive. */
  436. void
  437. ctf_arc_close_internal (struct ctf_archive *arc)
  438. {
  439. if (arc == NULL)
  440. return;
  441. /* See the comment in ctf_arc_open(). */
  442. arc_mmap_unmap (arc, arc->ctfa_magic, NULL);
  443. }
  444. /* Public entry point: close an archive, or CTF file. */
  445. void
  446. ctf_arc_close (ctf_archive_t *arc)
  447. {
  448. if (arc == NULL)
  449. return;
  450. if (arc->ctfi_is_archive)
  451. {
  452. if (arc->ctfi_unmap_on_close)
  453. ctf_arc_close_internal (arc->ctfi_archive);
  454. }
  455. else
  456. ctf_dict_close (arc->ctfi_dict);
  457. free (arc->ctfi_symdicts);
  458. free (arc->ctfi_symnamedicts);
  459. ctf_dynhash_destroy (arc->ctfi_dicts);
  460. if (arc->ctfi_free_symsect)
  461. free ((void *) arc->ctfi_symsect.cts_data);
  462. if (arc->ctfi_free_strsect)
  463. free ((void *) arc->ctfi_strsect.cts_data);
  464. free (arc->ctfi_data);
  465. if (arc->ctfi_bfd_close)
  466. arc->ctfi_bfd_close (arc);
  467. free (arc);
  468. }
  469. /* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
  470. non-NULL. A name of NULL means to open the default file. */
  471. static ctf_dict_t *
  472. ctf_dict_open_internal (const struct ctf_archive *arc,
  473. const ctf_sect_t *symsect,
  474. const ctf_sect_t *strsect,
  475. const char *name, int little_endian,
  476. int *errp)
  477. {
  478. struct ctf_archive_modent *modent;
  479. const char *search_nametbl;
  480. if (name == NULL)
  481. name = _CTF_SECTION; /* The default name. */
  482. ctf_dprintf ("ctf_dict_open_internal(%s): opening\n", name);
  483. modent = (ctf_archive_modent_t *) ((char *) arc
  484. + sizeof (struct ctf_archive));
  485. search_nametbl = (const char *) arc + le64toh (arc->ctfa_names);
  486. modent = bsearch_r (name, modent, le64toh (arc->ctfa_ndicts),
  487. sizeof (struct ctf_archive_modent),
  488. search_modent_by_name, (void *) search_nametbl);
  489. /* This is actually a common case and normal operation: no error
  490. debug output. */
  491. if (modent == NULL)
  492. {
  493. if (errp)
  494. *errp = ECTF_ARNNAME;
  495. return NULL;
  496. }
  497. return ctf_dict_open_by_offset (arc, symsect, strsect,
  498. le64toh (modent->ctf_offset),
  499. little_endian, errp);
  500. }
  501. /* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
  502. non-NULL. A name of NULL means to open the default file.
  503. Use the specified string and symbol table sections.
  504. Public entry point. */
  505. ctf_dict_t *
  506. ctf_dict_open_sections (const ctf_archive_t *arc,
  507. const ctf_sect_t *symsect,
  508. const ctf_sect_t *strsect,
  509. const char *name,
  510. int *errp)
  511. {
  512. if (arc->ctfi_is_archive)
  513. {
  514. ctf_dict_t *ret;
  515. ret = ctf_dict_open_internal (arc->ctfi_archive, symsect, strsect,
  516. name, arc->ctfi_symsect_little_endian,
  517. errp);
  518. if (ret)
  519. {
  520. ret->ctf_archive = (ctf_archive_t *) arc;
  521. ctf_arc_import_parent (arc, ret);
  522. }
  523. return ret;
  524. }
  525. if ((name != NULL) && (strcmp (name, _CTF_SECTION) != 0))
  526. {
  527. if (errp)
  528. *errp = ECTF_ARNNAME;
  529. return NULL;
  530. }
  531. arc->ctfi_dict->ctf_archive = (ctf_archive_t *) arc;
  532. /* Bump the refcount so that the user can ctf_dict_close() it. */
  533. arc->ctfi_dict->ctf_refcnt++;
  534. return arc->ctfi_dict;
  535. }
  536. /* Return the ctf_dict_t with the given name, or NULL if none, setting 'err' if
  537. non-NULL. A name of NULL means to open the default file.
  538. Public entry point. */
  539. ctf_dict_t *
  540. ctf_dict_open (const ctf_archive_t *arc, const char *name, int *errp)
  541. {
  542. const ctf_sect_t *symsect = &arc->ctfi_symsect;
  543. const ctf_sect_t *strsect = &arc->ctfi_strsect;
  544. if (symsect->cts_name == NULL)
  545. symsect = NULL;
  546. if (strsect->cts_name == NULL)
  547. strsect = NULL;
  548. return ctf_dict_open_sections (arc, symsect, strsect, name, errp);
  549. }
  550. static void
  551. ctf_cached_dict_close (void *fp)
  552. {
  553. ctf_dict_close ((ctf_dict_t *) fp);
  554. }
  555. /* Return the ctf_dict_t with the given name and cache it in the archive's
  556. ctfi_dicts. If this is the first cached dict, designate it the
  557. crossdict_cache. */
  558. static ctf_dict_t *
  559. ctf_dict_open_cached (ctf_archive_t *arc, const char *name, int *errp)
  560. {
  561. ctf_dict_t *fp;
  562. char *dupname;
  563. /* Just return from the cache if possible. */
  564. if (arc->ctfi_dicts
  565. && ((fp = ctf_dynhash_lookup (arc->ctfi_dicts, name)) != NULL))
  566. {
  567. fp->ctf_refcnt++;
  568. return fp;
  569. }
  570. /* Not yet cached: open it. */
  571. fp = ctf_dict_open (arc, name, errp);
  572. dupname = strdup (name);
  573. if (!fp || !dupname)
  574. goto oom;
  575. if (arc->ctfi_dicts == NULL)
  576. if ((arc->ctfi_dicts
  577. = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
  578. free, ctf_cached_dict_close)) == NULL)
  579. goto oom;
  580. if (ctf_dynhash_insert (arc->ctfi_dicts, dupname, fp) < 0)
  581. goto oom;
  582. fp->ctf_refcnt++;
  583. if (arc->ctfi_crossdict_cache == NULL)
  584. arc->ctfi_crossdict_cache = fp;
  585. return fp;
  586. oom:
  587. ctf_dict_close (fp);
  588. free (dupname);
  589. if (errp)
  590. *errp = ENOMEM;
  591. return NULL;
  592. }
  593. /* Flush any caches the CTF archive may have open. */
  594. void
  595. ctf_arc_flush_caches (ctf_archive_t *wrapper)
  596. {
  597. free (wrapper->ctfi_symdicts);
  598. free (wrapper->ctfi_symnamedicts);
  599. ctf_dynhash_destroy (wrapper->ctfi_dicts);
  600. wrapper->ctfi_symdicts = NULL;
  601. wrapper->ctfi_symnamedicts = NULL;
  602. wrapper->ctfi_dicts = NULL;
  603. wrapper->ctfi_crossdict_cache = NULL;
  604. }
  605. /* Return the ctf_dict_t at the given ctfa_ctfs-relative offset, or NULL if
  606. none, setting 'err' if non-NULL. */
  607. static ctf_dict_t *
  608. ctf_dict_open_by_offset (const struct ctf_archive *arc,
  609. const ctf_sect_t *symsect,
  610. const ctf_sect_t *strsect, size_t offset,
  611. int little_endian, int *errp)
  612. {
  613. ctf_sect_t ctfsect;
  614. ctf_dict_t *fp;
  615. ctf_dprintf ("ctf_dict_open_by_offset(%lu): opening\n", (unsigned long) offset);
  616. memset (&ctfsect, 0, sizeof (ctf_sect_t));
  617. offset += le64toh (arc->ctfa_ctfs);
  618. ctfsect.cts_name = _CTF_SECTION;
  619. ctfsect.cts_size = le64toh (*((uint64_t *) ((char *) arc + offset)));
  620. ctfsect.cts_entsize = 1;
  621. ctfsect.cts_data = (void *) ((char *) arc + offset + sizeof (uint64_t));
  622. fp = ctf_bufopen (&ctfsect, symsect, strsect, errp);
  623. if (fp)
  624. {
  625. ctf_setmodel (fp, le64toh (arc->ctfa_model));
  626. if (little_endian >= 0)
  627. ctf_symsect_endianness (fp, little_endian);
  628. }
  629. return fp;
  630. }
  631. /* Backward compatibility. */
  632. ctf_dict_t *
  633. ctf_arc_open_by_name (const ctf_archive_t *arc, const char *name,
  634. int *errp)
  635. {
  636. return ctf_dict_open (arc, name, errp);
  637. }
  638. ctf_dict_t *
  639. ctf_arc_open_by_name_sections (const ctf_archive_t *arc,
  640. const ctf_sect_t *symsect,
  641. const ctf_sect_t *strsect,
  642. const char *name,
  643. int *errp)
  644. {
  645. return ctf_dict_open_sections (arc, symsect, strsect, name, errp);
  646. }
  647. /* Import the parent into a ctf archive, if this is a child, the parent is not
  648. already set, and a suitable archive member exists. No error is raised if
  649. this is not possible: this is just a best-effort helper operation to give
  650. people useful dicts to start with. */
  651. static void
  652. ctf_arc_import_parent (const ctf_archive_t *arc, ctf_dict_t *fp)
  653. {
  654. if ((fp->ctf_flags & LCTF_CHILD) && fp->ctf_parname && !fp->ctf_parent)
  655. {
  656. ctf_dict_t *parent = ctf_dict_open_cached ((ctf_archive_t *) arc,
  657. fp->ctf_parname, NULL);
  658. if (parent)
  659. {
  660. ctf_import (fp, parent);
  661. ctf_dict_close (parent);
  662. }
  663. }
  664. }
  665. /* Return the number of members in an archive. */
  666. size_t
  667. ctf_archive_count (const ctf_archive_t *wrapper)
  668. {
  669. if (!wrapper->ctfi_is_archive)
  670. return 1;
  671. return wrapper->ctfi_archive->ctfa_ndicts;
  672. }
  673. /* Look up a symbol in an archive by name or index (if the name is set, a lookup
  674. by name is done). Return the dict in the archive that the symbol is found
  675. in, and (optionally) the ctf_id_t of the symbol in that dict (so you don't
  676. have to look it up yourself). The dict is cached, so repeated lookups are
  677. nearly free.
  678. As usual, you should ctf_dict_close() the returned dict once you are done
  679. with it.
  680. Returns NULL on error, and an error in errp (if set). */
  681. static ctf_dict_t *
  682. ctf_arc_lookup_sym_or_name (ctf_archive_t *wrapper, unsigned long symidx,
  683. const char *symname, ctf_id_t *typep, int *errp)
  684. {
  685. ctf_dict_t *fp;
  686. void *fpkey;
  687. ctf_id_t type;
  688. /* The usual non-archive-transparent-wrapper special case. */
  689. if (!wrapper->ctfi_is_archive)
  690. {
  691. if (!symname)
  692. {
  693. if ((type = ctf_lookup_by_symbol (wrapper->ctfi_dict, symidx)) == CTF_ERR)
  694. {
  695. if (errp)
  696. *errp = ctf_errno (wrapper->ctfi_dict);
  697. return NULL;
  698. }
  699. }
  700. else
  701. {
  702. if ((type = ctf_lookup_by_symbol_name (wrapper->ctfi_dict,
  703. symname)) == CTF_ERR)
  704. {
  705. if (errp)
  706. *errp = ctf_errno (wrapper->ctfi_dict);
  707. return NULL;
  708. }
  709. }
  710. if (typep)
  711. *typep = type;
  712. wrapper->ctfi_dict->ctf_refcnt++;
  713. return wrapper->ctfi_dict;
  714. }
  715. if (wrapper->ctfi_symsect.cts_name == NULL
  716. || wrapper->ctfi_symsect.cts_data == NULL
  717. || wrapper->ctfi_symsect.cts_size == 0
  718. || wrapper->ctfi_symsect.cts_entsize == 0)
  719. {
  720. if (errp)
  721. *errp = ECTF_NOSYMTAB;
  722. return NULL;
  723. }
  724. /* Make enough space for all possible symbol indexes, if not already done. We
  725. cache the originating dictionary of all symbols. The dict links are weak,
  726. to the dictionaries cached in ctfi_dicts: their refcnts are *not* bumped.
  727. We also cache similar mappings for symbol names: these are ordinary
  728. dynhashes, with weak links to dicts. */
  729. if (!wrapper->ctfi_symdicts)
  730. {
  731. if ((wrapper->ctfi_symdicts = calloc (wrapper->ctfi_symsect.cts_size
  732. / wrapper->ctfi_symsect.cts_entsize,
  733. sizeof (ctf_dict_t *))) == NULL)
  734. {
  735. if (errp)
  736. *errp = ENOMEM;
  737. return NULL;
  738. }
  739. }
  740. if (!wrapper->ctfi_symnamedicts)
  741. {
  742. if ((wrapper->ctfi_symnamedicts = ctf_dynhash_create (ctf_hash_string,
  743. ctf_hash_eq_string,
  744. free, NULL)) == NULL)
  745. {
  746. if (errp)
  747. *errp = ENOMEM;
  748. return NULL;
  749. }
  750. }
  751. /* Perhaps the dict in which we found a previous lookup is cached. If it's
  752. supposed to be cached but we don't find it, pretend it was always not
  753. found: this should never happen, but shouldn't be allowed to cause trouble
  754. if it does. */
  755. if ((symname && ctf_dynhash_lookup_kv (wrapper->ctfi_symnamedicts,
  756. symname, NULL, &fpkey))
  757. || (!symname && wrapper->ctfi_symdicts[symidx] != NULL))
  758. {
  759. if (symname)
  760. fp = (ctf_dict_t *) fpkey;
  761. else
  762. fp = wrapper->ctfi_symdicts[symidx];
  763. if (fp == &enosym)
  764. goto no_sym;
  765. if (symname)
  766. {
  767. if ((type = ctf_lookup_by_symbol_name (fp, symname)) == CTF_ERR)
  768. goto cache_no_sym;
  769. }
  770. else
  771. {
  772. if ((type = ctf_lookup_by_symbol (fp, symidx)) == CTF_ERR)
  773. goto cache_no_sym;
  774. }
  775. if (typep)
  776. *typep = type;
  777. fp->ctf_refcnt++;
  778. return fp;
  779. }
  780. /* Not cached: find it and cache it. We must track open errors ourselves even
  781. if our caller doesn't, to be able to distinguish no-error end-of-iteration
  782. from open errors. */
  783. int local_err;
  784. int *local_errp;
  785. ctf_next_t *i = NULL;
  786. const char *name;
  787. if (errp)
  788. local_errp = errp;
  789. else
  790. local_errp = &local_err;
  791. while ((fp = ctf_archive_next (wrapper, &i, &name, 0, local_errp)) != NULL)
  792. {
  793. if (!symname)
  794. {
  795. if ((type = ctf_lookup_by_symbol (fp, symidx)) != CTF_ERR)
  796. wrapper->ctfi_symdicts[symidx] = fp;
  797. }
  798. else
  799. {
  800. if ((type = ctf_lookup_by_symbol_name (fp, symname)) != CTF_ERR)
  801. {
  802. char *tmp;
  803. /* No error checking, as above. */
  804. if ((tmp = strdup (symname)) != NULL)
  805. ctf_dynhash_insert (wrapper->ctfi_symnamedicts, tmp, fp);
  806. }
  807. }
  808. if (type != CTF_ERR)
  809. {
  810. if (typep)
  811. *typep = type;
  812. ctf_next_destroy (i);
  813. return fp;
  814. }
  815. if (ctf_errno (fp) != ECTF_NOTYPEDAT)
  816. {
  817. if (errp)
  818. *errp = ctf_errno (fp);
  819. ctf_next_destroy (i);
  820. return NULL; /* errno is set for us. */
  821. }
  822. ctf_dict_close (fp);
  823. }
  824. if (*local_errp != ECTF_NEXT_END)
  825. {
  826. ctf_next_destroy (i);
  827. return NULL;
  828. }
  829. /* Don't leak end-of-iteration to the caller. */
  830. *local_errp = 0;
  831. cache_no_sym:
  832. if (!symname)
  833. wrapper->ctfi_symdicts[symidx] = &enosym;
  834. else
  835. {
  836. char *tmp;
  837. /* No error checking: if caching fails, there is only a slight performance
  838. impact. */
  839. if ((tmp = strdup (symname)) != NULL)
  840. if (ctf_dynhash_insert (wrapper->ctfi_symnamedicts, tmp, &enosym) < 0)
  841. free (tmp);
  842. }
  843. no_sym:
  844. if (errp)
  845. *errp = ECTF_NOTYPEDAT;
  846. if (typep)
  847. *typep = CTF_ERR;
  848. return NULL;
  849. }
  850. /* The public API for looking up a symbol by index. */
  851. ctf_dict_t *
  852. ctf_arc_lookup_symbol (ctf_archive_t *wrapper, unsigned long symidx,
  853. ctf_id_t *typep, int *errp)
  854. {
  855. return ctf_arc_lookup_sym_or_name (wrapper, symidx, NULL, typep, errp);
  856. }
  857. /* The public API for looking up a symbol by name. */
  858. ctf_dict_t *
  859. ctf_arc_lookup_symbol_name (ctf_archive_t *wrapper, const char *symname,
  860. ctf_id_t *typep, int *errp)
  861. {
  862. return ctf_arc_lookup_sym_or_name (wrapper, 0, symname, typep, errp);
  863. }
  864. /* Raw iteration over all CTF files in an archive. We pass the raw data for all
  865. CTF files in turn to the specified callback function. */
  866. static int
  867. ctf_archive_raw_iter_internal (const struct ctf_archive *arc,
  868. ctf_archive_raw_member_f *func, void *data)
  869. {
  870. int rc;
  871. size_t i;
  872. struct ctf_archive_modent *modent;
  873. const char *nametbl;
  874. modent = (ctf_archive_modent_t *) ((char *) arc
  875. + sizeof (struct ctf_archive));
  876. nametbl = (((const char *) arc) + le64toh (arc->ctfa_names));
  877. for (i = 0; i < le64toh (arc->ctfa_ndicts); i++)
  878. {
  879. const char *name;
  880. char *fp;
  881. name = &nametbl[le64toh (modent[i].name_offset)];
  882. fp = ((char *) arc + le64toh (arc->ctfa_ctfs)
  883. + le64toh (modent[i].ctf_offset));
  884. if ((rc = func (name, (void *) (fp + sizeof (uint64_t)),
  885. le64toh (*((uint64_t *) fp)), data)) != 0)
  886. return rc;
  887. }
  888. return 0;
  889. }
  890. /* Raw iteration over all CTF files in an archive: public entry point.
  891. Returns -EINVAL if not supported for this sort of archive. */
  892. int
  893. ctf_archive_raw_iter (const ctf_archive_t *arc,
  894. ctf_archive_raw_member_f * func, void *data)
  895. {
  896. if (arc->ctfi_is_archive)
  897. return ctf_archive_raw_iter_internal (arc->ctfi_archive, func, data);
  898. return -EINVAL; /* Not supported. */
  899. }
  900. /* Iterate over all CTF files in an archive: public entry point. We pass all
  901. CTF files in turn to the specified callback function. */
  902. int
  903. ctf_archive_iter (const ctf_archive_t *arc, ctf_archive_member_f *func,
  904. void *data)
  905. {
  906. ctf_next_t *i = NULL;
  907. ctf_dict_t *fp;
  908. const char *name;
  909. int err;
  910. while ((fp = ctf_archive_next (arc, &i, &name, 0, &err)) != NULL)
  911. {
  912. int rc;
  913. if ((rc = func (fp, name, data)) != 0)
  914. {
  915. ctf_dict_close (fp);
  916. ctf_next_destroy (i);
  917. return rc;
  918. }
  919. ctf_dict_close (fp);
  920. }
  921. return 0;
  922. }
  923. /* Iterate over all CTF files in an archive, returning each dict in turn as a
  924. ctf_dict_t, and NULL on error or end of iteration. It is the caller's
  925. responsibility to close it. Parent dicts may be skipped.
  926. The archive member is cached for rapid return on future calls.
  927. We identify parents by name rather than by flag value: for now, with the
  928. linker only emitting parents named _CTF_SECTION, this works well enough. */
  929. ctf_dict_t *
  930. ctf_archive_next (const ctf_archive_t *wrapper, ctf_next_t **it, const char **name,
  931. int skip_parent, int *errp)
  932. {
  933. ctf_dict_t *f;
  934. ctf_next_t *i = *it;
  935. struct ctf_archive *arc;
  936. struct ctf_archive_modent *modent;
  937. const char *nametbl;
  938. const char *name_;
  939. if (!i)
  940. {
  941. if ((i = ctf_next_create()) == NULL)
  942. {
  943. if (errp)
  944. *errp = ENOMEM;
  945. return NULL;
  946. }
  947. i->cu.ctn_arc = wrapper;
  948. i->ctn_iter_fun = (void (*) (void)) ctf_archive_next;
  949. *it = i;
  950. }
  951. if ((void (*) (void)) ctf_archive_next != i->ctn_iter_fun)
  952. {
  953. if (errp)
  954. *errp = ECTF_NEXT_WRONGFUN;
  955. return NULL;
  956. }
  957. if (wrapper != i->cu.ctn_arc)
  958. {
  959. if (errp)
  960. *errp = ECTF_NEXT_WRONGFP;
  961. return NULL;
  962. }
  963. /* Iteration is made a bit more complex by the need to handle ctf_dict_t's
  964. transparently wrapped in a single-member archive. These are parents: if
  965. skip_parent is on, they are skipped and the iterator terminates
  966. immediately. */
  967. if (!wrapper->ctfi_is_archive && i->ctn_n == 0)
  968. {
  969. i->ctn_n++;
  970. if (!skip_parent)
  971. {
  972. wrapper->ctfi_dict->ctf_refcnt++;
  973. if (name)
  974. *name = _CTF_SECTION;
  975. return wrapper->ctfi_dict;
  976. }
  977. }
  978. arc = wrapper->ctfi_archive;
  979. /* The loop keeps going when skip_parent is on as long as the member we find
  980. is the parent (i.e. at most two iterations, but possibly an early return if
  981. *all* we have is a parent). */
  982. do
  983. {
  984. if ((!wrapper->ctfi_is_archive) || (i->ctn_n >= le64toh (arc->ctfa_ndicts)))
  985. {
  986. ctf_next_destroy (i);
  987. *it = NULL;
  988. if (errp)
  989. *errp = ECTF_NEXT_END;
  990. return NULL;
  991. }
  992. modent = (ctf_archive_modent_t *) ((char *) arc
  993. + sizeof (struct ctf_archive));
  994. nametbl = (((const char *) arc) + le64toh (arc->ctfa_names));
  995. name_ = &nametbl[le64toh (modent[i->ctn_n].name_offset)];
  996. i->ctn_n++;
  997. }
  998. while (skip_parent && strcmp (name_, _CTF_SECTION) == 0);
  999. if (name)
  1000. *name = name_;
  1001. f = ctf_dict_open_cached ((ctf_archive_t *) wrapper, name_, errp);
  1002. return f;
  1003. }
  1004. #ifdef HAVE_MMAP
  1005. /* Map the header in. Only used on new, empty files. */
  1006. static void *arc_mmap_header (int fd, size_t headersz)
  1007. {
  1008. void *hdr;
  1009. if ((hdr = mmap (NULL, headersz, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
  1010. 0)) == MAP_FAILED)
  1011. return NULL;
  1012. return hdr;
  1013. }
  1014. /* mmap() the whole file, for reading only. (Map it writably, but privately: we
  1015. need to modify the region, but don't need anyone else to see the
  1016. modifications.) */
  1017. static void *arc_mmap_file (int fd, size_t size)
  1018. {
  1019. void *arc;
  1020. if ((arc = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
  1021. fd, 0)) == MAP_FAILED)
  1022. return NULL;
  1023. return arc;
  1024. }
  1025. /* Persist the header to disk. */
  1026. static int arc_mmap_writeout (int fd _libctf_unused_, void *header,
  1027. size_t headersz, const char **errmsg)
  1028. {
  1029. if (msync (header, headersz, MS_ASYNC) < 0)
  1030. {
  1031. if (errmsg)
  1032. *errmsg = N_("arc_mmap_writeout(): cannot sync after writing "
  1033. "to %s: %s");
  1034. return -1;
  1035. }
  1036. return 0;
  1037. }
  1038. /* Unmap the region. */
  1039. static int arc_mmap_unmap (void *header, size_t headersz, const char **errmsg)
  1040. {
  1041. if (munmap (header, headersz) < 0)
  1042. {
  1043. if (errmsg)
  1044. *errmsg = N_("arc_mmap_munmap(): cannot unmap after writing "
  1045. "to %s: %s");
  1046. return -1;
  1047. }
  1048. return 0;
  1049. }
  1050. #else
  1051. /* Map the header in. Only used on new, empty files. */
  1052. static void *arc_mmap_header (int fd _libctf_unused_, size_t headersz)
  1053. {
  1054. void *hdr;
  1055. if ((hdr = malloc (headersz)) == NULL)
  1056. return NULL;
  1057. return hdr;
  1058. }
  1059. /* Pull in the whole file, for reading only. We assume the current file
  1060. position is at the start of the file. */
  1061. static void *arc_mmap_file (int fd, size_t size)
  1062. {
  1063. char *data;
  1064. if ((data = malloc (size)) == NULL)
  1065. return NULL;
  1066. if (ctf_pread (fd, data, size, 0) < 0)
  1067. {
  1068. free (data);
  1069. return NULL;
  1070. }
  1071. return data;
  1072. }
  1073. /* Persist the header to disk. */
  1074. static int arc_mmap_writeout (int fd, void *header, size_t headersz,
  1075. const char **errmsg)
  1076. {
  1077. ssize_t len;
  1078. size_t acc = 0;
  1079. char *data = (char *) header;
  1080. ssize_t count = headersz;
  1081. if ((lseek (fd, 0, SEEK_SET)) < 0)
  1082. {
  1083. if (errmsg)
  1084. *errmsg = N_("arc_mmap_writeout(): cannot seek while writing header to "
  1085. "%s: %s");
  1086. return -1;
  1087. }
  1088. while (headersz > 0)
  1089. {
  1090. if ((len = write (fd, data, count)) < 0)
  1091. {
  1092. if (errmsg)
  1093. *errmsg = N_("arc_mmap_writeout(): cannot write header to %s: %s");
  1094. return len;
  1095. }
  1096. if (len == EINTR)
  1097. continue;
  1098. acc += len;
  1099. if (len == 0) /* EOF. */
  1100. break;
  1101. count -= len;
  1102. data += len;
  1103. }
  1104. return 0;
  1105. }
  1106. /* Unmap the region. */
  1107. static int arc_mmap_unmap (void *header, size_t headersz _libctf_unused_,
  1108. const char **errmsg _libctf_unused_)
  1109. {
  1110. free (header);
  1111. return 0;
  1112. }
  1113. #endif