ctf-link.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  1. /* CTF linking.
  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 <string.h>
  17. #if defined (PIC)
  18. #pragma weak ctf_open
  19. #endif
  20. /* CTF linking consists of adding CTF archives full of content to be merged into
  21. this one to the current file (which must be writable) by calling
  22. ctf_link_add_ctf. Once this is done, a call to ctf_link will merge the type
  23. tables together, generating new CTF files as needed, with this one as a
  24. parent, to contain types from the inputs which conflict. ctf_link_add_strtab
  25. takes a callback which provides string/offset pairs to be added to the
  26. external symbol table and deduplicated from all CTF string tables in the
  27. output link; ctf_link_shuffle_syms takes a callback which provides symtab
  28. entries in ascending order, and shuffles the function and data sections to
  29. match; and ctf_link_write emits a CTF file (if there are no conflicts
  30. requiring per-compilation-unit sub-CTF files) or CTF archives (otherwise) and
  31. returns it, suitable for addition in the .ctf section of the output. */
  32. /* Return the name of the compilation unit this CTF dict or its parent applies
  33. to, or a non-null string otherwise: prefer the parent. Used in debugging
  34. output. Sometimes used for outputs too. */
  35. const char *
  36. ctf_link_input_name (ctf_dict_t *fp)
  37. {
  38. if (fp->ctf_parent && fp->ctf_parent->ctf_cuname)
  39. return fp->ctf_parent->ctf_cuname;
  40. else if (fp->ctf_cuname)
  41. return fp->ctf_cuname;
  42. else
  43. return "(unnamed)";
  44. }
  45. /* Return the cuname of a dict, or the string "unnamed-CU" if none. */
  46. static const char *
  47. ctf_unnamed_cuname (ctf_dict_t *fp)
  48. {
  49. const char *cuname = ctf_cuname (fp);
  50. if (!cuname)
  51. cuname = "unnamed-CU";
  52. return cuname;
  53. }
  54. /* The linker inputs look like this. clin_fp is used for short-circuited
  55. CU-mapped links that can entirely avoid the first link phase in some
  56. situations in favour of just passing on the contained ctf_dict_t: it is
  57. always the sole ctf_dict_t inside the corresponding clin_arc. If set, it
  58. gets assigned directly to the final link inputs and freed from there, so it
  59. never gets explicitly freed in the ctf_link_input. */
  60. typedef struct ctf_link_input
  61. {
  62. const char *clin_filename;
  63. ctf_archive_t *clin_arc;
  64. ctf_dict_t *clin_fp;
  65. int n;
  66. } ctf_link_input_t;
  67. static void
  68. ctf_link_input_close (void *input)
  69. {
  70. ctf_link_input_t *i = (ctf_link_input_t *) input;
  71. if (i->clin_arc)
  72. ctf_arc_close (i->clin_arc);
  73. free (i);
  74. }
  75. /* Like ctf_link_add_ctf, below, but with no error-checking, so it can be called
  76. in the middle of an ongoing link. */
  77. static int
  78. ctf_link_add_ctf_internal (ctf_dict_t *fp, ctf_archive_t *ctf,
  79. ctf_dict_t *fp_input, const char *name)
  80. {
  81. ctf_link_input_t *input = NULL;
  82. char *dupname = NULL;
  83. if ((input = calloc (1, sizeof (ctf_link_input_t))) == NULL)
  84. goto oom;
  85. if ((dupname = strdup (name)) == NULL)
  86. goto oom;
  87. input->clin_arc = ctf;
  88. input->clin_fp = fp_input;
  89. input->clin_filename = dupname;
  90. input->n = ctf_dynhash_elements (fp->ctf_link_inputs);
  91. if (ctf_dynhash_insert (fp->ctf_link_inputs, dupname, input) < 0)
  92. goto oom;
  93. return 0;
  94. oom:
  95. free (input);
  96. free (dupname);
  97. return ctf_set_errno (fp, ENOMEM);
  98. }
  99. /* Add a file, memory buffer, or unopened file (by name) to a link.
  100. You can call this with:
  101. CTF and NAME: link the passed ctf_archive_t, with the given NAME.
  102. NAME alone: open NAME as a CTF file when needed.
  103. BUF and NAME: open the BUF (of length N) as CTF, with the given NAME. (Not
  104. yet implemented.)
  105. Passed in CTF args are owned by the dictionary and will be freed by it.
  106. The BUF arg is *not* owned by the dictionary, and the user should not free
  107. its referent until the link is done.
  108. The order of calls to this function influences the order of types in the
  109. final link output, but otherwise is not important.
  110. Private for now, but may in time become public once support for BUF is
  111. implemented. */
  112. static int
  113. ctf_link_add (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name,
  114. void *buf _libctf_unused_, size_t n _libctf_unused_)
  115. {
  116. if (buf)
  117. return (ctf_set_errno (fp, ECTF_NOTYET));
  118. if (!((ctf && name && !buf)
  119. || (name && !buf && !ctf)
  120. || (buf && name && !ctf)))
  121. return (ctf_set_errno (fp, EINVAL));
  122. /* We can only lazily open files if libctf.so is in use rather than
  123. libctf-nobfd.so. This is a little tricky: in shared libraries, we can use
  124. a weak symbol so that -lctf -lctf-nobfd works, but in static libraries we
  125. must distinguish between the two libraries explicitly. */
  126. #if defined (PIC)
  127. if (!buf && !ctf && name && !ctf_open)
  128. return (ctf_set_errno (fp, ECTF_NEEDSBFD));
  129. #elif NOBFD
  130. if (!buf && !ctf && name)
  131. return (ctf_set_errno (fp, ECTF_NEEDSBFD));
  132. #endif
  133. if (fp->ctf_link_outputs)
  134. return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
  135. if (fp->ctf_link_inputs == NULL)
  136. fp->ctf_link_inputs = ctf_dynhash_create (ctf_hash_string,
  137. ctf_hash_eq_string, free,
  138. ctf_link_input_close);
  139. if (fp->ctf_link_inputs == NULL)
  140. return (ctf_set_errno (fp, ENOMEM));
  141. return ctf_link_add_ctf_internal (fp, ctf, NULL, name);
  142. }
  143. /* Add an opened CTF archive or unopened file (by name) to a link.
  144. If CTF is NULL and NAME is non-null, an unopened file is meant:
  145. otherwise, the specified archive is assumed to have the given NAME.
  146. Passed in CTF args are owned by the dictionary and will be freed by it.
  147. The order of calls to this function influences the order of types in the
  148. final link output, but otherwise is not important. */
  149. int
  150. ctf_link_add_ctf (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name)
  151. {
  152. return ctf_link_add (fp, ctf, name, NULL, 0);
  153. }
  154. /* Lazily open a CTF archive for linking, if not already open.
  155. Returns the number of files contained within the opened archive (0 for none),
  156. or -1 on error, as usual. */
  157. static ssize_t
  158. ctf_link_lazy_open (ctf_dict_t *fp, ctf_link_input_t *input)
  159. {
  160. size_t count;
  161. int err;
  162. if (input->clin_arc)
  163. return ctf_archive_count (input->clin_arc);
  164. if (input->clin_fp)
  165. return 1;
  166. /* See ctf_link_add_ctf. */
  167. #if defined (PIC) || !NOBFD
  168. input->clin_arc = ctf_open (input->clin_filename, NULL, &err);
  169. #else
  170. ctf_err_warn (fp, 0, ECTF_NEEDSBFD, _("cannot open %s lazily"),
  171. input->clin_filename);
  172. ctf_set_errno (fp, ECTF_NEEDSBFD);
  173. return -1;
  174. #endif
  175. /* Having no CTF sections is not an error. We just don't need to do
  176. anything. */
  177. if (!input->clin_arc)
  178. {
  179. if (err == ECTF_NOCTFDATA)
  180. return 0;
  181. ctf_err_warn (fp, 0, err, _("opening CTF %s failed"),
  182. input->clin_filename);
  183. ctf_set_errno (fp, err);
  184. return -1;
  185. }
  186. if ((count = ctf_archive_count (input->clin_arc)) == 0)
  187. ctf_arc_close (input->clin_arc);
  188. return (ssize_t) count;
  189. }
  190. /* Return a per-CU output CTF dictionary suitable for the given CU, creating and
  191. interning it if need be. */
  192. _libctf_nonnull_((1,2))
  193. static ctf_dict_t *
  194. ctf_create_per_cu (ctf_dict_t *fp, const char *cu_name)
  195. {
  196. ctf_dict_t *cu_fp;
  197. const char *ctf_name = NULL;
  198. char *dynname = NULL;
  199. /* First, check the mapping table and translate the per-CU name we use
  200. accordingly. */
  201. if (fp->ctf_link_in_cu_mapping)
  202. {
  203. if ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping,
  204. cu_name)) == NULL)
  205. ctf_name = cu_name;
  206. }
  207. if (ctf_name == NULL)
  208. ctf_name = cu_name;
  209. if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL)
  210. {
  211. int err;
  212. if ((cu_fp = ctf_create (&err)) == NULL)
  213. {
  214. ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive for "
  215. "input CU %s"), cu_name);
  216. ctf_set_errno (fp, err);
  217. return NULL;
  218. }
  219. if ((dynname = strdup (ctf_name)) == NULL)
  220. goto oom;
  221. if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0)
  222. goto oom;
  223. ctf_import_unref (cu_fp, fp);
  224. ctf_cuname_set (cu_fp, cu_name);
  225. ctf_parent_name_set (cu_fp, _CTF_SECTION);
  226. }
  227. return cu_fp;
  228. oom:
  229. free (dynname);
  230. ctf_dict_close (cu_fp);
  231. ctf_set_errno (fp, ENOMEM);
  232. return NULL;
  233. }
  234. /* Add a mapping directing that the CU named FROM should have its
  235. conflicting/non-duplicate types (depending on link mode) go into a dict
  236. named TO. Many FROMs can share a TO.
  237. We forcibly add a dict named TO in every case, even though it may well
  238. wind up empty, because clients that use this facility usually expect to find
  239. every TO dict present, even if empty, and malfunction otherwise. */
  240. int
  241. ctf_link_add_cu_mapping (ctf_dict_t *fp, const char *from, const char *to)
  242. {
  243. int err;
  244. char *f = NULL, *t = NULL;
  245. ctf_dynhash_t *one_out;
  246. if (fp->ctf_link_in_cu_mapping == NULL)
  247. fp->ctf_link_in_cu_mapping = ctf_dynhash_create (ctf_hash_string,
  248. ctf_hash_eq_string, free,
  249. free);
  250. if (fp->ctf_link_in_cu_mapping == NULL)
  251. goto oom;
  252. if (fp->ctf_link_out_cu_mapping == NULL)
  253. fp->ctf_link_out_cu_mapping = ctf_dynhash_create (ctf_hash_string,
  254. ctf_hash_eq_string, free,
  255. (ctf_hash_free_fun)
  256. ctf_dynhash_destroy);
  257. if (fp->ctf_link_out_cu_mapping == NULL)
  258. goto oom;
  259. f = strdup (from);
  260. t = strdup (to);
  261. if (!f || !t)
  262. goto oom;
  263. /* Track both in a list from FROM to TO and in a list from TO to a list of
  264. FROM. The former is used to create TUs with the mapped-to name at need:
  265. the latter is used in deduplicating links to pull in all input CUs
  266. corresponding to a single output CU. */
  267. if ((err = ctf_dynhash_insert (fp->ctf_link_in_cu_mapping, f, t)) < 0)
  268. {
  269. ctf_set_errno (fp, err);
  270. goto oom_noerrno;
  271. }
  272. /* f and t are now owned by the in_cu_mapping: reallocate them. */
  273. f = strdup (from);
  274. t = strdup (to);
  275. if (!f || !t)
  276. goto oom;
  277. if ((one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, t)) == NULL)
  278. {
  279. if ((one_out = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
  280. free, NULL)) == NULL)
  281. goto oom;
  282. if ((err = ctf_dynhash_insert (fp->ctf_link_out_cu_mapping,
  283. t, one_out)) < 0)
  284. {
  285. ctf_dynhash_destroy (one_out);
  286. ctf_set_errno (fp, err);
  287. goto oom_noerrno;
  288. }
  289. }
  290. else
  291. free (t);
  292. if (ctf_dynhash_insert (one_out, f, NULL) < 0)
  293. {
  294. ctf_set_errno (fp, err);
  295. goto oom_noerrno;
  296. }
  297. return 0;
  298. oom:
  299. ctf_set_errno (fp, errno);
  300. oom_noerrno:
  301. free (f);
  302. free (t);
  303. return -1;
  304. }
  305. /* Set a function which is called to transform the names of archive members.
  306. This is useful for applying regular transformations to many names, where
  307. ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
  308. names. The member name changer is applied at ctf_link_write time, so it
  309. cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
  310. The changer function accepts a name and should return a new
  311. dynamically-allocated name, or NULL if the name should be left unchanged. */
  312. void
  313. ctf_link_set_memb_name_changer (ctf_dict_t *fp,
  314. ctf_link_memb_name_changer_f *changer,
  315. void *arg)
  316. {
  317. fp->ctf_link_memb_name_changer = changer;
  318. fp->ctf_link_memb_name_changer_arg = arg;
  319. }
  320. /* Set a function which is used to filter out unwanted variables from the link. */
  321. int
  322. ctf_link_set_variable_filter (ctf_dict_t *fp, ctf_link_variable_filter_f *filter,
  323. void *arg)
  324. {
  325. fp->ctf_link_variable_filter = filter;
  326. fp->ctf_link_variable_filter_arg = arg;
  327. return 0;
  328. }
  329. /* Check if we can safely add a variable with the given type to this dict. */
  330. static int
  331. check_variable (const char *name, ctf_dict_t *fp, ctf_id_t type,
  332. ctf_dvdef_t **out_dvd)
  333. {
  334. ctf_dvdef_t *dvd;
  335. dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name);
  336. *out_dvd = dvd;
  337. if (!dvd)
  338. return 1;
  339. if (dvd->dvd_type != type)
  340. {
  341. /* Variable here. Wrong type: cannot add. Just skip it, because there is
  342. no way to express this in CTF. Don't even warn: this case is too
  343. common. (This might be the parent, in which case we'll try adding in
  344. the child first, and only then give up.) */
  345. ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name);
  346. }
  347. return 0; /* Already exists. */
  348. }
  349. /* Link one variable named NAME of type TYPE found in IN_FP into FP. */
  350. static int
  351. ctf_link_one_variable (ctf_dict_t *fp, ctf_dict_t *in_fp, const char *name,
  352. ctf_id_t type, int cu_mapped)
  353. {
  354. ctf_dict_t *per_cu_out_fp;
  355. ctf_id_t dst_type = 0;
  356. ctf_dvdef_t *dvd;
  357. /* See if this variable is filtered out. */
  358. if (fp->ctf_link_variable_filter)
  359. {
  360. void *farg = fp->ctf_link_variable_filter_arg;
  361. if (fp->ctf_link_variable_filter (in_fp, name, type, farg))
  362. return 0;
  363. }
  364. /* If this type is mapped to a type in the parent dict, we want to try to add
  365. to that first: if it reports a duplicate, or if the type is in a child
  366. already, add straight to the child. */
  367. if ((dst_type = ctf_dedup_type_mapping (fp, in_fp, type)) == CTF_ERR)
  368. return -1; /* errno is set for us. */
  369. if (dst_type != 0)
  370. {
  371. if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
  372. return -1; /* errno is set for us. */
  373. if (check_variable (name, fp, dst_type, &dvd))
  374. {
  375. /* No variable here: we can add it. */
  376. if (ctf_add_variable (fp, name, dst_type) < 0)
  377. return -1; /* errno is set for us. */
  378. return 0;
  379. }
  380. /* Already present? Nothing to do. */
  381. if (dvd && dvd->dvd_type == dst_type)
  382. return 0;
  383. }
  384. /* Can't add to the parent due to a name clash, or because it references a
  385. type only present in the child. Try adding to the child, creating if need
  386. be. If we can't do that, skip it. Don't add to a child if we're doing a
  387. CU-mapped link, since that has only one output. */
  388. if (cu_mapped)
  389. {
  390. ctf_dprintf ("Variable %s in input file %s depends on a type %lx hidden "
  391. "due to conflicts: skipped.\n", name,
  392. ctf_unnamed_cuname (in_fp), type);
  393. return 0;
  394. }
  395. if ((per_cu_out_fp = ctf_create_per_cu (fp, ctf_unnamed_cuname (in_fp))) == NULL)
  396. return -1; /* errno is set for us. */
  397. /* If the type was not found, check for it in the child too. */
  398. if (dst_type == 0)
  399. {
  400. if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
  401. in_fp, type)) == CTF_ERR)
  402. return -1; /* errno is set for us. */
  403. if (dst_type == 0)
  404. {
  405. ctf_err_warn (fp, 1, 0, _("type %lx for variable %s in input file %s "
  406. "not found: skipped"), type, name,
  407. ctf_unnamed_cuname (in_fp));
  408. /* Do not terminate the link: just skip the variable. */
  409. return 0;
  410. }
  411. }
  412. if (check_variable (name, per_cu_out_fp, dst_type, &dvd))
  413. if (ctf_add_variable (per_cu_out_fp, name, dst_type) < 0)
  414. return (ctf_set_errno (fp, ctf_errno (per_cu_out_fp)));
  415. return 0;
  416. }
  417. typedef struct link_sort_inputs_cb_arg
  418. {
  419. int is_cu_mapped;
  420. ctf_dict_t *fp;
  421. } link_sort_inputs_cb_arg_t;
  422. /* Sort the inputs by N (the link order). For CU-mapped links, this is a
  423. mapping of input to output name, not a mapping of input name to input
  424. ctf_link_input_t: compensate accordingly. */
  425. static int
  426. ctf_link_sort_inputs (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
  427. void *arg)
  428. {
  429. ctf_link_input_t *input_1;
  430. ctf_link_input_t *input_2;
  431. link_sort_inputs_cb_arg_t *cu_mapped = (link_sort_inputs_cb_arg_t *) arg;
  432. if (!cu_mapped || !cu_mapped->is_cu_mapped)
  433. {
  434. input_1 = (ctf_link_input_t *) one->hkv_value;
  435. input_2 = (ctf_link_input_t *) two->hkv_value;
  436. }
  437. else
  438. {
  439. const char *name_1 = (const char *) one->hkv_key;
  440. const char *name_2 = (const char *) two->hkv_key;
  441. input_1 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_1);
  442. input_2 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_2);
  443. /* There is no guarantee that CU-mappings actually have corresponding
  444. inputs: the relative ordering in that case is unimportant. */
  445. if (!input_1)
  446. return -1;
  447. if (!input_2)
  448. return 1;
  449. }
  450. if (input_1->n < input_2->n)
  451. return -1;
  452. else if (input_1->n > input_2->n)
  453. return 1;
  454. else
  455. return 0;
  456. }
  457. /* Count the number of input dicts in the ctf_link_inputs, or that subset of the
  458. ctf_link_inputs given by CU_NAMES if set. Return the number of input dicts,
  459. and optionally the name and ctf_link_input_t of the single input archive if
  460. only one exists (no matter how many dicts it contains). */
  461. static ssize_t
  462. ctf_link_deduplicating_count_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
  463. ctf_link_input_t **only_one_input)
  464. {
  465. ctf_dynhash_t *inputs = fp->ctf_link_inputs;
  466. ctf_next_t *i = NULL;
  467. void *name, *input;
  468. ctf_link_input_t *one_input = NULL;
  469. const char *one_name = NULL;
  470. ssize_t count = 0, narcs = 0;
  471. int err;
  472. if (cu_names)
  473. inputs = cu_names;
  474. while ((err = ctf_dynhash_next (inputs, &i, &name, &input)) == 0)
  475. {
  476. ssize_t one_count;
  477. one_name = (const char *) name;
  478. /* If we are processing CU names, get the real input. */
  479. if (cu_names)
  480. one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
  481. else
  482. one_input = (ctf_link_input_t *) input;
  483. if (!one_input)
  484. continue;
  485. one_count = ctf_link_lazy_open (fp, one_input);
  486. if (one_count < 0)
  487. {
  488. ctf_next_destroy (i);
  489. return -1; /* errno is set for us. */
  490. }
  491. count += one_count;
  492. narcs++;
  493. }
  494. if (err != ECTF_NEXT_END)
  495. {
  496. ctf_err_warn (fp, 0, err, _("iteration error counting deduplicating "
  497. "CTF link inputs"));
  498. ctf_set_errno (fp, err);
  499. return -1;
  500. }
  501. if (!count)
  502. return 0;
  503. if (narcs == 1)
  504. {
  505. if (only_one_input)
  506. *only_one_input = one_input;
  507. }
  508. else if (only_one_input)
  509. *only_one_input = NULL;
  510. return count;
  511. }
  512. /* Allocate and populate an inputs array big enough for a given set of inputs:
  513. either a specific set of CU names (those from that set found in the
  514. ctf_link_inputs), or the entire ctf_link_inputs (if cu_names is not set).
  515. The number of inputs (from ctf_link_deduplicating_count_inputs, above) is
  516. passed in NINPUTS: an array of uint32_t containing parent pointers
  517. (corresponding to those members of the inputs that have parents) is allocated
  518. and returned in PARENTS.
  519. The inputs are *archives*, not files: the archive can have multiple members
  520. if it is the result of a previous incremental link. We want to add every one
  521. in turn, including the shared parent. (The dedup machinery knows that a type
  522. used by a single dictionary and its parent should not be shared in
  523. CTF_LINK_SHARE_DUPLICATED mode.)
  524. If no inputs exist that correspond to these CUs, return NULL with the errno
  525. set to ECTF_NOCTFDATA. */
  526. static ctf_dict_t **
  527. ctf_link_deduplicating_open_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
  528. ssize_t ninputs, uint32_t **parents)
  529. {
  530. ctf_dynhash_t *inputs = fp->ctf_link_inputs;
  531. ctf_next_t *i = NULL;
  532. void *name, *input;
  533. link_sort_inputs_cb_arg_t sort_arg;
  534. ctf_dict_t **dedup_inputs = NULL;
  535. ctf_dict_t **walk;
  536. uint32_t *parents_ = NULL;
  537. int err;
  538. if (cu_names)
  539. inputs = cu_names;
  540. if ((dedup_inputs = calloc (ninputs, sizeof (ctf_dict_t *))) == NULL)
  541. goto oom;
  542. if ((parents_ = calloc (ninputs, sizeof (uint32_t))) == NULL)
  543. goto oom;
  544. walk = dedup_inputs;
  545. /* Counting done: push every input into the array, in the order they were
  546. passed to ctf_link_add_ctf (and ultimately ld). */
  547. sort_arg.is_cu_mapped = (cu_names != NULL);
  548. sort_arg.fp = fp;
  549. while ((err = ctf_dynhash_next_sorted (inputs, &i, &name, &input,
  550. ctf_link_sort_inputs, &sort_arg)) == 0)
  551. {
  552. const char *one_name = (const char *) name;
  553. ctf_link_input_t *one_input;
  554. ctf_dict_t *one_fp;
  555. ctf_dict_t *parent_fp = NULL;
  556. uint32_t parent_i;
  557. ctf_next_t *j = NULL;
  558. /* If we are processing CU names, get the real input. All the inputs
  559. will have been opened, if they contained any CTF at all. */
  560. if (cu_names)
  561. one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
  562. else
  563. one_input = (ctf_link_input_t *) input;
  564. if (!one_input || (!one_input->clin_arc && !one_input->clin_fp))
  565. continue;
  566. /* Short-circuit: if clin_fp is set, just use it. */
  567. if (one_input->clin_fp)
  568. {
  569. parents_[walk - dedup_inputs] = walk - dedup_inputs;
  570. *walk = one_input->clin_fp;
  571. walk++;
  572. continue;
  573. }
  574. /* Get and insert the parent archive (if any), if this archive has
  575. multiple members. We assume, as elsewhere, that the parent is named
  576. _CTF_SECTION. */
  577. if ((parent_fp = ctf_dict_open (one_input->clin_arc, _CTF_SECTION,
  578. &err)) == NULL)
  579. {
  580. if (err != ECTF_NOMEMBNAM)
  581. {
  582. ctf_next_destroy (i);
  583. ctf_set_errno (fp, err);
  584. goto err;
  585. }
  586. }
  587. else
  588. {
  589. *walk = parent_fp;
  590. parent_i = walk - dedup_inputs;
  591. walk++;
  592. }
  593. /* We disregard the input archive name: either it is the parent (which we
  594. already have), or we want to put everything into one TU sharing the
  595. cuname anyway (if this is a CU-mapped link), or this is the final phase
  596. of a relink with CU-mapping off (i.e. ld -r) in which case the cuname
  597. is correctly set regardless. */
  598. while ((one_fp = ctf_archive_next (one_input->clin_arc, &j, NULL,
  599. 1, &err)) != NULL)
  600. {
  601. if (one_fp->ctf_flags & LCTF_CHILD)
  602. {
  603. /* The contents of the parents array for elements not
  604. corresponding to children is undefined. If there is no parent
  605. (itself a sign of a likely linker bug or corrupt input), we set
  606. it to itself. */
  607. ctf_import (one_fp, parent_fp);
  608. if (parent_fp)
  609. parents_[walk - dedup_inputs] = parent_i;
  610. else
  611. parents_[walk - dedup_inputs] = walk - dedup_inputs;
  612. }
  613. *walk = one_fp;
  614. walk++;
  615. }
  616. if (err != ECTF_NEXT_END)
  617. {
  618. ctf_next_destroy (i);
  619. goto iterr;
  620. }
  621. }
  622. if (err != ECTF_NEXT_END)
  623. goto iterr;
  624. *parents = parents_;
  625. return dedup_inputs;
  626. oom:
  627. err = ENOMEM;
  628. iterr:
  629. ctf_set_errno (fp, err);
  630. err:
  631. free (dedup_inputs);
  632. free (parents_);
  633. ctf_err_warn (fp, 0, 0, _("error in deduplicating CTF link "
  634. "input allocation"));
  635. return NULL;
  636. }
  637. /* Close INPUTS that have already been linked, first the passed array, and then
  638. that subset of the ctf_link_inputs archives they came from cited by the
  639. CU_NAMES. If CU_NAMES is not specified, close all the ctf_link_inputs in one
  640. go, leaving it empty. */
  641. static int
  642. ctf_link_deduplicating_close_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
  643. ctf_dict_t **inputs, ssize_t ninputs)
  644. {
  645. ctf_next_t *it = NULL;
  646. void *name;
  647. int err;
  648. ssize_t i;
  649. /* This is the inverse of ctf_link_deduplicating_open_inputs: so first, close
  650. all the individual input dicts, opened by the archive iterator. */
  651. for (i = 0; i < ninputs; i++)
  652. ctf_dict_close (inputs[i]);
  653. /* Now close the archives they are part of. */
  654. if (cu_names)
  655. {
  656. while ((err = ctf_dynhash_next (cu_names, &it, &name, NULL)) == 0)
  657. {
  658. /* Remove the input from the linker inputs, if it exists, which also
  659. closes it. */
  660. ctf_dynhash_remove (fp->ctf_link_inputs, (const char *) name);
  661. }
  662. if (err != ECTF_NEXT_END)
  663. {
  664. ctf_err_warn (fp, 0, err, _("iteration error in deduplicating link "
  665. "input freeing"));
  666. ctf_set_errno (fp, err);
  667. }
  668. }
  669. else
  670. ctf_dynhash_empty (fp->ctf_link_inputs);
  671. return 0;
  672. }
  673. /* Do a deduplicating link of all variables in the inputs.
  674. Also, if we are not omitting the variable section, integrate all symbols from
  675. the symtypetabs into the variable section too. (Duplication with the
  676. symtypetab section in the output will be eliminated at serialization time.) */
  677. static int
  678. ctf_link_deduplicating_variables (ctf_dict_t *fp, ctf_dict_t **inputs,
  679. size_t ninputs, int cu_mapped)
  680. {
  681. size_t i;
  682. for (i = 0; i < ninputs; i++)
  683. {
  684. ctf_next_t *it = NULL;
  685. ctf_id_t type;
  686. const char *name;
  687. /* First the variables on the inputs. */
  688. while ((type = ctf_variable_next (inputs[i], &it, &name)) != CTF_ERR)
  689. {
  690. if (ctf_link_one_variable (fp, inputs[i], name, type, cu_mapped) < 0)
  691. {
  692. ctf_next_destroy (it);
  693. return -1; /* errno is set for us. */
  694. }
  695. }
  696. if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
  697. return ctf_set_errno (fp, ctf_errno (inputs[i]));
  698. /* Next the symbols. We integrate data symbols even though the compiler
  699. is currently doing the same, to allow the compiler to stop in
  700. future. */
  701. while ((type = ctf_symbol_next (inputs[i], &it, &name, 0)) != CTF_ERR)
  702. {
  703. if (ctf_link_one_variable (fp, inputs[i], name, type, 1) < 0)
  704. {
  705. ctf_next_destroy (it);
  706. return -1; /* errno is set for us. */
  707. }
  708. }
  709. if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
  710. return ctf_set_errno (fp, ctf_errno (inputs[i]));
  711. /* Finally the function symbols. */
  712. while ((type = ctf_symbol_next (inputs[i], &it, &name, 1)) != CTF_ERR)
  713. {
  714. if (ctf_link_one_variable (fp, inputs[i], name, type, 1) < 0)
  715. {
  716. ctf_next_destroy (it);
  717. return -1; /* errno is set for us. */
  718. }
  719. }
  720. if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
  721. return ctf_set_errno (fp, ctf_errno (inputs[i]));
  722. }
  723. return 0;
  724. }
  725. /* Check for symbol conflicts during linking. Three possibilities: already
  726. exists, conflicting, or nonexistent. We don't have a dvd structure we can
  727. use as a flag like check_variable does, so we use a tristate return
  728. value instead: -1: conflicting; 1: nonexistent: 0: already exists. */
  729. static int
  730. check_sym (ctf_dict_t *fp, const char *name, ctf_id_t type, int functions)
  731. {
  732. ctf_dynhash_t *thishash = functions ? fp->ctf_funchash : fp->ctf_objthash;
  733. ctf_dynhash_t *thathash = functions ? fp->ctf_objthash : fp->ctf_funchash;
  734. void *value;
  735. /* Wrong type (function when object is wanted, etc). */
  736. if (ctf_dynhash_lookup_kv (thathash, name, NULL, NULL))
  737. return -1;
  738. /* Not present at all yet. */
  739. if (!ctf_dynhash_lookup_kv (thishash, name, NULL, &value))
  740. return 1;
  741. /* Already present. */
  742. if ((ctf_id_t) (uintptr_t) value == type)
  743. return 0;
  744. /* Wrong type. */
  745. return -1;
  746. }
  747. /* Do a deduplicating link of one symtypetab (function info or data object) in
  748. one input dict. */
  749. static int
  750. ctf_link_deduplicating_one_symtypetab (ctf_dict_t *fp, ctf_dict_t *input,
  751. int cu_mapped, int functions)
  752. {
  753. ctf_next_t *it = NULL;
  754. const char *name;
  755. ctf_id_t type;
  756. while ((type = ctf_symbol_next (input, &it, &name, functions)) != CTF_ERR)
  757. {
  758. ctf_id_t dst_type;
  759. ctf_dict_t *per_cu_out_fp;
  760. int sym;
  761. /* Look in the parent first. */
  762. if ((dst_type = ctf_dedup_type_mapping (fp, input, type)) == CTF_ERR)
  763. return -1; /* errno is set for us. */
  764. if (dst_type != 0)
  765. {
  766. if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
  767. return -1; /* errno is set for us. */
  768. sym = check_sym (fp, name, dst_type, functions);
  769. /* Already present: next symbol. */
  770. if (sym == 0)
  771. continue;
  772. /* Not present: add it. */
  773. else if (sym > 0)
  774. {
  775. if (ctf_add_funcobjt_sym (fp, functions,
  776. name, dst_type) < 0)
  777. return -1; /* errno is set for us. */
  778. continue;
  779. }
  780. }
  781. /* Can't add to the parent due to a name clash (most unlikely), or because
  782. it references a type only present in the child. Try adding to the
  783. child, creating if need be. If we can't do that, skip it. Don't add
  784. to a child if we're doing a CU-mapped link, since that has only one
  785. output. */
  786. if (cu_mapped)
  787. {
  788. ctf_dprintf ("Symbol %s in input file %s depends on a type %lx "
  789. "hidden due to conflicts: skipped.\n", name,
  790. ctf_unnamed_cuname (input), type);
  791. continue;
  792. }
  793. if ((per_cu_out_fp = ctf_create_per_cu (fp, ctf_unnamed_cuname (input))) == NULL)
  794. return -1; /* errno is set for us. */
  795. /* If the type was not found, check for it in the child too. */
  796. if (dst_type == 0)
  797. {
  798. if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
  799. input, type)) == CTF_ERR)
  800. return -1; /* errno is set for us. */
  801. if (dst_type == 0)
  802. {
  803. ctf_err_warn (fp, 1, 0,
  804. _("type %lx for symbol %s in input file %s "
  805. "not found: skipped"), type, name,
  806. ctf_unnamed_cuname (input));
  807. continue;
  808. }
  809. }
  810. sym = check_sym (per_cu_out_fp, name, dst_type, functions);
  811. /* Already present: next symbol. */
  812. if (sym == 0)
  813. continue;
  814. /* Not present: add it. */
  815. else if (sym > 0)
  816. {
  817. if (ctf_add_funcobjt_sym (per_cu_out_fp, functions,
  818. name, dst_type) < 0)
  819. return -1; /* errno is set for us. */
  820. }
  821. else
  822. {
  823. /* Perhaps this should be an assertion failure. */
  824. ctf_err_warn (fp, 0, ECTF_DUPLICATE,
  825. _("symbol %s in input file %s found conflicting "
  826. "even when trying in per-CU dict."), name,
  827. ctf_unnamed_cuname (input));
  828. return (ctf_set_errno (fp, ECTF_DUPLICATE));
  829. }
  830. }
  831. if (ctf_errno (input) != ECTF_NEXT_END)
  832. {
  833. ctf_set_errno (fp, ctf_errno (input));
  834. ctf_err_warn (fp, 0, ctf_errno (input),
  835. functions ? _("iterating over function symbols") :
  836. _("iterating over data symbols"));
  837. return -1;
  838. }
  839. return 0;
  840. }
  841. /* Do a deduplicating link of the function info and data objects
  842. in the inputs. */
  843. static int
  844. ctf_link_deduplicating_syms (ctf_dict_t *fp, ctf_dict_t **inputs,
  845. size_t ninputs, int cu_mapped)
  846. {
  847. size_t i;
  848. for (i = 0; i < ninputs; i++)
  849. {
  850. if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
  851. cu_mapped, 0) < 0)
  852. return -1; /* errno is set for us. */
  853. if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
  854. cu_mapped, 1) < 0)
  855. return -1; /* errno is set for us. */
  856. }
  857. return 0;
  858. }
  859. /* Do the per-CU part of a deduplicating link. */
  860. static int
  861. ctf_link_deduplicating_per_cu (ctf_dict_t *fp)
  862. {
  863. ctf_next_t *i = NULL;
  864. int err;
  865. void *out_cu;
  866. void *in_cus;
  867. /* Links with a per-CU mapping in force get a first pass of deduplication,
  868. dedupping the inputs for a given CU mapping into the output for that
  869. mapping. The outputs from this process get fed back into the final pass
  870. that is carried out even for non-CU links. */
  871. while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &out_cu,
  872. &in_cus)) == 0)
  873. {
  874. const char *out_name = (const char *) out_cu;
  875. ctf_dynhash_t *in = (ctf_dynhash_t *) in_cus;
  876. ctf_dict_t *out = NULL;
  877. ctf_dict_t **inputs;
  878. ctf_dict_t **outputs;
  879. ctf_archive_t *in_arc;
  880. ssize_t ninputs;
  881. ctf_link_input_t *only_input;
  882. uint32_t noutputs;
  883. uint32_t *parents;
  884. if ((ninputs = ctf_link_deduplicating_count_inputs (fp, in,
  885. &only_input)) == -1)
  886. goto err_open_inputs;
  887. /* CU mapping with no inputs? Skip. */
  888. if (ninputs == 0)
  889. continue;
  890. if (labs ((long int) ninputs) > 0xfffffffe)
  891. {
  892. ctf_err_warn (fp, 0, EFBIG, _("too many inputs in deduplicating "
  893. "link: %li"), (long int) ninputs);
  894. ctf_set_errno (fp, EFBIG);
  895. goto err_open_inputs;
  896. }
  897. /* Short-circuit: a cu-mapped link with only one input archive with
  898. unconflicting contents is a do-nothing, and we can just leave the input
  899. in place: we do have to change the cuname, though, so we unwrap it,
  900. change the cuname, then stuff it back in the linker input again, via
  901. the clin_fp short-circuit member. ctf_link_deduplicating_open_inputs
  902. will spot this member and jam it straight into the next link phase,
  903. ignoring the corresponding archive. */
  904. if (only_input && ninputs == 1)
  905. {
  906. ctf_next_t *ai = NULL;
  907. int err;
  908. /* We can abuse an archive iterator to get the only member cheaply, no
  909. matter what its name. */
  910. only_input->clin_fp = ctf_archive_next (only_input->clin_arc,
  911. &ai, NULL, 0, &err);
  912. if (!only_input->clin_fp)
  913. {
  914. ctf_err_warn (fp, 0, err, _("cannot open archive %s in "
  915. "CU-mapped CTF link"),
  916. only_input->clin_filename);
  917. ctf_set_errno (fp, err);
  918. goto err_open_inputs;
  919. }
  920. ctf_next_destroy (ai);
  921. if (strcmp (only_input->clin_filename, out_name) != 0)
  922. {
  923. /* Renaming. We need to add a new input, then null out the
  924. clin_arc and clin_fp of the old one to stop it being
  925. auto-closed on removal. The new input needs its cuname changed
  926. to out_name, which is doable only because the cuname is a
  927. dynamic property which can be changed even in readonly
  928. dicts. */
  929. ctf_cuname_set (only_input->clin_fp, out_name);
  930. if (ctf_link_add_ctf_internal (fp, only_input->clin_arc,
  931. only_input->clin_fp,
  932. out_name) < 0)
  933. {
  934. ctf_err_warn (fp, 0, 0, _("cannot add intermediate files "
  935. "to link"));
  936. goto err_open_inputs;
  937. }
  938. only_input->clin_arc = NULL;
  939. only_input->clin_fp = NULL;
  940. ctf_dynhash_remove (fp->ctf_link_inputs,
  941. only_input->clin_filename);
  942. }
  943. continue;
  944. }
  945. /* This is a real CU many-to-one mapping: we must dedup the inputs into
  946. a new output to be used in the final link phase. */
  947. if ((inputs = ctf_link_deduplicating_open_inputs (fp, in, ninputs,
  948. &parents)) == NULL)
  949. {
  950. ctf_next_destroy (i);
  951. goto err_inputs;
  952. }
  953. if ((out = ctf_create (&err)) == NULL)
  954. {
  955. ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive "
  956. "for %s"),
  957. out_name);
  958. ctf_set_errno (fp, err);
  959. goto err_inputs;
  960. }
  961. /* Share the atoms table to reduce memory usage. */
  962. out->ctf_dedup_atoms = fp->ctf_dedup_atoms_alloc;
  963. /* No ctf_imports at this stage: this per-CU dictionary has no parents.
  964. Parent/child deduplication happens in the link's final pass. However,
  965. the cuname *is* important, as it is propagated into the final
  966. dictionary. */
  967. ctf_cuname_set (out, out_name);
  968. if (ctf_dedup (out, inputs, ninputs, parents, 1) < 0)
  969. {
  970. ctf_set_errno (fp, ctf_errno (out));
  971. ctf_err_warn (fp, 0, 0, _("CU-mapped deduplication failed for %s"),
  972. out_name);
  973. goto err_inputs;
  974. }
  975. if ((outputs = ctf_dedup_emit (out, inputs, ninputs, parents,
  976. &noutputs, 1)) == NULL)
  977. {
  978. ctf_set_errno (fp, ctf_errno (out));
  979. ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link type emission "
  980. "failed for %s"), out_name);
  981. goto err_inputs;
  982. }
  983. if (!ctf_assert (fp, noutputs == 1))
  984. {
  985. size_t j;
  986. for (j = 1; j < noutputs; j++)
  987. ctf_dict_close (outputs[j]);
  988. goto err_inputs_outputs;
  989. }
  990. if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
  991. && ctf_link_deduplicating_variables (out, inputs, ninputs, 1) < 0)
  992. {
  993. ctf_set_errno (fp, ctf_errno (out));
  994. ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link variable "
  995. "emission failed for %s"), out_name);
  996. goto err_inputs_outputs;
  997. }
  998. ctf_dedup_fini (out, outputs, noutputs);
  999. /* For now, we omit symbol section linking for CU-mapped links, until it
  1000. is clear how to unify the symbol table across such links. (Perhaps we
  1001. should emit an unconditionally indexed symtab, like the compiler
  1002. does.) */
  1003. if (ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs) < 0)
  1004. {
  1005. free (inputs);
  1006. free (parents);
  1007. goto err_outputs;
  1008. }
  1009. free (inputs);
  1010. free (parents);
  1011. /* Splice any errors or warnings created during this link back into the
  1012. dict that the caller knows about. */
  1013. ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
  1014. /* This output now becomes an input to the next link phase, with a name
  1015. equal to the CU name. We have to wrap it in an archive wrapper
  1016. first. */
  1017. if ((in_arc = ctf_new_archive_internal (0, 0, NULL, outputs[0], NULL,
  1018. NULL, &err)) == NULL)
  1019. {
  1020. ctf_set_errno (fp, err);
  1021. goto err_outputs;
  1022. }
  1023. if (ctf_link_add_ctf_internal (fp, in_arc, NULL,
  1024. ctf_cuname (outputs[0])) < 0)
  1025. {
  1026. ctf_err_warn (fp, 0, 0, _("cannot add intermediate files to link"));
  1027. goto err_outputs;
  1028. }
  1029. ctf_dict_close (out);
  1030. free (outputs);
  1031. continue;
  1032. err_inputs_outputs:
  1033. ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
  1034. ctf_dict_close (outputs[0]);
  1035. free (outputs);
  1036. err_inputs:
  1037. ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs);
  1038. ctf_dict_close (out);
  1039. free (inputs);
  1040. free (parents);
  1041. err_open_inputs:
  1042. ctf_next_destroy (i);
  1043. return -1;
  1044. err_outputs:
  1045. ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
  1046. ctf_dict_close (outputs[0]);
  1047. free (outputs);
  1048. ctf_next_destroy (i);
  1049. return -1; /* Errno is set for us. */
  1050. }
  1051. if (err != ECTF_NEXT_END)
  1052. {
  1053. ctf_err_warn (fp, 0, err, _("iteration error in CU-mapped deduplicating "
  1054. "link"));
  1055. return ctf_set_errno (fp, err);
  1056. }
  1057. return 0;
  1058. }
  1059. /* Do a deduplicating link using the ctf-dedup machinery. */
  1060. static void
  1061. ctf_link_deduplicating (ctf_dict_t *fp)
  1062. {
  1063. size_t i;
  1064. ctf_dict_t **inputs, **outputs = NULL;
  1065. ssize_t ninputs;
  1066. uint32_t noutputs;
  1067. uint32_t *parents;
  1068. if (ctf_dedup_atoms_init (fp) < 0)
  1069. {
  1070. ctf_err_warn (fp, 0, 0, _("allocating CTF dedup atoms table"));
  1071. return; /* Errno is set for us. */
  1072. }
  1073. if (fp->ctf_link_out_cu_mapping
  1074. && (ctf_link_deduplicating_per_cu (fp) < 0))
  1075. return; /* Errno is set for us. */
  1076. if ((ninputs = ctf_link_deduplicating_count_inputs (fp, NULL, NULL)) < 0)
  1077. return; /* Errno is set for us. */
  1078. if ((inputs = ctf_link_deduplicating_open_inputs (fp, NULL, ninputs,
  1079. &parents)) == NULL)
  1080. return; /* Errno is set for us. */
  1081. if (ninputs == 1 && ctf_cuname (inputs[0]) != NULL)
  1082. ctf_cuname_set (fp, ctf_cuname (inputs[0]));
  1083. if (ctf_dedup (fp, inputs, ninputs, parents, 0) < 0)
  1084. {
  1085. ctf_err_warn (fp, 0, 0, _("deduplication failed for %s"),
  1086. ctf_link_input_name (fp));
  1087. goto err;
  1088. }
  1089. if ((outputs = ctf_dedup_emit (fp, inputs, ninputs, parents, &noutputs,
  1090. 0)) == NULL)
  1091. {
  1092. ctf_err_warn (fp, 0, 0, _("deduplicating link type emission failed "
  1093. "for %s"), ctf_link_input_name (fp));
  1094. goto err;
  1095. }
  1096. if (!ctf_assert (fp, outputs[0] == fp))
  1097. {
  1098. for (i = 1; i < noutputs; i++)
  1099. ctf_dict_close (outputs[i]);
  1100. goto err;
  1101. }
  1102. for (i = 0; i < noutputs; i++)
  1103. {
  1104. char *dynname;
  1105. /* We already have access to this one. Close the duplicate. */
  1106. if (i == 0)
  1107. {
  1108. ctf_dict_close (outputs[0]);
  1109. continue;
  1110. }
  1111. if ((dynname = strdup (ctf_cuname (outputs[i]))) == NULL)
  1112. goto oom_one_output;
  1113. if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, outputs[i]) < 0)
  1114. goto oom_one_output;
  1115. continue;
  1116. oom_one_output:
  1117. ctf_set_errno (fp, ENOMEM);
  1118. ctf_err_warn (fp, 0, 0, _("out of memory allocating link outputs"));
  1119. free (dynname);
  1120. for (; i < noutputs; i++)
  1121. ctf_dict_close (outputs[i]);
  1122. goto err;
  1123. }
  1124. if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
  1125. && ctf_link_deduplicating_variables (fp, inputs, ninputs, 0) < 0)
  1126. {
  1127. ctf_err_warn (fp, 0, 0, _("deduplicating link variable emission failed for "
  1128. "%s"), ctf_link_input_name (fp));
  1129. goto err_clean_outputs;
  1130. }
  1131. if (ctf_link_deduplicating_syms (fp, inputs, ninputs, 0) < 0)
  1132. {
  1133. ctf_err_warn (fp, 0, 0, _("deduplicating link symbol emission failed for "
  1134. "%s"), ctf_link_input_name (fp));
  1135. goto err_clean_outputs;
  1136. }
  1137. ctf_dedup_fini (fp, outputs, noutputs);
  1138. /* Now close all the inputs, including per-CU intermediates. */
  1139. if (ctf_link_deduplicating_close_inputs (fp, NULL, inputs, ninputs) < 0)
  1140. return; /* errno is set for us. */
  1141. ninputs = 0; /* Prevent double-close. */
  1142. ctf_set_errno (fp, 0);
  1143. /* Fall through. */
  1144. err:
  1145. for (i = 0; i < (size_t) ninputs; i++)
  1146. ctf_dict_close (inputs[i]);
  1147. free (inputs);
  1148. free (parents);
  1149. free (outputs);
  1150. return;
  1151. err_clean_outputs:
  1152. for (i = 1; i < noutputs; i++)
  1153. {
  1154. ctf_dynhash_remove (fp->ctf_link_outputs, ctf_cuname (outputs[i]));
  1155. ctf_dict_close (outputs[i]);
  1156. }
  1157. goto err;
  1158. }
  1159. /* Merge types and variable sections in all dicts added to the link together.
  1160. All the added dicts are closed. */
  1161. int
  1162. ctf_link (ctf_dict_t *fp, int flags)
  1163. {
  1164. ctf_next_t *i = NULL;
  1165. int err;
  1166. fp->ctf_link_flags = flags;
  1167. if (fp->ctf_link_inputs == NULL)
  1168. return 0; /* Nothing to do. */
  1169. if (fp->ctf_link_outputs == NULL)
  1170. fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
  1171. ctf_hash_eq_string, free,
  1172. (ctf_hash_free_fun)
  1173. ctf_dict_close);
  1174. if (fp->ctf_link_outputs == NULL)
  1175. return ctf_set_errno (fp, ENOMEM);
  1176. /* Create empty CUs if requested. We do not currently claim that multiple
  1177. links in succession with CTF_LINK_EMPTY_CU_MAPPINGS set in some calls and
  1178. not set in others will do anything especially sensible. */
  1179. fp->ctf_flags |= LCTF_LINKING;
  1180. if (fp->ctf_link_out_cu_mapping && (flags & CTF_LINK_EMPTY_CU_MAPPINGS))
  1181. {
  1182. void *v;
  1183. while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &v,
  1184. NULL)) == 0)
  1185. {
  1186. const char *to = (const char *) v;
  1187. if (ctf_create_per_cu (fp, to) == NULL)
  1188. {
  1189. fp->ctf_flags &= ~LCTF_LINKING;
  1190. ctf_next_destroy (i);
  1191. return -1; /* Errno is set for us. */
  1192. }
  1193. }
  1194. if (err != ECTF_NEXT_END)
  1195. {
  1196. fp->ctf_flags &= ~LCTF_LINKING;
  1197. ctf_err_warn (fp, 1, err, _("iteration error creating empty CUs"));
  1198. ctf_set_errno (fp, err);
  1199. return -1;
  1200. }
  1201. }
  1202. ctf_link_deduplicating (fp);
  1203. fp->ctf_flags &= ~LCTF_LINKING;
  1204. if ((ctf_errno (fp) != 0) && (ctf_errno (fp) != ECTF_NOCTFDATA))
  1205. return -1;
  1206. return 0;
  1207. }
  1208. typedef struct ctf_link_out_string_cb_arg
  1209. {
  1210. const char *str;
  1211. uint32_t offset;
  1212. int err;
  1213. } ctf_link_out_string_cb_arg_t;
  1214. /* Intern a string in the string table of an output per-CU CTF file. */
  1215. static void
  1216. ctf_link_intern_extern_string (void *key _libctf_unused_, void *value,
  1217. void *arg_)
  1218. {
  1219. ctf_dict_t *fp = (ctf_dict_t *) value;
  1220. ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_;
  1221. fp->ctf_flags |= LCTF_DIRTY;
  1222. if (!ctf_str_add_external (fp, arg->str, arg->offset))
  1223. arg->err = ENOMEM;
  1224. }
  1225. /* Repeatedly call ADD_STRING to acquire strings from the external string table,
  1226. adding them to the atoms table for this CU and all subsidiary CUs.
  1227. If ctf_link is also called, it must be called first if you want the new CTF
  1228. files ctf_link can create to get their strings dedupped against the ELF
  1229. strtab properly. */
  1230. int
  1231. ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string,
  1232. void *arg)
  1233. {
  1234. const char *str;
  1235. uint32_t offset;
  1236. int err = 0;
  1237. while ((str = add_string (&offset, arg)) != NULL)
  1238. {
  1239. ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 };
  1240. fp->ctf_flags |= LCTF_DIRTY;
  1241. if (!ctf_str_add_external (fp, str, offset))
  1242. err = ENOMEM;
  1243. ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string,
  1244. &iter_arg);
  1245. if (iter_arg.err)
  1246. err = iter_arg.err;
  1247. }
  1248. if (err)
  1249. ctf_set_errno (fp, err);
  1250. return -err;
  1251. }
  1252. /* Inform the ctf-link machinery of a new symbol in the target symbol table
  1253. (which must be some symtab that is not usually stripped, and which
  1254. is in agreement with ctf_bfdopen_ctfsect). May be called either before or
  1255. after ctf_link_add_strtab. */
  1256. int
  1257. ctf_link_add_linker_symbol (ctf_dict_t *fp, ctf_link_sym_t *sym)
  1258. {
  1259. ctf_in_flight_dynsym_t *cid;
  1260. /* Cheat a little: if there is already an ENOMEM error code recorded against
  1261. this dict, we shouldn't even try to add symbols because there will be no
  1262. memory to do so: probably we failed to add some previous symbol. This
  1263. makes out-of-memory exits 'sticky' across calls to this function, so the
  1264. caller doesn't need to worry about error conditions. */
  1265. if (ctf_errno (fp) == ENOMEM)
  1266. return -ENOMEM; /* errno is set for us. */
  1267. if (ctf_symtab_skippable (sym))
  1268. return 0;
  1269. if (sym->st_type != STT_OBJECT && sym->st_type != STT_FUNC)
  1270. return 0;
  1271. /* Add the symbol to the in-flight list. */
  1272. if ((cid = malloc (sizeof (ctf_in_flight_dynsym_t))) == NULL)
  1273. goto oom;
  1274. cid->cid_sym = *sym;
  1275. ctf_list_append (&fp->ctf_in_flight_dynsyms, cid);
  1276. return 0;
  1277. oom:
  1278. ctf_dynhash_destroy (fp->ctf_dynsyms);
  1279. fp->ctf_dynsyms = NULL;
  1280. ctf_set_errno (fp, ENOMEM);
  1281. return -ENOMEM;
  1282. }
  1283. /* Impose an ordering on symbols. The ordering takes effect immediately, but
  1284. since the ordering info does not include type IDs, lookups may return nothing
  1285. until such IDs are added by calls to ctf_add_*_sym. Must be called after
  1286. ctf_link_add_strtab and ctf_link_add_linker_symbol. */
  1287. int
  1288. ctf_link_shuffle_syms (ctf_dict_t *fp)
  1289. {
  1290. ctf_in_flight_dynsym_t *did, *nid;
  1291. ctf_next_t *i = NULL;
  1292. int err = ENOMEM;
  1293. void *name_, *sym_;
  1294. if (!fp->ctf_dynsyms)
  1295. {
  1296. fp->ctf_dynsyms = ctf_dynhash_create (ctf_hash_string,
  1297. ctf_hash_eq_string,
  1298. NULL, free);
  1299. if (!fp->ctf_dynsyms)
  1300. {
  1301. ctf_set_errno (fp, ENOMEM);
  1302. return -ENOMEM;
  1303. }
  1304. }
  1305. /* Add all the symbols, excluding only those we already know are prohibited
  1306. from appearing in symtypetabs. */
  1307. for (did = ctf_list_next (&fp->ctf_in_flight_dynsyms); did != NULL; did = nid)
  1308. {
  1309. ctf_link_sym_t *new_sym;
  1310. nid = ctf_list_next (did);
  1311. ctf_list_delete (&fp->ctf_in_flight_dynsyms, did);
  1312. /* We might get a name or an external strtab offset. The strtab offset is
  1313. guaranteed resolvable at this point, so turn it into a string. */
  1314. if (did->cid_sym.st_name == NULL)
  1315. {
  1316. uint32_t off = CTF_SET_STID (did->cid_sym.st_nameidx, CTF_STRTAB_1);
  1317. did->cid_sym.st_name = ctf_strraw (fp, off);
  1318. did->cid_sym.st_nameidx_set = 0;
  1319. if (!ctf_assert (fp, did->cid_sym.st_name != NULL))
  1320. return -ECTF_INTERNAL; /* errno is set for us. */
  1321. }
  1322. /* The symbol might have turned out to be nameless, so we have to recheck
  1323. for skippability here. */
  1324. if (!ctf_symtab_skippable (&did->cid_sym))
  1325. {
  1326. ctf_dprintf ("symbol from linker: %s (%x)\n", did->cid_sym.st_name,
  1327. did->cid_sym.st_symidx);
  1328. if ((new_sym = malloc (sizeof (ctf_link_sym_t))) == NULL)
  1329. goto local_oom;
  1330. memcpy (new_sym, &did->cid_sym, sizeof (ctf_link_sym_t));
  1331. if (ctf_dynhash_cinsert (fp->ctf_dynsyms, new_sym->st_name, new_sym) < 0)
  1332. goto local_oom;
  1333. if (fp->ctf_dynsymmax < new_sym->st_symidx)
  1334. fp->ctf_dynsymmax = new_sym->st_symidx;
  1335. }
  1336. free (did);
  1337. continue;
  1338. local_oom:
  1339. free (did);
  1340. free (new_sym);
  1341. goto err;
  1342. }
  1343. /* If no symbols are reported, unwind what we have done and return. This
  1344. makes it a bit easier for the serializer to tell that no symbols have been
  1345. reported and that it should look elsewhere for reported symbols. */
  1346. if (!ctf_dynhash_elements (fp->ctf_dynsyms))
  1347. {
  1348. ctf_dprintf ("No symbols: not a final link.\n");
  1349. ctf_dynhash_destroy (fp->ctf_dynsyms);
  1350. fp->ctf_dynsyms = NULL;
  1351. return 0;
  1352. }
  1353. /* Construct a mapping from shndx to the symbol info. */
  1354. free (fp->ctf_dynsymidx);
  1355. if ((fp->ctf_dynsymidx = calloc (fp->ctf_dynsymmax + 1,
  1356. sizeof (ctf_link_sym_t *))) == NULL)
  1357. goto err;
  1358. while ((err = ctf_dynhash_next (fp->ctf_dynsyms, &i, &name_, &sym_)) == 0)
  1359. {
  1360. const char *name = (const char *) name;
  1361. ctf_link_sym_t *symp = (ctf_link_sym_t *) sym_;
  1362. if (!ctf_assert (fp, symp->st_symidx <= fp->ctf_dynsymmax))
  1363. {
  1364. ctf_next_destroy (i);
  1365. err = ctf_errno (fp);
  1366. goto err;
  1367. }
  1368. fp->ctf_dynsymidx[symp->st_symidx] = symp;
  1369. }
  1370. if (err != ECTF_NEXT_END)
  1371. {
  1372. ctf_err_warn (fp, 0, err, _("error iterating over shuffled symbols"));
  1373. goto err;
  1374. }
  1375. return 0;
  1376. err:
  1377. /* Leave the in-flight symbols around: they'll be freed at
  1378. dict close time regardless. */
  1379. ctf_dynhash_destroy (fp->ctf_dynsyms);
  1380. fp->ctf_dynsyms = NULL;
  1381. free (fp->ctf_dynsymidx);
  1382. fp->ctf_dynsymidx = NULL;
  1383. fp->ctf_dynsymmax = 0;
  1384. ctf_set_errno (fp, err);
  1385. return -err;
  1386. }
  1387. typedef struct ctf_name_list_accum_cb_arg
  1388. {
  1389. char **names;
  1390. ctf_dict_t *fp;
  1391. ctf_dict_t **files;
  1392. size_t i;
  1393. char **dynames;
  1394. size_t ndynames;
  1395. } ctf_name_list_accum_cb_arg_t;
  1396. /* Accumulate the names and a count of the names in the link output hash. */
  1397. static void
  1398. ctf_accumulate_archive_names (void *key, void *value, void *arg_)
  1399. {
  1400. const char *name = (const char *) key;
  1401. ctf_dict_t *fp = (ctf_dict_t *) value;
  1402. char **names;
  1403. ctf_dict_t **files;
  1404. ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_;
  1405. if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL)
  1406. {
  1407. (arg->i)--;
  1408. ctf_set_errno (arg->fp, ENOMEM);
  1409. return;
  1410. }
  1411. if ((files = realloc (arg->files, sizeof (ctf_dict_t *) * arg->i)) == NULL)
  1412. {
  1413. (arg->i)--;
  1414. ctf_set_errno (arg->fp, ENOMEM);
  1415. return;
  1416. }
  1417. /* Allow the caller to get in and modify the name at the last minute. If the
  1418. caller *does* modify the name, we have to stash away the new name the
  1419. caller returned so we can free it later on. (The original name is the key
  1420. of the ctf_link_outputs hash and is freed by the dynhash machinery.) */
  1421. if (fp->ctf_link_memb_name_changer)
  1422. {
  1423. char **dynames;
  1424. char *dyname;
  1425. void *nc_arg = fp->ctf_link_memb_name_changer_arg;
  1426. dyname = fp->ctf_link_memb_name_changer (fp, name, nc_arg);
  1427. if (dyname != NULL)
  1428. {
  1429. if ((dynames = realloc (arg->dynames,
  1430. sizeof (char *) * ++(arg->ndynames))) == NULL)
  1431. {
  1432. (arg->ndynames)--;
  1433. ctf_set_errno (arg->fp, ENOMEM);
  1434. return;
  1435. }
  1436. arg->dynames = dynames;
  1437. name = (const char *) dyname;
  1438. }
  1439. }
  1440. arg->names = names;
  1441. arg->names[(arg->i) - 1] = (char *) name;
  1442. arg->files = files;
  1443. arg->files[(arg->i) - 1] = fp;
  1444. }
  1445. /* Change the name of the parent CTF section, if the name transformer has got to
  1446. it. */
  1447. static void
  1448. ctf_change_parent_name (void *key _libctf_unused_, void *value, void *arg)
  1449. {
  1450. ctf_dict_t *fp = (ctf_dict_t *) value;
  1451. const char *name = (const char *) arg;
  1452. ctf_parent_name_set (fp, name);
  1453. }
  1454. /* Warn if we may suffer information loss because the CTF input files are too
  1455. old. Usually we provide complete backward compatibility, but compiler
  1456. changes etc which never hit a release may have a flag in the header that
  1457. simply prevents those changes from being used. */
  1458. static void
  1459. ctf_link_warn_outdated_inputs (ctf_dict_t *fp)
  1460. {
  1461. ctf_next_t *i = NULL;
  1462. void *name_;
  1463. void *ifp_;
  1464. int err;
  1465. while ((err = ctf_dynhash_next (fp->ctf_link_inputs, &i, &name_, &ifp_)) == 0)
  1466. {
  1467. const char *name = (const char *) name_;
  1468. ctf_dict_t *ifp = (ctf_dict_t *) ifp_;
  1469. if (!(ifp->ctf_header->cth_flags & CTF_F_NEWFUNCINFO)
  1470. && (ifp->ctf_header->cth_varoff - ifp->ctf_header->cth_funcoff) > 0)
  1471. ctf_err_warn (ifp, 1, 0, _("linker input %s has CTF func info but uses "
  1472. "an old, unreleased func info format: "
  1473. "this func info section will be dropped."),
  1474. name);
  1475. }
  1476. if (err != ECTF_NEXT_END)
  1477. ctf_err_warn (fp, 0, err, _("error checking for outdated inputs"));
  1478. }
  1479. /* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
  1480. (otherwise) into a new dynamically-allocated string, and return it.
  1481. Members with sizes above THRESHOLD are compressed. */
  1482. unsigned char *
  1483. ctf_link_write (ctf_dict_t *fp, size_t *size, size_t threshold)
  1484. {
  1485. ctf_name_list_accum_cb_arg_t arg;
  1486. char **names;
  1487. char *transformed_name = NULL;
  1488. ctf_dict_t **files;
  1489. FILE *f = NULL;
  1490. size_t i;
  1491. int err;
  1492. long fsize;
  1493. const char *errloc;
  1494. unsigned char *buf = NULL;
  1495. memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t));
  1496. arg.fp = fp;
  1497. fp->ctf_flags |= LCTF_LINKING;
  1498. ctf_link_warn_outdated_inputs (fp);
  1499. if (fp->ctf_link_outputs)
  1500. {
  1501. ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg);
  1502. if (ctf_errno (fp) < 0)
  1503. {
  1504. errloc = "hash creation";
  1505. goto err;
  1506. }
  1507. }
  1508. /* No extra outputs? Just write a simple ctf_dict_t. */
  1509. if (arg.i == 0)
  1510. {
  1511. unsigned char *ret = ctf_write_mem (fp, size, threshold);
  1512. fp->ctf_flags &= ~LCTF_LINKING;
  1513. return ret;
  1514. }
  1515. /* Writing an archive. Stick ourselves (the shared repository, parent of all
  1516. other archives) on the front of it with the default name. */
  1517. if ((names = realloc (arg.names, sizeof (char *) * (arg.i + 1))) == NULL)
  1518. {
  1519. errloc = "name reallocation";
  1520. goto err_no;
  1521. }
  1522. arg.names = names;
  1523. memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i));
  1524. arg.names[0] = (char *) _CTF_SECTION;
  1525. if (fp->ctf_link_memb_name_changer)
  1526. {
  1527. void *nc_arg = fp->ctf_link_memb_name_changer_arg;
  1528. transformed_name = fp->ctf_link_memb_name_changer (fp, _CTF_SECTION,
  1529. nc_arg);
  1530. if (transformed_name != NULL)
  1531. {
  1532. arg.names[0] = transformed_name;
  1533. ctf_dynhash_iter (fp->ctf_link_outputs, ctf_change_parent_name,
  1534. transformed_name);
  1535. }
  1536. }
  1537. /* Propagate the link flags to all the dicts in this link. */
  1538. for (i = 0; i < arg.i; i++)
  1539. {
  1540. arg.files[i]->ctf_link_flags = fp->ctf_link_flags;
  1541. arg.files[i]->ctf_flags |= LCTF_LINKING;
  1542. }
  1543. if ((files = realloc (arg.files,
  1544. sizeof (struct ctf_dict *) * (arg.i + 1))) == NULL)
  1545. {
  1546. errloc = "ctf_dict reallocation";
  1547. goto err_no;
  1548. }
  1549. arg.files = files;
  1550. memmove (&(arg.files[1]), arg.files, sizeof (ctf_dict_t *) * (arg.i));
  1551. arg.files[0] = fp;
  1552. if ((f = tmpfile ()) == NULL)
  1553. {
  1554. errloc = "tempfile creation";
  1555. goto err_no;
  1556. }
  1557. if ((err = ctf_arc_write_fd (fileno (f), arg.files, arg.i + 1,
  1558. (const char **) arg.names,
  1559. threshold)) < 0)
  1560. {
  1561. errloc = "archive writing";
  1562. ctf_set_errno (fp, err);
  1563. goto err;
  1564. }
  1565. if (fseek (f, 0, SEEK_END) < 0)
  1566. {
  1567. errloc = "seeking to end";
  1568. goto err_no;
  1569. }
  1570. if ((fsize = ftell (f)) < 0)
  1571. {
  1572. errloc = "filesize determination";
  1573. goto err_no;
  1574. }
  1575. if (fseek (f, 0, SEEK_SET) < 0)
  1576. {
  1577. errloc = "filepos resetting";
  1578. goto err_no;
  1579. }
  1580. if ((buf = malloc (fsize)) == NULL)
  1581. {
  1582. errloc = "CTF archive buffer allocation";
  1583. goto err_no;
  1584. }
  1585. while (!feof (f) && fread (buf, fsize, 1, f) == 0)
  1586. if (ferror (f))
  1587. {
  1588. errloc = "reading archive from temporary file";
  1589. goto err_no;
  1590. }
  1591. *size = fsize;
  1592. free (arg.names);
  1593. free (arg.files);
  1594. free (transformed_name);
  1595. if (arg.ndynames)
  1596. {
  1597. size_t i;
  1598. for (i = 0; i < arg.ndynames; i++)
  1599. free (arg.dynames[i]);
  1600. free (arg.dynames);
  1601. }
  1602. fclose (f);
  1603. return buf;
  1604. err_no:
  1605. ctf_set_errno (fp, errno);
  1606. /* Turn off the is-linking flag on all the dicts in this link. */
  1607. for (i = 0; i < arg.i; i++)
  1608. arg.files[i]->ctf_flags &= ~LCTF_LINKING;
  1609. err:
  1610. free (buf);
  1611. if (f)
  1612. fclose (f);
  1613. free (arg.names);
  1614. free (arg.files);
  1615. free (transformed_name);
  1616. if (arg.ndynames)
  1617. {
  1618. size_t i;
  1619. for (i = 0; i < arg.ndynames; i++)
  1620. free (arg.dynames[i]);
  1621. free (arg.dynames);
  1622. }
  1623. ctf_err_warn (fp, 0, 0, _("cannot write archive in link: %s failure"),
  1624. errloc);
  1625. return NULL;
  1626. }