elfcomm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* elfcomm.c -- common code for ELF format file.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Originally developed by Eric Youngdale <eric@andante.jic.com>
  4. Modifications by Nick Clifton <nickc@redhat.com>
  5. This file is part of GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* Do not include bfd.h in this file. Functions in this file are used
  19. by readelf.c and elfedit.c which define BFD64, and by objdump.c
  20. which doesn't. */
  21. #include "sysdep.h"
  22. #include "libiberty.h"
  23. #include "bfd.h"
  24. #include "filenames.h"
  25. #include "aout/ar.h"
  26. #include "elfcomm.h"
  27. #include <assert.h>
  28. extern char *program_name;
  29. void
  30. error (const char *message, ...)
  31. {
  32. va_list args;
  33. /* Try to keep error messages in sync with the program's normal output. */
  34. fflush (stdout);
  35. va_start (args, message);
  36. fprintf (stderr, _("%s: Error: "), program_name);
  37. vfprintf (stderr, message, args);
  38. va_end (args);
  39. }
  40. void
  41. warn (const char *message, ...)
  42. {
  43. va_list args;
  44. /* Try to keep warning messages in sync with the program's normal output. */
  45. fflush (stdout);
  46. va_start (args, message);
  47. fprintf (stderr, _("%s: Warning: "), program_name);
  48. vfprintf (stderr, message, args);
  49. va_end (args);
  50. }
  51. void (*byte_put) (unsigned char *, elf_vma, unsigned int);
  52. void
  53. byte_put_little_endian (unsigned char * field, elf_vma value, unsigned int size)
  54. {
  55. if (size > sizeof (elf_vma))
  56. {
  57. error (_("Unhandled data length: %d\n"), size);
  58. abort ();
  59. }
  60. while (size--)
  61. {
  62. *field++ = value & 0xff;
  63. value >>= 8;
  64. }
  65. }
  66. void
  67. byte_put_big_endian (unsigned char * field, elf_vma value, unsigned int size)
  68. {
  69. if (size > sizeof (elf_vma))
  70. {
  71. error (_("Unhandled data length: %d\n"), size);
  72. abort ();
  73. }
  74. while (size--)
  75. {
  76. field[size] = value & 0xff;
  77. value >>= 8;
  78. }
  79. }
  80. elf_vma (*byte_get) (const unsigned char *, unsigned int);
  81. elf_vma
  82. byte_get_little_endian (const unsigned char *field, unsigned int size)
  83. {
  84. switch (size)
  85. {
  86. case 1:
  87. return *field;
  88. case 2:
  89. return ((unsigned int) (field[0]))
  90. | (((unsigned int) (field[1])) << 8);
  91. case 3:
  92. return ((unsigned long) (field[0]))
  93. | (((unsigned long) (field[1])) << 8)
  94. | (((unsigned long) (field[2])) << 16);
  95. case 4:
  96. return ((unsigned long) (field[0]))
  97. | (((unsigned long) (field[1])) << 8)
  98. | (((unsigned long) (field[2])) << 16)
  99. | (((unsigned long) (field[3])) << 24);
  100. case 5:
  101. if (sizeof (elf_vma) >= 8)
  102. return ((elf_vma) (field[0]))
  103. | (((elf_vma) (field[1])) << 8)
  104. | (((elf_vma) (field[2])) << 16)
  105. | (((elf_vma) (field[3])) << 24)
  106. | (((elf_vma) (field[4])) << 32);
  107. /* Fall through. */
  108. case 6:
  109. if (sizeof (elf_vma) >= 8)
  110. return ((elf_vma) (field[0]))
  111. | (((elf_vma) (field[1])) << 8)
  112. | (((elf_vma) (field[2])) << 16)
  113. | (((elf_vma) (field[3])) << 24)
  114. | (((elf_vma) (field[4])) << 32)
  115. | (((elf_vma) (field[5])) << 40);
  116. /* Fall through. */
  117. case 7:
  118. if (sizeof (elf_vma) >= 8)
  119. return ((elf_vma) (field[0]))
  120. | (((elf_vma) (field[1])) << 8)
  121. | (((elf_vma) (field[2])) << 16)
  122. | (((elf_vma) (field[3])) << 24)
  123. | (((elf_vma) (field[4])) << 32)
  124. | (((elf_vma) (field[5])) << 40)
  125. | (((elf_vma) (field[6])) << 48);
  126. /* Fall through. */
  127. case 8:
  128. if (sizeof (elf_vma) >= 8)
  129. return ((elf_vma) (field[0]))
  130. | (((elf_vma) (field[1])) << 8)
  131. | (((elf_vma) (field[2])) << 16)
  132. | (((elf_vma) (field[3])) << 24)
  133. | (((elf_vma) (field[4])) << 32)
  134. | (((elf_vma) (field[5])) << 40)
  135. | (((elf_vma) (field[6])) << 48)
  136. | (((elf_vma) (field[7])) << 56);
  137. /* Fall through. */
  138. default:
  139. error (_("Unhandled data length: %d\n"), size);
  140. abort ();
  141. }
  142. }
  143. elf_vma
  144. byte_get_big_endian (const unsigned char *field, unsigned int size)
  145. {
  146. switch (size)
  147. {
  148. case 1:
  149. return *field;
  150. case 2:
  151. return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
  152. case 3:
  153. return ((unsigned long) (field[2]))
  154. | (((unsigned long) (field[1])) << 8)
  155. | (((unsigned long) (field[0])) << 16);
  156. case 4:
  157. return ((unsigned long) (field[3]))
  158. | (((unsigned long) (field[2])) << 8)
  159. | (((unsigned long) (field[1])) << 16)
  160. | (((unsigned long) (field[0])) << 24);
  161. case 5:
  162. if (sizeof (elf_vma) >= 8)
  163. return ((elf_vma) (field[4]))
  164. | (((elf_vma) (field[3])) << 8)
  165. | (((elf_vma) (field[2])) << 16)
  166. | (((elf_vma) (field[1])) << 24)
  167. | (((elf_vma) (field[0])) << 32);
  168. /* Fall through. */
  169. case 6:
  170. if (sizeof (elf_vma) >= 8)
  171. return ((elf_vma) (field[5]))
  172. | (((elf_vma) (field[4])) << 8)
  173. | (((elf_vma) (field[3])) << 16)
  174. | (((elf_vma) (field[2])) << 24)
  175. | (((elf_vma) (field[1])) << 32)
  176. | (((elf_vma) (field[0])) << 40);
  177. /* Fall through. */
  178. case 7:
  179. if (sizeof (elf_vma) >= 8)
  180. return ((elf_vma) (field[6]))
  181. | (((elf_vma) (field[5])) << 8)
  182. | (((elf_vma) (field[4])) << 16)
  183. | (((elf_vma) (field[3])) << 24)
  184. | (((elf_vma) (field[2])) << 32)
  185. | (((elf_vma) (field[1])) << 40)
  186. | (((elf_vma) (field[0])) << 48);
  187. /* Fall through. */
  188. case 8:
  189. if (sizeof (elf_vma) >= 8)
  190. return ((elf_vma) (field[7]))
  191. | (((elf_vma) (field[6])) << 8)
  192. | (((elf_vma) (field[5])) << 16)
  193. | (((elf_vma) (field[4])) << 24)
  194. | (((elf_vma) (field[3])) << 32)
  195. | (((elf_vma) (field[2])) << 40)
  196. | (((elf_vma) (field[1])) << 48)
  197. | (((elf_vma) (field[0])) << 56);
  198. /* Fall through. */
  199. default:
  200. error (_("Unhandled data length: %d\n"), size);
  201. abort ();
  202. }
  203. }
  204. elf_vma
  205. byte_get_signed (const unsigned char *field, unsigned int size)
  206. {
  207. elf_vma x = byte_get (field, size);
  208. switch (size)
  209. {
  210. case 1:
  211. return (x ^ 0x80) - 0x80;
  212. case 2:
  213. return (x ^ 0x8000) - 0x8000;
  214. case 3:
  215. return (x ^ 0x800000) - 0x800000;
  216. case 4:
  217. return (x ^ 0x80000000) - 0x80000000;
  218. case 5:
  219. case 6:
  220. case 7:
  221. case 8:
  222. /* Reads of 5-, 6-, and 7-byte numbers are the result of
  223. trying to read past the end of a buffer, and will therefore
  224. not have meaningful values, so we don't try to deal with
  225. the sign in these cases. */
  226. return x;
  227. default:
  228. abort ();
  229. }
  230. }
  231. /* Return the path name for a proxy entry in a thin archive, adjusted
  232. relative to the path name of the thin archive itself if necessary.
  233. Always returns a pointer to malloc'ed memory. */
  234. char *
  235. adjust_relative_path (const char *file_name, const char *name,
  236. unsigned long name_len)
  237. {
  238. char * member_file_name;
  239. const char * base_name = lbasename (file_name);
  240. size_t amt;
  241. /* This is a proxy entry for a thin archive member.
  242. If the extended name table contains an absolute path
  243. name, or if the archive is in the current directory,
  244. use the path name as given. Otherwise, we need to
  245. find the member relative to the directory where the
  246. archive is located. */
  247. if (IS_ABSOLUTE_PATH (name) || base_name == file_name)
  248. {
  249. amt = name_len + 1;
  250. if (amt == 0)
  251. return NULL;
  252. member_file_name = (char *) malloc (amt);
  253. if (member_file_name == NULL)
  254. {
  255. error (_("Out of memory\n"));
  256. return NULL;
  257. }
  258. memcpy (member_file_name, name, name_len);
  259. member_file_name[name_len] = '\0';
  260. }
  261. else
  262. {
  263. /* Concatenate the path components of the archive file name
  264. to the relative path name from the extended name table. */
  265. size_t prefix_len = base_name - file_name;
  266. amt = prefix_len + name_len + 1;
  267. /* PR 17531: file: 2896dc8b
  268. Catch wraparound. */
  269. if (amt < prefix_len || amt < name_len)
  270. {
  271. error (_("Abnormal length of thin archive member name: %lx\n"),
  272. name_len);
  273. return NULL;
  274. }
  275. member_file_name = (char *) malloc (amt);
  276. if (member_file_name == NULL)
  277. {
  278. error (_("Out of memory\n"));
  279. return NULL;
  280. }
  281. memcpy (member_file_name, file_name, prefix_len);
  282. memcpy (member_file_name + prefix_len, name, name_len);
  283. member_file_name[prefix_len + name_len] = '\0';
  284. }
  285. return member_file_name;
  286. }
  287. /* Processes the archive index table and symbol table in ARCH.
  288. Entries in the index table are SIZEOF_AR_INDEX bytes long.
  289. Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
  290. If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
  291. ARCH->sym_size and ARCH->sym_table.
  292. It is the caller's responsibility to free ARCH->index_array and
  293. ARCH->sym_table.
  294. Returns 1 upon success, 0 otherwise.
  295. If failure occurs an error message is printed. */
  296. static int
  297. process_archive_index_and_symbols (struct archive_info *arch,
  298. unsigned int sizeof_ar_index,
  299. int read_symbols)
  300. {
  301. size_t got;
  302. unsigned long size;
  303. char fmag_save;
  304. fmag_save = arch->arhdr.ar_fmag[0];
  305. arch->arhdr.ar_fmag[0] = 0;
  306. size = strtoul (arch->arhdr.ar_size, NULL, 10);
  307. arch->arhdr.ar_fmag[0] = fmag_save;
  308. /* PR 17531: file: 912bd7de. */
  309. if ((signed long) size < 0)
  310. {
  311. error (_("%s: invalid archive header size: %ld\n"),
  312. arch->file_name, size);
  313. return 0;
  314. }
  315. size = size + (size & 1);
  316. arch->next_arhdr_offset += sizeof arch->arhdr + size;
  317. if (! read_symbols)
  318. {
  319. if (fseek (arch->file, size, SEEK_CUR) != 0)
  320. {
  321. error (_("%s: failed to skip archive symbol table\n"),
  322. arch->file_name);
  323. return 0;
  324. }
  325. }
  326. else
  327. {
  328. unsigned long i;
  329. /* A buffer used to hold numbers read in from an archive index.
  330. These are always SIZEOF_AR_INDEX bytes long and stored in
  331. big-endian format. */
  332. unsigned char integer_buffer[sizeof arch->index_num];
  333. unsigned char * index_buffer;
  334. assert (sizeof_ar_index <= sizeof integer_buffer);
  335. /* Check the size of the archive index. */
  336. if (size < sizeof_ar_index)
  337. {
  338. error (_("%s: the archive index is empty\n"), arch->file_name);
  339. return 0;
  340. }
  341. /* Read the number of entries in the archive index. */
  342. got = fread (integer_buffer, 1, sizeof_ar_index, arch->file);
  343. if (got != sizeof_ar_index)
  344. {
  345. error (_("%s: failed to read archive index\n"), arch->file_name);
  346. return 0;
  347. }
  348. arch->index_num = byte_get_big_endian (integer_buffer, sizeof_ar_index);
  349. size -= sizeof_ar_index;
  350. if (size < arch->index_num * sizeof_ar_index
  351. /* PR 17531: file: 585515d1. */
  352. || size < arch->index_num)
  353. {
  354. error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
  355. arch->file_name, (long) arch->index_num, sizeof_ar_index, size);
  356. return 0;
  357. }
  358. /* Read in the archive index. */
  359. index_buffer = (unsigned char *)
  360. malloc (arch->index_num * sizeof_ar_index);
  361. if (index_buffer == NULL)
  362. {
  363. error (_("Out of memory whilst trying to read archive symbol index\n"));
  364. return 0;
  365. }
  366. got = fread (index_buffer, sizeof_ar_index, arch->index_num, arch->file);
  367. if (got != arch->index_num)
  368. {
  369. free (index_buffer);
  370. error (_("%s: failed to read archive index\n"), arch->file_name);
  371. return 0;
  372. }
  373. size -= arch->index_num * sizeof_ar_index;
  374. /* Convert the index numbers into the host's numeric format. */
  375. arch->index_array = (elf_vma *)
  376. malloc (arch->index_num * sizeof (* arch->index_array));
  377. if (arch->index_array == NULL)
  378. {
  379. free (index_buffer);
  380. error (_("Out of memory whilst trying to convert the archive symbol index\n"));
  381. return 0;
  382. }
  383. for (i = 0; i < arch->index_num; i++)
  384. arch->index_array[i] =
  385. byte_get_big_endian ((unsigned char *) (index_buffer + (i * sizeof_ar_index)),
  386. sizeof_ar_index);
  387. free (index_buffer);
  388. /* The remaining space in the header is taken up by the symbol table. */
  389. if (size < 1)
  390. {
  391. error (_("%s: the archive has an index but no symbols\n"),
  392. arch->file_name);
  393. return 0;
  394. }
  395. arch->sym_table = (char *) malloc (size);
  396. if (arch->sym_table == NULL)
  397. {
  398. error (_("Out of memory whilst trying to read archive index symbol table\n"));
  399. return 0;
  400. }
  401. arch->sym_size = size;
  402. got = fread (arch->sym_table, 1, size, arch->file);
  403. if (got != size)
  404. {
  405. error (_("%s: failed to read archive index symbol table\n"),
  406. arch->file_name);
  407. return 0;
  408. }
  409. }
  410. /* Read the next archive header. */
  411. got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
  412. if (got != sizeof arch->arhdr && got != 0)
  413. {
  414. error (_("%s: failed to read archive header following archive index\n"),
  415. arch->file_name);
  416. return 0;
  417. }
  418. return 1;
  419. }
  420. /* Read the symbol table and long-name table from an archive. */
  421. int
  422. setup_archive (struct archive_info *arch, const char *file_name,
  423. FILE *file, off_t file_size,
  424. int is_thin_archive, int read_symbols)
  425. {
  426. size_t got;
  427. arch->file_name = strdup (file_name);
  428. arch->file = file;
  429. arch->index_num = 0;
  430. arch->index_array = NULL;
  431. arch->sym_table = NULL;
  432. arch->sym_size = 0;
  433. arch->longnames = NULL;
  434. arch->longnames_size = 0;
  435. arch->nested_member_origin = 0;
  436. arch->is_thin_archive = is_thin_archive;
  437. arch->uses_64bit_indices = 0;
  438. arch->next_arhdr_offset = SARMAG;
  439. /* Read the first archive member header. */
  440. if (fseek (file, SARMAG, SEEK_SET) != 0)
  441. {
  442. error (_("%s: failed to seek to first archive header\n"), file_name);
  443. return 1;
  444. }
  445. got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
  446. if (got != sizeof arch->arhdr)
  447. {
  448. if (got == 0)
  449. return 0;
  450. error (_("%s: failed to read archive header\n"), file_name);
  451. return 1;
  452. }
  453. /* See if this is the archive symbol table. */
  454. if (startswith (arch->arhdr.ar_name, "/ "))
  455. {
  456. if (! process_archive_index_and_symbols (arch, 4, read_symbols))
  457. return 1;
  458. }
  459. else if (startswith (arch->arhdr.ar_name, "/SYM64/ "))
  460. {
  461. arch->uses_64bit_indices = 1;
  462. if (! process_archive_index_and_symbols (arch, 8, read_symbols))
  463. return 1;
  464. }
  465. else if (read_symbols)
  466. printf (_("%s has no archive index\n"), file_name);
  467. if (startswith (arch->arhdr.ar_name, "// "))
  468. {
  469. /* This is the archive string table holding long member names. */
  470. char fmag_save = arch->arhdr.ar_fmag[0];
  471. arch->arhdr.ar_fmag[0] = 0;
  472. arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
  473. arch->arhdr.ar_fmag[0] = fmag_save;
  474. /* PR 17531: file: 01068045. */
  475. if (arch->longnames_size < 8)
  476. {
  477. error (_("%s: long name table is too small, (size = %ld)\n"),
  478. file_name, arch->longnames_size);
  479. return 1;
  480. }
  481. /* PR 17531: file: 639d6a26. */
  482. if ((off_t) arch->longnames_size > file_size
  483. || (signed long) arch->longnames_size < 0)
  484. {
  485. error (_("%s: long name table is too big, (size = 0x%lx)\n"),
  486. file_name, arch->longnames_size);
  487. return 1;
  488. }
  489. arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
  490. /* Plus one to allow for a string terminator. */
  491. arch->longnames = (char *) malloc (arch->longnames_size + 1);
  492. if (arch->longnames == NULL)
  493. {
  494. error (_("Out of memory reading long symbol names in archive\n"));
  495. return 1;
  496. }
  497. if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
  498. {
  499. free (arch->longnames);
  500. arch->longnames = NULL;
  501. error (_("%s: failed to read long symbol name string table\n"),
  502. file_name);
  503. return 1;
  504. }
  505. if ((arch->longnames_size & 1) != 0)
  506. getc (file);
  507. arch->longnames[arch->longnames_size] = 0;
  508. }
  509. return 0;
  510. }
  511. /* Open and setup a nested archive, if not already open. */
  512. int
  513. setup_nested_archive (struct archive_info *nested_arch,
  514. const char *member_file_name)
  515. {
  516. FILE * member_file;
  517. struct stat statbuf;
  518. /* Have we already setup this archive? */
  519. if (nested_arch->file_name != NULL
  520. && streq (nested_arch->file_name, member_file_name))
  521. return 0;
  522. /* Close previous file and discard cached information. */
  523. if (nested_arch->file != NULL)
  524. {
  525. fclose (nested_arch->file);
  526. nested_arch->file = NULL;
  527. }
  528. release_archive (nested_arch);
  529. member_file = fopen (member_file_name, "rb");
  530. if (member_file == NULL)
  531. return 1;
  532. if (fstat (fileno (member_file), &statbuf) < 0)
  533. return 1;
  534. return setup_archive (nested_arch, member_file_name, member_file,
  535. statbuf.st_size, 0, 0);
  536. }
  537. /* Release the memory used for the archive information. */
  538. void
  539. release_archive (struct archive_info * arch)
  540. {
  541. free (arch->file_name);
  542. free (arch->index_array);
  543. free (arch->sym_table);
  544. free (arch->longnames);
  545. arch->file_name = NULL;
  546. arch->index_array = NULL;
  547. arch->sym_table = NULL;
  548. arch->longnames = NULL;
  549. }
  550. /* Get the name of an archive member from the current archive header.
  551. For simple names, this will modify the ar_name field of the current
  552. archive header. For long names, it will return a pointer to the
  553. longnames table. For nested archives, it will open the nested archive
  554. and get the name recursively. NESTED_ARCH is a single-entry cache so
  555. we don't keep rereading the same information from a nested archive. */
  556. char *
  557. get_archive_member_name (struct archive_info *arch,
  558. struct archive_info *nested_arch)
  559. {
  560. unsigned long j, k;
  561. if (arch->arhdr.ar_name[0] == '/')
  562. {
  563. /* We have a long name. */
  564. char *endp;
  565. char *member_file_name;
  566. char *member_name;
  567. char fmag_save;
  568. if (arch->longnames == NULL || arch->longnames_size == 0)
  569. {
  570. error (_("Archive member uses long names, but no longname table found\n"));
  571. return NULL;
  572. }
  573. arch->nested_member_origin = 0;
  574. fmag_save = arch->arhdr.ar_fmag[0];
  575. arch->arhdr.ar_fmag[0] = 0;
  576. k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
  577. if (arch->is_thin_archive && endp != NULL && * endp == ':')
  578. arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
  579. arch->arhdr.ar_fmag[0] = fmag_save;
  580. if (j > arch->longnames_size)
  581. {
  582. error (_("Found long name index (%ld) beyond end of long name table\n"),j);
  583. return NULL;
  584. }
  585. while ((j < arch->longnames_size)
  586. && (arch->longnames[j] != '\n')
  587. && (arch->longnames[j] != '\0'))
  588. j++;
  589. if (j > 0 && arch->longnames[j-1] == '/')
  590. j--;
  591. if (j > arch->longnames_size)
  592. j = arch->longnames_size;
  593. arch->longnames[j] = '\0';
  594. if (!arch->is_thin_archive || arch->nested_member_origin == 0)
  595. return xstrdup (arch->longnames + k);
  596. /* PR 17531: file: 2896dc8b. */
  597. if (k >= j)
  598. {
  599. error (_("Invalid Thin archive member name\n"));
  600. return NULL;
  601. }
  602. /* This is a proxy for a member of a nested archive.
  603. Find the name of the member in that archive. */
  604. member_file_name = adjust_relative_path (arch->file_name,
  605. arch->longnames + k, j - k);
  606. if (member_file_name != NULL
  607. && setup_nested_archive (nested_arch, member_file_name) == 0)
  608. {
  609. member_name = get_archive_member_name_at (nested_arch,
  610. arch->nested_member_origin,
  611. NULL);
  612. if (member_name != NULL)
  613. {
  614. free (member_file_name);
  615. return member_name;
  616. }
  617. }
  618. free (member_file_name);
  619. /* Last resort: just return the name of the nested archive. */
  620. return xstrdup (arch->longnames + k);
  621. }
  622. /* We have a normal (short) name. */
  623. for (j = 0; j < sizeof (arch->arhdr.ar_name); j++)
  624. if (arch->arhdr.ar_name[j] == '/')
  625. {
  626. arch->arhdr.ar_name[j] = '\0';
  627. return xstrdup (arch->arhdr.ar_name);
  628. }
  629. /* The full ar_name field is used. Don't rely on ar_date starting
  630. with a zero byte. */
  631. {
  632. char *name = xmalloc (sizeof (arch->arhdr.ar_name) + 1);
  633. memcpy (name, arch->arhdr.ar_name, sizeof (arch->arhdr.ar_name));
  634. name[sizeof (arch->arhdr.ar_name)] = '\0';
  635. return name;
  636. }
  637. }
  638. /* Get the name of an archive member at a given OFFSET within an archive
  639. ARCH. */
  640. char *
  641. get_archive_member_name_at (struct archive_info *arch,
  642. unsigned long offset,
  643. struct archive_info *nested_arch)
  644. {
  645. size_t got;
  646. if (fseek (arch->file, offset, SEEK_SET) != 0)
  647. {
  648. error (_("%s: failed to seek to next file name\n"), arch->file_name);
  649. return NULL;
  650. }
  651. got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
  652. if (got != sizeof arch->arhdr)
  653. {
  654. error (_("%s: failed to read archive header\n"), arch->file_name);
  655. return NULL;
  656. }
  657. if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
  658. {
  659. error (_("%s: did not find a valid archive header\n"),
  660. arch->file_name);
  661. return NULL;
  662. }
  663. return get_archive_member_name (arch, nested_arch);
  664. }
  665. /* Construct a string showing the name of the archive member, qualified
  666. with the name of the containing archive file. For thin archives, we
  667. use square brackets to denote the indirection. For nested archives,
  668. we show the qualified name of the external member inside the square
  669. brackets (e.g., "thin.a[normal.a(foo.o)]"). */
  670. char *
  671. make_qualified_name (struct archive_info * arch,
  672. struct archive_info * nested_arch,
  673. const char *member_name)
  674. {
  675. const char * error_name = _("<corrupt>");
  676. size_t len;
  677. char * name;
  678. len = strlen (arch->file_name) + strlen (member_name) + 3;
  679. if (arch->is_thin_archive
  680. && arch->nested_member_origin != 0)
  681. {
  682. /* PR 15140: Allow for corrupt thin archives. */
  683. if (nested_arch->file_name)
  684. len += strlen (nested_arch->file_name) + 2;
  685. else
  686. len += strlen (error_name) + 2;
  687. }
  688. name = (char *) malloc (len);
  689. if (name == NULL)
  690. {
  691. error (_("Out of memory\n"));
  692. return NULL;
  693. }
  694. if (arch->is_thin_archive
  695. && arch->nested_member_origin != 0)
  696. {
  697. if (nested_arch->file_name)
  698. snprintf (name, len, "%s[%s(%s)]", arch->file_name,
  699. nested_arch->file_name, member_name);
  700. else
  701. snprintf (name, len, "%s[%s(%s)]", arch->file_name,
  702. error_name, member_name);
  703. }
  704. else if (arch->is_thin_archive)
  705. snprintf (name, len, "%s[%s]", arch->file_name, member_name);
  706. else
  707. snprintf (name, len, "%s(%s)", arch->file_name, member_name);
  708. return name;
  709. }